简化shell终端命令输入的脚本式快捷键工具_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就会列出所有的命令。

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

时间: 2024-09-17 04:23:12

简化shell终端命令输入的脚本式快捷键工具_linux shell的相关文章

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

1.解决的问题 当你需要一次输入很多个命令的时候,例如一次去多个目录删除文件cd dir1rm file1.tempcd ../../dir2rm -rf dir3 当你懒得输入一个好长的命令或者直接就记不住那么长的命令的时候,例如生成ctagsctags --languages=C++ --exclude=third_party --exclude=.git --exclude=build --exclude=out -R -f .tags 当你想要个类似快捷键来一键搞定重复的事情又懒得写好多

实现shell终端代码分享(可用户登录 实现系统命令)_linux shell

复制代码 代码如下: #include"apue.h" int userlogin(struct passwd **); int main(){ int ret,i=0; long ret_cwd; struct passwd *pw; char buf[128],hostname[16],usercwd[128]; char *ptmp1;  do{  ret = userlogin(&pw); }while(ret != 1);  getchar();  while(1){

shell结合expect写的批量scp脚本工具_linux shell

在部署一个任务时,其中有一项必须的过程就是将一些文件,如安装包发送到大量的服务器上去.虽然已有宇哥的脚本可用:通过paramiko模块提供的ssh和scp功能编写的python脚本.但我到现在还在对python的恐惧之中(虽然已经在空闲时间努力去学习了),所以使用了shell和expect脚本结合的方式,写了这个批量scp的脚本工具. expect用于自动化地执行linux环境下的命令行交互任务,例如scp.ssh之类需要用户手动输入密码然后确认的任务.有了这个工具,定义在scp过程中可能遇到的

Linux Shell脚本编程初体验_linux shell

通常,当人们提到"shell脚本语言"时,浮现在他们脑海中是bash,ksh,sh或者其它相类似的linux/unix脚本语言.脚本语言是与计算机交流的另外一种途径.使用图形化窗口界面(不管是windows还是linux都无所谓)用户可以移动鼠标并点击各种对象,比如按钮.列表.选框等等.但这种方式在每次用户想要计算机/服务器完成相同任务时(比如说批量转换照片,或者下载新的电影.mp3等)却是十分不方便.要想让所有这些事情变得简单并且自动化,我们可以使用shell脚本. 某些编程语言,像

Linux 脚本编写基础知识_linux shell

1. Linux 脚本编写基础 1.1 语法基本介绍 1.1.1 开头 程序必须以下面的行开始(必须放在文件的第一行):#!/bin/sh 符号#!用来告诉系统它后面的参数是用来执行该文件的程序.在这个例子中我们使用/bin/sh来执行程序. 当编辑好脚本时,如果要执行该脚本,还必须使其可执行. 要使脚本可执行:编译 chmod +x filename 这样才能用./filename 来运行 1.1.2 注释 在进行shell编程时,以#开头的句子表示注释,直到这一行的结束.我们真诚地建议您在程

shell脚本实现文件锁功能_linux shell

1.背景 当多个进程可能会对同样的数据执行操作时,这些进程需要保证其它进程没有在操作,以免损坏数据.通常,这样的进程会使用一个"锁文件",也就是建立一个文件来告诉别的进程自己在运行,如果检测到那个文件存在则认为有操作同样数据的进程在工作.这样的问题是,进程不小心意外死亡了,没有清理掉那个锁文件,那么只能由用户手动来清理了. 2.关于flock flock 是对于整个文件的建议性锁.也就是说,如果一个进程在一个文件(inode)上放了锁,那么其它进程是可以知道的.(建议性锁不强求进程遵守

Linux shell脚本实现CPU预警_linux shell

如果CPU占用率持续达到80以上则调用打印java线程占用率堆栈的脚本,见https://github.com/oldratlee/useful-shells/blob/master/show-busy-java-threads.sh,具体用法见他的github wiki! #!/bin/bash #cpu idle percent cpu_idle=`top -b -d 1 -n 2 | grep Cpu | awk 'NR>1{print $5}' | cut -f 1 -d ".&q

Linux Shell脚本查看NUMA信息_linux shell

Nova在NFV场景下会提供NUMA相关高级特性,这里提供一个脚本查看计算节点的NUMA相关信息. #!/bin/bash function get_nr_processor() { grep '^processor' /proc/cpuinfo | wc -l } function get_nr_socket() { grep 'physical id' /proc/cpuinfo | awk -F: '{ print $2 | "sort -un"}' | wc -l } func

Shell脚本实现的基于SVN的代码提交量统计工具_linux shell

最近没啥事,就用bash写了一个基于svn的代码统计小工具. 可以指定统计的目录,默认递归统计子目录. 目前还没有屏蔽指定目录的功能.哈 代码比较粗糙.不过先晒出来. #!/bin/bash - #""""""""""""""""""""""""""