您所在的位置: 首页 > 开发 > 语言&工具 >

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

http://developer.51cto.com  2005-09-27 17:12    ChinaITLab  我要评论(0)
  • 摘要:UNIX Shell 脚本类似 DOS 的批处理命令,但比较起来 UNIX Shell 的功能更强大,在某些方面,Shell 甚至超过了一些高级语言。 本文用 Shell 脚本演示了如何用 Shell 脚本编写递归程序。
  • 标签:shell  s

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)


Scala编程语言
华山论剑——五款主流服务器OS评测
SUSE——低调的潜伏
SQL Server 2008深度应用
ASP.NET视频教程
 
 验证码: (点击刷新验证码)   匿名发表
  • 亮剑.NET. 图解C#开发实战

  • 作者:李新峰 付志涛 缪勇
  • 本书采用全新的图解思路,分3篇介绍使用微软C#语言开发实际应用程序的基本知识。第1篇包括10章,介绍了C#语言的基础知识,主要..
Copyright©2005-2009 51CTO.COM 版权所有