提高Linux工作效率的bash技巧

发表于:2016-11-15 09:31

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:jingkeke    来源:51Testing软件测试网采编

#
Linux
分享:
  帮你保持历史操作,跳回到你经常使用的目录。
  下面是我的配置文件里脚本:
# USAGE:
# s bookmarkname - saves the curr dir as bookmarkname
# g bookmarkname - jumps to the that bookmark
# g b[TAB] - tab completion is available
# l - list all bookmarks
# save current directory to bookmarks
touch ~/.sdirs
function s {
cat ~/.sdirs | grep -v "export DIR_$1=" > ~/.sdirs1
mv ~/.sdirs1 ~/.sdirs
echo "export DIR_$1=$PWD" >> ~/.sdirs
}
# jump to bookmark
function g {
source ~/.sdirs
cd $(eval $(echo echo $(echo \$DIR_$1)))
}
# list bookmarks with dirnam
function l {
source ~/.sdirs
env | grep "^DIR_" | cut -c5- | grep "^.*="
}
# list bookmarks without dirname
function _l {
source ~/.sdirs
env | grep "^DIR_" | cut -c5- | grep "^.*=" | cut -f1 -d "="
}
# completion command for g
function _gcomp {
local curw
COMPREPLY=()
curw=${COMP_WORDS[COMP_CWORD]}
COMPREPLY=($(compgen -W '`_l`' -- $curw))
return 0
}
# bind completion command for g to _gcomp
complete -F _gcomp g
  创建自己的命令包.
  通过脚本,我可以将ssh key拷贝到任何网站服务器——只需要键入dur keyuser@somehost.
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
}
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号