linux快捷命令脚本,简化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就会列出所有的命令。

8b3fd5e65027f3e0dd593bdaa7e1b248.png

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值