日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

shell指令可以直接在终端输入吗_简化shell终端命令输入的脚本式快捷键工具

發布時間:2025/3/12 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 shell指令可以直接在终端输入吗_简化shell终端命令输入的脚本式快捷键工具 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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|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'

)

也就是,第一個空格前是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 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

總結

以上是生活随笔為你收集整理的shell指令可以直接在终端输入吗_简化shell终端命令输入的脚本式快捷键工具的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。