標籤:shell 快速鍵 簡化輸入 terminal 終端
1.解決的問題
當你需要一次輸入很多個命令的時候,例如一次去多個目錄刪除檔案
cd dir1
rm file1.temp
cd ../../dir2
rm -rf dir3
當你懶得輸入一個好長的命令或者直接就記不住那麼長的命令的時候,例如產生ctags
ctags --languages=C++ --exclude=third_party --exclude=.git --exclude=build --exclude=out -R -f .tags
當你想要個類似快速鍵來一鍵搞定重複的事情又懶得寫好多指令碼的時候,
duang~~ 這個工具就有用啦!
2.感性認識
這個工具是個shell指令碼,附在本文末尾,複製整個指令碼源碼,在本地儲存為rew.sh,放到$PATH下,加上chmod +x許可權。
檔案名稱命名可以按喜好改。r e w 可以用一隻左手輸入完畢,然後tab鍵就出.sh了,我感覺挺好的。
前面所說的複雜操作就變成了這麼簡單:
一次去幾個目錄刪除檔案,只需要rew.sh d
如果想指定起始目錄,還可以再帶參數rew.sh d dir_path
產生ctags,只需要rew.sh ct
如果忘了這個指令碼有什麼功能,只需要不帶參數運行crt.sh就會列出所有的命令。
3.理性認識如果想加入自訂的命令,找到
cmds=(xxxxx)
的地方,增加一行就可以了。每行就是一個參數和實際命令,可以看到:
# defines commands here,format:# shortcut|one space|actioncmds=( ‘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‘)
也就是,第一個空格前是rew.sh的參數(快速鍵),第一個空格後是實際啟動並執行命令。其中多個步驟的命令可以做成函數,例如
updateChrome() { git pull cd third_party/WebKit git pull gclient sync --nohooks}
如果怕忘記長命令用來幹什麼,也可以放到函數裡,函數名要見名知意或者加註釋。
這個指令碼可以被其它指令碼調用,傳回值和被代替的命令相同。
附工具指令碼:
#!/bin/bash#author liuhx 2015/03/03 http://blog.csdn.net/hursing# if including multiple steps, combine them into functionupdateChrome() { 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|actioncmds=( ‘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 255fifor ((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 [email protected] exit $? fidone# if no cmd matched, echoHelpechoHelp
轉載請註明出處:http://blog.csdn.net/hursing
指令碼式快速鍵:一個簡化shell終端命令輸入的工具