您所在的位置:开发 > 语言&工具 > 如何用 Shell 脚本编写递归程序

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

2005-09-27 17:12 ChinaITLab 我要评论(0) 字号:T | T
一键收藏,随时查看,分享好友!

UNIX Shell 脚本类似 DOS 的批处理命令,但比较起来 UNIX Shell 的功能更强大,在某些方面,Shell 甚至超过了一些高级语言。 本文用 Shell 脚本演示了如何用 Shell 脚本编写递归程序。

AD:

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)


网友评论TOP5

查看所有评论(

提交评论

  1. 编程排行榜:图形编程的LOGO语言
  2. 什么是响应式Web设计?

热点专题

更多>>

读书

网管第一课——网络组建与管理
本书针对初级网管朋友所需掌握的网络组建和网络管理技能,以示例方式编写而成,其主要特点就是实用性和可操作性非常强。 全书共

51CTO旗下网站

领先的IT技术网站 51CTO 领先的中文存储媒体 WatchStor 中国首个CIO网站 CIOage 中国首家数字医疗网站 HC3i 移动互联网生活门户 灵客风LinkPhone