前言:
Linux中,有个维护内核代码的同事嫌一层层cd太麻烦,比如要到某个include目录,要cd ./xx/xx/xx/xx/xx/xx/incldue,于是帮助写了个脚本函数可以直接跳到子目录。
函数
function cdx() {
local szroot=.
local sztarg=""
if [ $# -gt 1 ]; then
if [ -d $1 ]; then szroot=$1; sztarg=$2; else szroot=$2; sztarg=$1; fi
elif [ $# -gt 0 ]; then
sztarg=$1;
fi
if [ "x$sztarg" = "x" ]; then
echo "Usage: zz root-directory target-directory"
else
sztmp=$(echo $sztarg | awk '{s=$0; do{match(s,/\//); if(RLENGTH>0){s=substr(s,RSTART+1);}}while(RLENGTH>0); printf(s); }')
slist=$(find $szroot -type d -name "*$sztmp" | grep "$sztarg$");
total=0;
sztmp="";
for s in $slist;
do
sztmp=$s; total=$(expr $total "+" 1);
done
if [ $total -gt 1 ]; then
for s in $sl