如何用 Shell 脚本编写递归程序

开发者在线 Builder.com.cn 更新时间:2007-09-15作者:ChinaITLab 来源:ChinaITLab

本文关键词: SHELL 脚本 递归程序

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 "$/$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 $/$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 $/$d

done

}

# main

TREE=`basename `

if [ "" ]

then DIR=""

else DIR="."

fi

if cd $DIR

then $TREE *

else echo ": Directory read fail."

fi

# (End)

查看本文来源

用户评论

  • 用户名
  • 评论内容