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)
| 论坛热门帖子: | [lch203] 写得蛮好的linux学习笔记(10-21) [黑马制造] 学习java的30个目标(10-19) [笑傲股林] 做测试半年了,有点迷茫,应该再学些什么提高自己的测试水平和测试能力呢?(10-19) [udp8589] 大家用google的来吱一声? 用百度的~~也来报道下?(10-18) [沂偌掳兆] 本人总结的一些认为C++比较经典的书籍,希望对大家有用(10-18) |
| TAG标签: | 程序 编写 脚本 如何 PWD tree.sh cd Shell // done pwd |
注册
个人空间
