前言
在远程连接到linux进行操作时,经常要切换目录,有些目录切换频次较高,因此写了个shell工具,用于收藏目录、切换目录等。也不需要安装,直接添加脚本即可。
配置
首先声明脚本是基于bash shell,zsh和fish未经测试。
- 编辑文件
~/.bash_custom_functions
,添加以下内容
mark(){local markfile="$HOME/.mark"if [ ! -f ${markfile} ]; thenecho "${markfile} is not exist, create it."touch ${markfile}ficase $1 inl)cat -n ${markfile};;d)# deleteif [ "x" == "x$2" ]; thenecho "need a sequence number"return 1fiif [ $2 -lt 0 ] || [ $2 -gt $(wc -l < ${markfile}) ]; thenecho "invalid sequence number"return 1fised -i "$2d" ${markfile};;a)# add / appendlocal new_mark=$(pwd)local tmp_grep=$(grep ${new_mark} ${markfile})if [ "x" == "x${tmp_grep}" ]; thenecho "mark current dirname: ${new_mark}"echo ${new_mark} >> ${markfile}elseecho "Warning! Duplicate mark"fi;;c)# cdif [ "x" == "x$2" ]; thenecho "need a sequence number"return 1fiif [ $2 -lt 0 ] || [ $2 -gt $(wc -l < ${markfile}) ]; thenecho "invalid sequence number"return 1filocal target_dir=$(sed -n "$2p" ${markfile})if [ ! -d ${target_dir} ]; thenecho "${target_dir} not found"return 1ficd ${target_dir};;*)echo "Options: l(ist), a(dd), d(elete), c(hange)"esac
}
- 在
~/.bashrc
中追加以下内容以引入上一步的文件
if [ -f ~/.bash_custom_functions ]; then. ~/.bash_custom_functions
fi
- source生效,或者重建shell会话
source ~/.bashrc