2009年1月29日星期四

BASH练习之“目录轮回”

写了一个bash脚本当作练习内容,为bash添加了qu和hui命令.

qu "去"
hui "回"

anders@~#qu .tencent/
/home/anders /home/anders/.tencent/
anders@~/.tencent#qu qq
/home/anders /home/anders/.tencent/ /home/anders/.tencent/qq
anders@~/.tencent/qq#hui
anders@~/.tencent#hui
anders@~#

--------.bashrc-------
# anders bash exercise
declare -a FOLDER_ARRAY
export FOLDER_ARRAY
FOLDER_ARRAY[0]=$(pwd)

qu ()
{
dirname=$1

if [ $# -eq 0 ] || [ ! -d $1 ]; then
echo "usage:qu "
return
else
if [ ${dirname:0:1} != "/" ]; then
dirname=$(pwd)/$dirname
fi
FOLDER_ARRAY[${#FOLDER_ARRAY[@]}]=$dirname
cd $dirname
echo ${FOLDER_ARRAY[@]}
fi
}

hui ()
{
if [ ${#FOLDER_ARRAY[@]} -le 1 ]; then
echo "no back way"
return
else
unset FOLDER_ARRAY[$((${#FOLDER_ARRAY[@]}-1))]
cd ${FOLDER_ARRAY[$((${#FOLDER_ARRAY[@]}-1))]}
fi
}

没有评论: