[ 永远的UNIX::UNIX技术资料的宝库 ]

首页 > 编程技术 > Shell > 正文
 

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

作者:ChinaITLab 来源:ChinaITLab (2007-02-06 15:27:31)

  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用户登陆shell控制文件 2007-02-06 15:14:11
TCSH shell变量和特征配置 2006-06-27 17:06:05
Shell程序设计的流程控制 2006-06-15 12:12:54
在Linux中批量建立用户的shell 2006-06-05 17:46:18
Unix系列shell程序编写(下) 2006-05-25 11:29:45
 

★  感谢所有的作者为我们学习技术知识提供了一条捷径  ★
www.fanqiang.com