[ 永遠的UNIX::UNIX技術資料的寶庫 ]   GB | BIG5

首頁 > 編程技術 > Shell > 正文
Shell 遞歸程序設計-目錄列表
本文出自: http://ehnt.net/clansoft/gb/ (2001-06-18 11:00:00)
UNIX Shell 腳本類似 DOS 的批處理命令,但比較起來 UNIX Shell 的功能更強大,在某些方面,Shell 甚至超過了一些高級語言。
下邊的 Shell 腳本演示了如何用 Shell 腳本編寫遞歸程序。

運行前先執行下述準備命令:
ln tree.sh /usr/bin/tree
ln tree.sh /usr/bin/wtree
ln tree.sh /usr/bin/dtree
rm tree.sh


# tree.sh

# Depth first Directory list
dtree() { 
PWD=`pwd|sed 's/\/\$//`
for d in $*
do
echo "${PWD}/$d"
[ -d "$d" -a -x "$d" ] && {
        cd "$d"
        dtree *
        cd ..
        PWD=`pwd|sed 's/\/\$//` # restore PWD
        }
done


# Depth first Directory list
wtree() {        
PWD=`pwd|sed 's/\/\$//`
for d in $*
do
        echo ${PWD}/$d        
done
for d in $*
do
        [ -d "$d" -a -x "$d" ] && {
        cd $d
        wtree *
        cd ..
        }
done
}

# Directory list
tree() {        
PWD=`pwd|sed 's/\/\$//`
for d in $*
do
        echo ${PWD}/$d        
done
}

# main
TREE=`basename $0`
if [ "$1" ]
then DIR="$1"
else DIR="."
fi
if cd $DIR
then $TREE *
else echo "$0: Directory $1 read fail."
fi

# (End)

(http://www.fanqiang.com)
    進入【UNIX論壇

相關文章
 

★  樊強制作 歡迎分享  ★