LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
楼主: LYOO

写了一个显示目录树的脚本,可输出不对

[复制链接]
 楼主| 发表于 2003-4-28 20:30:14 | 显示全部楼层
1.if条件判断语句的结构为:
  1. if statement
  2. then
  3.         ......
  4. fi
复制代码

shell解释器判断语句结束的标志为;和换行符,所以如果两条语句在两一行时,应该用;分开。将if startement和then写成一行为了让代码看上去更简洁。

2.上述脚本在我的系统上执行正常,现附上脚本文件,如果该脚本在你的系统上执行出错,请告知你执行该脚本时所指定的目录是什么结构,我会进一步改正。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
发表于 2003-4-28 20:39:29 | 显示全部楼层
哦,这我就明白了。源码已下,我回去再试试。
发表于 2003-4-30 09:29:04 | 显示全部楼层
呵呵,已经搞定了呀,昨天我还特地回去写了一个,基本和tree命令的输出完全一样(除了目录未的工作不同),文件带色彩输出,文件、目录数统计,贴上来给大家做个参考:

源代码:http://www.arbornet.org/~matthew/code/bash/tree.sh

tree.sh
------------------------------------------------------------

  1. #!/bin/sh
  2. #
  3. # tree.sh
  4. # A tool that display the dictionary structure in dos's
  5. # tree command style.
  6. # By Matthew <matthew@linuxforum.net>
  7. #
  8. #    __@
  9. #  _ \<_
  10. # (_)/(_)
  11. # Apr 29 2003
  12. #
  13. # Tested on slackware, openbsd, netbsd, freebsd.
  14. #
  15. # Just for fun.
  16. #

  17. # The name of the ls program, please use
  18. # the absolute path, otherwise, there
  19. # may be get some strange errors.
  20. #
  21. LSPROG="/bin/ls"

  22. # COLOR DEFINE
  23. # ============
  24. #
  25. DIR="\033[01;34m"
  26. EXE="\033[01;32m"
  27. DEV="\033[01;33m"
  28. LNK="\033[01;36m"
  29. ZIP="\033[01;31m"
  30. SOCK="\033[01;35m"
  31. NULL="\033[00m"

  32. ROOT=${1:-.}
  33. TRUE=0
  34. FALSE=1
  35. LAYERS=0
  36. FILECOUNT=0
  37. DIRCOUNT=0

  38. # print_dash
  39. # ==========
  40. # Print the structure lines
  41. #
  42. print_dash()
  43. {
  44.         local i=0
  45.         local num=$1
  46.        
  47.         while [ $i -lt $num ]; do
  48.                 echo -n "|"
  49.                 for j in 1 2 3; do
  50.                         echo -n " "
  51.                 done

  52.                 i=`expr $i + 1`
  53.         done

  54.         echo -n "|-- "
  55. }

  56. # ispkg
  57. # =====
  58. # Test if the file is a package like:
  59. # .gz .tar .tgz .tar.gz .zip .rar .rpm
  60. # and etc.
  61. #
  62. ispkg()
  63. {
  64.         local f=$1
  65.         local i       

  66.         # Package extension list, you can add your coustom
  67.         # extensions in it.
  68.         #
  69.         local pkg__ext=".gz .tar .tgz .tar.gz .zip .rar .rpm"

  70.         # if the file's suffix contain any package extension
  71.         # then cut it.

  72.         for i in $pkg__ext; do
  73.                 f=${f%$i}
  74.         done

  75.         if [ "$f" != "$1" ]; then
  76.                 return $TRUE
  77.         else
  78.                 return $FALSE
  79.         fi
  80. }

  81. # mktree
  82. # ======
  83. # The main function, that print the
  84. # dictionary structure in dos's tree
  85. # command style. It's runs in nesting.
  86. #
  87. mktree()
  88. {
  89.         local f
  90.         for f in `$LSPROG -1 $1 2> /dev/null`; do
  91.                 f=${f%/}
  92.                 f=${f##*/}
  93.        
  94.                 # If dictionary then print it and enter
  95.                 # the nesting block.
  96.                 if [ -d $1/$f ]; then
  97.                         print_dash $LAYERS
  98.                         echo -e "${DIR}$f${NULL}"
  99.                         DIRCOUNT=`expr $DIRCOUNT + 1`

  100.                         LAYERS=`expr $LAYERS + 1`
  101.                         mktree $1/$f
  102.                 else
  103.                         print_dash $LAYERS
  104.                         # file is a symbol link
  105.                         if [ -L $1/$f ]; then
  106.                                 echo -e "${LNK}$f${NULL}"
  107.                         # file is executable
  108.                         elif [ -x $1/$f ]; then
  109.                                 echo -e "${EXE}$f${NULL}"
  110.                         # file is a device
  111.                         elif [ -c $1/$f -o -b $1/$f ]; then
  112.                                 echo -e "${DEV}$f${NULL}"
  113.                         # file is a socket
  114.                         elif [ -S $1/$f ]; then
  115.                                 echo -e "${SOCK}$f${NULL}"
  116.                         # file is a package
  117.                         elif `ispkg $f`; then
  118.                                 echo -e "${ZIP}$f${NULL}"
  119.                         else       
  120.                                 echo -e "$f"
  121.                         fi
  122.                        
  123.                         FILECOUNT=`expr $FILECOUNT + 1`
  124.                 fi
  125.         done

  126.         LAYERS=`expr $LAYERS - 1`
  127. }

  128. echo $ROOT
  129. mktree $ROOT
  130. echo "\`"
  131. echo "$DIRCOUNT directories, $FILECOUNT files"
复制代码


显示效果:

  1. /home/matthew/
  2. |-- [color=#0000ff]src[/color]
  3. |   |-- [color=#0000ff]asm[/color]
  4. |   |   |-- hello.s
  5. |   |   |-- shell.s
  6. |   |   |-- [color=#00ff00]hello[/color]
  7. |   |   |-- [color=#00ff00]shell[/color]
  8. |   |-- [color=#0000ff]shell[/color]
  9. |   |   |-- [color=#00ff00]tree.sh[/color]
  10. |-- [color=#ff0000]xfce.tar.gz[/color]
  11. |-- mbox
  12. `
  13. 3 directories, 7 files
复制代码
发表于 2003-4-30 11:55:59 | 显示全部楼层
恭喜~~...佩服~...
 楼主| 发表于 2003-4-30 18:41:52 | 显示全部楼层
呵呵,plan9兄的脚本功能比我的强许多,嗯,我给我的脚本升升级。不过我觉得目录树中处于末端的目录做特殊处理显示起来更好。
发表于 2003-5-3 20:55:44 | 显示全部楼层
为什么用plan9兄的方法输出重定向后为乱码一般??
 楼主| 发表于 2003-5-4 10:18:28 | 显示全部楼层
那不是乱码,是文件着色,看看他的脚本你就明白了
发表于 2003-5-4 10:25:29 | 显示全部楼层
这个我也知道撒,问题是颜色后面的目录名就不对了?
 楼主| 发表于 2003-5-4 10:33:43 | 显示全部楼层
我没发现,把出错情形帖出来瞧瞧
发表于 2003-5-4 11:04:50 | 显示全部楼层
注意:我说的是输出重定向后,比如#./tree >/home/freebird/tree.txt
.
|-- bin
|   |-- arch
|   |-- ash
|   |-- ash.static
|   |-- aumix-minimal
|   |-- awk
|   |-- basename
|   |-- bash
|   |-- bash2
|   |-- bsh
|   |-- cat
|   |-- chgrp
|   |-- chmod
|   |-- chown
|   |-- cp
|   |-- cpio
|   |-- csh
|   |-- cut
|   |-- date
|   |-- dd
|   |-- df
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表