》说明:
- 使用时, 直接source 保存后的文件, 然后敲入
qj
命令即可- 原理: 将指定的目录的软链接统一保存到目录下(
$HOME/.qj
),然后在此目录下进行一系列的操作
#!/usr/bin/env bash
qj() {
_addQJT() {
local vREF_DIR= vNEW_TAG=
case $# in
0) vREF_DIR=$PWD && vNEW_TAG=${vREF_DIR##*/} ;;
2) vREF_DIR="$(realpath $1)" && vNEW_TAG="${2}" ;;
1)
[ -d $1 ] && {
vREF_DIR=$(realpath $1) && vNEW_TAG=${vREF_DIR##*/}
} || {
vREF_DIR=$PWD && vNEW_TAG="${1}"
}
;;
*) echo -e "[\e[31;5;1mERROR\e[0m] incorrect usage, $vHLP -a [NEW_TAG] [REF_DIR]" && return ;;
esac
[ ! -d $vREF_DIR ] && echo -e "[\e[31;5;1mERROR\e[0m] Directory '${vREF_DIR}' not exists, skip!!!" && return
: vNEW_TAG=${vNEW_TAG##*.[^a-zA-Z0-9]}
ln -sf $vREF_DIR $sTDIR/${vNEW_TAG,,}
}
_listQJT() {
local vTR1=$(mktemp) vTR2=$(mktemp)
trap 'rm -f $vTR1 $vTR2' RETURN INT HUP EXIT
command ls -l $sTDIR | awk '/^l/' | awk "/$1/" | awk '{print NR,$(NF-2),$NF}' 2>/dev/null >$vTR1
[ $(wc -l <$vTR1) -eq 0 ] && return
while read vNO vTAG vDST; do
[ ! -d $vDST ] && rm -f $sTDIR/$vTAG || echo -e "$vTAG=$vDST" >>$vTR2
done <$vTR1
echo && echo "Now, has tags:"
awk '{print NR".",$0}' $vTR2 | column -t | sed 's,=, --> ,g'
}
_delQJT() {
__delAll() { for vID in $@; do rm -f ${vID}; done ; unset -v vID; }
trap 'unset -f __delAll' RETURN INT EXIT HUP
case $1 in
"") return ;;
all) __delAll $(command ls -d1 $sTDIR/* 2>/dev/null) && return ;;
esac
local vTAGS=($(command ls -d1 $sTDIR/*${1}* 2>/dev/null))
case ${#vTAGS[@]} in
0) return ;;
1) rm -f ${vTAGS} && return ;;
esac
local t=
select t in all ${vTAGS[@]}; do
case $t in
all) __delAll "${vTAGS[@]}" ;;
*) [ "X$t" != "X" ] && rm -f $t ;;
esac
break
done
}
_intoQJT() {
local vTAGS=($(command ls -ad1 $sTDIR/*${1}* 2>/dev/null))
case ${#vTAGS[@]} in
0)
[ -d $1 ] || mkdir -p $1
cd $1 && return
;;
1) cd -P ${tags} && return ;;
esac
local id= && echo
PS3="Which tag do you want to jump (1 ~ ${#vTAGS[@]}):>"
select id in ${vTAGS[@]}; do
case $id in
*) [[ "X$id" != "X" && -L $id ]] && cd -P ${id} 2>/dev/null ;;
esac
break
done
}
trap 'unset -f _{list,del,add,into}QJT' RETURN INT EXIT HUP
local vHLP=$FUNCNAME sTDIR=$HOME/.qj
[ -d $sTDIR ] || mkdir -p $sTDIR
case $1 in
-h | --help | help)
: echo && echo "$vHLP -- quickly jump between recently use directories" && echo
echo "Usage:" && echo
echo " $vHLP [-l] # List all valid tags for quickly jump"
echo " $vHLP <EXIST_TAG> # Jump into EXIST_TAG"
echo " $vHLP -a [REF_DIR] [NEW_TAG] # Add new tag"
echo " $vHLP -d <EXIST_TAG | all> # Delete an or all exist tags"
echo && return
;;
"") _listQJT && return ;;
esac
local vACT=$1 && shift
case $vACT in
-l | --list) _listQJT $@ ;;
-a | --add) _addQJT $@ ;;
-d | --delete) _delQJT $@ ;;
*) [ "X${act:0:1}" != "X-" ] && _intoQJT $vACT ;;
esac
}
case $0 in
*.sh) qj $@ ;;
esac