簡(jiǎn)化shell終端命令輸入的腳本式快捷鍵工具
1.解決的問題
當(dāng)你需要一次輸入很多個(gè)命令的時(shí)候,例如一次去多個(gè)目錄刪除文件
cd dir1
rm file1.temp
cd ../../dir2
rm -rf dir3
當(dāng)你懶得輸入一個(gè)好長(zhǎng)的命令或者直接就記不住那么長(zhǎng)的命令的時(shí)候,例如生成ctags
ctags --languages=C++ --exclude=third_party --exclude=.git --exclude=build --exclude=out -R -f .tags
當(dāng)你想要個(gè)類似快捷鍵來一鍵搞定重復(fù)的事情又懶得寫好多腳本的時(shí)候,
duang~~ 這個(gè)工具就有用啦!
2.感性認(rèn)識(shí)
這個(gè)工具是個(gè)shell腳本,附在本文末尾,復(fù)制整個(gè)腳本源碼,在本地保存為rew.sh,放到$PATH下,加上chmod +x權(quán)限。
文件名命名可以按喜好改。r e w 可以用一只左手輸入完畢,然后tab鍵就出.sh了,我感覺挺好的。
前面所說的復(fù)雜操作就變成了這么簡(jiǎn)單:
一次去幾個(gè)目錄刪除文件,只需要rew.sh d
如果想指定起始目錄,還可以再帶參數(shù)rew.sh d dir_path
生成ctags,只需要rew.sh ct
如果忘了這個(gè)腳本有什么功能,只需要不帶參數(shù)運(yùn)行crt.sh就會(huì)列出所有的命令。
3.理性認(rèn)識(shí)
如果想加入自定義的命令,找到
cmds=(xxxxx)
的地方,增加一行就可以了。每行就是一個(gè)參數(shù)和實(shí)際命令,可以看到:
# defines commands here,format:
# shortcut|one space|action
cmds=(
'w ninja -C out/Debug android_webview_apk'
'gyp build/gyp_chromium'
'd gdb -ex=r --args out/Debug/chrome --no-sandbox http://100.84.44.189'
'ct ctags --languages=C++ --exclude=third_party --exclude=.git --exclude=build --exclude=out -R -f .tags'
# 'e echo example to show this can be commentted'
'r ninja -C out/Debug chrome_shell_apk'
'u updateChrome'
't testChromeShellMemory'
'o openUrlInCAWShell'
)
也就是,第一個(gè)空格前是rew.sh的參數(shù)(快捷鍵),第一個(gè)空格后是實(shí)際運(yùn)行的命令。其中多個(gè)步驟的命令可以做成函數(shù),例如:
updateChrome() {
git pull
cd third_party/WebKit
git pull
gclient sync --nohooks
}
如果怕忘記長(zhǎng)命令用來干什么,也可以放到函數(shù)里,函數(shù)名要見名知意或者加注釋。
這個(gè)腳本可以被其它腳本調(diào)用,返回值和被代替的命令相同。
附工具腳本:
#!/bin/bash
#author liuhx 2015/03/03 http://blog.csdn.net/hursing
# if including multiple steps, combine them into function
updateChrome() {
git pull
cd third_party/WebKit
git pull
gclient sync --nohooks
}
testChromeShellMemory() {
ps=`adb shell ps | grep org.chromium.chrome.shell`
rsss=`echo "$ps" | awk '{print $5;}'`
echo "$rsss"
pids=`echo "$ps" | awk '{print $2;}'`
for p in $pids; do
adb shell dumpsys meminfo $p | grep TOTAL | awk '{print $2;}'
done
}
openUrlInCAWShell() {
# $1 should be url
adb shell am start -a android.intent.action.VIEW -n com.caw.webkit.test/.BrowserActivity -e policy UCM_CURRENT_WINDOW -d $1
}
# defines commands here,format:
# shortcut|one space|action
cmds=(
'w ninja -C out/Debug android_webview_apk'
'gyp build/gyp_chromium'
'd gdb -ex=r --args out/Debug/chrome --no-sandbox http://100.84.44.189'
'ct ctags --languages=C++ --exclude=third_party --exclude=.git --exclude=build --exclude=out -R -f .tags'
# 'e echo example to show this can be commentted'
'r ninja -C out/Debug chrome_shell_apk'
'u updateChrome'
't testChromeShellMemory'
'o openUrlInCAWShell'
)
echoHelp() {
for ((i = 0; i < ${#cmds[@]}; i++)); do
echo "${cmds[$i]}"
done
shName=`basename $0`
echo -e "\033[0;33;1mexample: input '$shName ${cmds[0]%% *}' to run '${cmds[0]#* }'\033[0m"
}
if [[ $# -eq 0 ]]; then
echoHelp
exit 255
fi
for ((i = 0; i < ${#cmds[@]}; i++)); do
cmd=${cmds[$i]}
shortcut=${cmd%% *}
if [[ "$shortcut"x == "$1"x ]]; then
action=${cmd#* }
echo -e "\033[0;33;1m$action\033[0m"
# skip shortcut
shift 1
eval $action $@
exit $?
fi
done
# if no cmd matched, echoHelp
echoHelp
相關(guān)文章
并發(fā)數(shù)據(jù)庫(kù)壓力測(cè)試的shell腳本代碼
并發(fā)數(shù)據(jù)庫(kù)壓力測(cè)試的shell腳本,有需要的朋友可以參考下2013-02-02svn服務(wù)器啟動(dòng)和svn服務(wù)器重啟、停止等操作腳本分享
這篇文章主要介紹了svn服務(wù)器啟動(dòng)和svn服務(wù)器重啟、停止等操作腳本,需要的朋友可以參考下2014-03-03shell腳本nicenumber實(shí)現(xiàn)代碼
給出一個(gè)數(shù)字,用逗號(hào)分隔的形式顯示出來,希望DD和TD被實(shí)例化等2016-08-08Shell獲取路徑操作(dirname $0 pwd)的實(shí)現(xiàn)
在shell腳本中經(jīng)常會(huì)看到$(cd $(dirname $0); pwd)、basename等操作,本文主要介紹了Shell獲取路徑操作(dirname $0 pwd)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02linux shell實(shí)現(xiàn)獲取用戶輸入指定范圍的單個(gè)字符的兩種方法
用shell實(shí)現(xiàn)的,要求獲取用戶輸一個(gè)字符a-zA-Z實(shí)現(xiàn)方法如下,需要的朋友可以參考下2013-03-03shell 通過makefile傳參給c語(yǔ)言的實(shí)現(xiàn)示例
本文主要介紹了shell 通過makefile傳參給c語(yǔ)言的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03