脚本式快捷键:一个简化shell终端命令输入的工具

简介: 1.解决的问题当你需要一次输入很多个命令的时候,例如一次去多个目录删除文件cd dir1rm file1.tempcd ../../dir2rm -rf dir3当你懒得输入一个好长的命令或者直接就记不住那么长的命令的时候,例如生成ctagsct...

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

转载请注明出处: http://blog.csdn.net/hursing
目录
相关文章
|
2天前
|
监控 Unix Shell
shell脚本编程学习
shell脚本编程
22 12
|
2天前
|
Unix Shell Linux
常见的shell命令
shell常用命令
18 11
|
4天前
|
Shell Linux
Linux shell编程学习笔记82:w命令——一览无余
Linux shell编程学习笔记82:w命令——一览无余
|
6天前
|
Shell
shell脚本变量 $name ${name}啥区别
shell脚本变量 $name ${name}啥区别
|
6天前
|
Java Shell Windows
java Runtime.exec()执行shell/cmd命令:常见的几种陷阱与一种完善实现
java Runtime.exec()执行shell/cmd命令:常见的几种陷阱与一种完善实现
15 5
|
8天前
|
人工智能 监控 Shell
常用的 55 个 Linux Shell 脚本(包括基础案例、文件操作、实用工具、图形化、sed、gawk)
这篇文章提供了55个常用的Linux Shell脚本实例,涵盖基础案例、文件操作、实用工具、图形化界面及sed、gawk的使用。
26 2
|
Shell Linux 网络安全
linux shell 终端中文乱码(转)
方法一:修改/etc/sysconfig/i18n 文件把里面的LANG="en_US"改成 GB2312就可以了要重启一下机器不用重启的方法,直接# LANG="GB2312"然后就可以了修改 i18n 只是为了重启有效 方法二:#vi ~/.
7367 0
|
29天前
|
Shell
Shell脚本有哪些基本语法?
【9月更文挑战第4天】
43 17
|
29天前
|
存储 Unix Shell
shell脚本编程基础
【9月更文挑战第4天】
36 12
|
28天前
|
网络协议 关系型数据库 MySQL
Shell 脚本案例
Shell 脚本案例
36 8