提高Linux工作效率的十大bash技巧(2)

我几乎天天都会使用它。真的。经常会有一些输出,我只需要其中的第二列,或第三列,下面这个命令就能做到这些:

#Sample output of git status -s command: $ git status -s M .bashrc ?? .vim/bundle/extempore/ # Remove status code from git status and just get the file names $ git status -s | awk '{print $2}' .bashrc .vim/bundle/extempore/

为什么不写个函数,让我们随时都可以用呢?

function col { awk -v col=$1 '{print $col}' }

这使得提取列非常容易,比如,你不想要第一列?简单:

$ git status -s | col 2 .bashrc .vim/bundle/extempore/ 技巧八、忽略头x个词

我对xargs很着迷,我感觉它就像一把快刀。但有时候用它获得的结果需要调整一下,也许需要取得一些值。例如,你想去掉下面文件影像里的一些信息:

function skip { n=$(($1 + 1)) cut -d' ' -f$n- }

下面是如何使用它:

使用 docker images 得到下面的输出:

$ docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE <none> <none> 65a9e3ef7171 3 weeks ago 1.592 GB <none> <none> 7c01ca6c30f2 3 weeks ago 11.1 MB <none> <none> 9518620e6a0e 3 weeks ago 7.426 MB <none> <none> 430707ee7fe8 3 weeks ago 7.426 MB boot2docker/boot2docker latest 1dbd7ebffe31 3 weeks ago 1.592 GB spaceghost/tinycore-x86_64 5.4 f47686df00df 7 weeks ago 11.1 MB durdn/bithub latest df1e39df8dbf 8 weeks ago 100.9 MB <none> <none> c5e6cf38d985 8 weeks ago 100.9 MB nginx latest e426f6ef897e 12 weeks ago 100.2 MB zoobab/tinycore-x64 latest 8cdd417ec611 8 months ago 7.426 MB scratch latest 511136ea3c5a 20 months ago 0 B

使用上面的函数,你可以获取所有的IDs:

$ docker images | col 3 IMAGE 65a9e3ef7171 7c01ca6c30f2 9518620e6a0e 430707ee7fe8 1dbd7ebffe31 f47686df00df df1e39df8dbf c5e6cf38d985 e426f6ef897e 8cdd417ec611 511136ea3c5a

进一步处理:

docker images | col 3 | xargs IMAGE 65a9e3ef7171 7c01ca6c30f2 9518620e6a0e 430707ee7fe8 1dbd7ebffe31 f47686df00df df1e39df8dbf c5e6cf38d985 e426f6ef897e 8cdd417ec611 511136ea3c5a

但前面的”IMAGE”字符我也想去掉:

docker images | col 3 | xargs | skip 1 65a9e3ef7171 7c01ca6c30f2 9518620e6a0e 430707ee7fe8 1dbd7ebffe31 f47686df00df df1e39df8dbf c5e6cf38d985 e426f6ef897e 8cdd417ec611 511136ea3c5a

完整的写下来就是这样:

docker rmi $(docker images | col 3 | xargs | skip 1) 技巧九、创建自己的命令包

在bash里,你可以很容易的创建自己的命令组件,你可以看一下下面我写的:

function dur { case $1 in clone|cl) git clone git@bitbucket.org:nicolapaolucci/$2.git ;; move|mv) git remote add bitbucket git@bitbucket.org:nicolapaolucci/$(basename $(pwd)).git git push --all bitbucket ;; trackall|tr) #track all remote branches of a project for remote in $(git branch -r | grep -v master ); do git checkout --track $remote ; done ;; key|k) #track all remote branches of a project ssh $2 'mkdir -p .ssh && cat >> .ssh/authorized_keys' < ~/.ssh/id_rsa.pub ;; fun|f) #list all custom bash functions defined typeset -F | col 3 | grep -v _ | xargs | fold -sw 60 ;; def|d) #show definition of function $1 typeset -f $2 ;; help|h|*) echo "[dur]dn shell automation tools" echo "commands available:" echo " [cl]one, [mv|move]" echo " [f]fun lists all bash functions defined in .bashrc" echo " [def] <fun> lists definition of function defined in .bashrc" echo " [k]ey <host> copies ssh key to target host" echo " [tr]ackall], [h]elp" ;; esac }

通过上面的脚本,我可以将ssh key拷贝到任何网站服务器——只需要键入 dur key
user@somehost.

总结

你可以试一下我的这个.bashrc文件,或你自己也可以写一个。你有更好更多的技巧吗?请写在下面的评论里,或私信给 @程序师视野 。期待你的来信。

bash的登录与欢迎信息:/etc/issue,/etc/motd 

Bash常用的几个配置文件

Bash脚本15分钟进阶教程

10个 Linux/Unix下 Bash 和 KSH shell 的作业控制实例

Ubuntu下shell脚本运行异常:Bash和dash的区别

Bash脚本之for语句if语句以及各种测试语句

什么是Bash Shell的内建(build in)命令

分享有用的 bash 别名和函数 

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/16709.html