LinuxSir.cn,穿越时空的Linuxsir!

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

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

[复制链接]
 楼主| 发表于 2003-5-9 11:16:08 | 显示全部楼层
plan9客气了,指点谈不上,大家讨论一下吧:p
我读了你的脚本,我们的解题思路完全不同。
我的改造方案是:增加一个目录状态数组(每个组员可能有三种赋值1、2、3分别对应“| ”、“|-”、“ ”),由它来确定每行的目录树如何绘制,本行数组值在继承上一行数年组值的基础上,根据“本行文件是否为本层的最后一个目录”来确定是否对上行数组值的末位值进行修改。然后改造print_dash(),让它成根据数组数组值进行绘图。
这个方案的缺点就是改动比较大,但基本思路和你原脚本是一致的。
发表于 2003-5-12 16:43:58 | 显示全部楼层
最初由 LYOO 发表
plan9客气了,指点谈不上,大家讨论一下吧:p
我读了你的脚本,我们的解题思路完全不同。
我的改造方案是:增加一个目录状态数组(每个组员可能有三种赋值1、2、3分别对应“| ”、“|-”、“ ”),由它来确定每行的目录树如何绘制,本行数组值在继承上一行数年组值的基础上,根据“本行文件是否为本层的最后一个目录”来确定是否对上行数组值的末位值进行修改。然后改造print_dash(),让它成根据数组数组值进行绘图。
这个方案的缺点就是改动比较大,但基本思路和你原脚本是一致的。

我的想法与你类似,而且我已经这样做了,不过我没有做数据组,这种方法实现起来速度非常慢,因为每进一个目录都要运行一次ls来找最后一个文件。
而且我现在还有一个问题没有解决,你看一下我的脚本的输出就知道了。

  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. TRUE=0
  33. FALSE=1
  34. LAYERS=0
  35. FILECOUNT=0
  36. DIRCOUNT=0

  37. color=${TRUE}
  38. if [ "${1#-n}" != "${1}" ]; then
  39.         shift
  40.         color=${FALSE}
  41. fi

  42. ROOT=${1:-.}

  43. if [ ${COLOR} -eq ${FALSE} ]; then
  44.         DIR=""
  45.         EXE=""
  46.         DEV=""
  47.         LNK=""
  48.         ZIP=""
  49.         SOCK=""
  50.         NULL=""
  51. fi

  52. # print_dash
  53. # ==========
  54. # Print the structure lines
  55. # $1=LAYERS $2=FILENAME $3=CURRENT DIR
  56. print_dash()
  57. {
  58.         local i=0
  59.         local num=$1
  60.         local f=${2}
  61.         local lastf
  62.         local dir
  63.         local pdir
  64.         local plastf

  65.         dir=${3##*/}
  66.         pdir=${3%/*}
  67.         pdir=${pdir:-/}
  68.         plastf=`ls -1 $pdir 2> /dev/null | tail -1`
  69.        
  70.         while [ $i -lt $num ]; do
  71.                 echo -n "|   "

  72.                 i=`expr $i + 1`
  73.         done


  74.         # If the last file then print `--
  75.         lastf=`ls -1 $3 2> /dev/null | tail -1`
  76.         if [ "$f" = "$lastf" ]; then
  77.                 echo -n "\`-- "
  78.         else
  79.                 echo -n "|-- "
  80.         fi
  81. }

  82. # ispkg
  83. # =====
  84. # Test if the file is a package like:
  85. # .gz .tar .tgz .tar.gz .zip .rar .rpm
  86. # and etc.
  87. #
  88. ispkg()
  89. {
  90.         local f=$1
  91.         local i       

  92.         # Package extension list, you can add your coustom
  93.         # extensions in it.
  94.         #
  95.         local pkg_ext=".gz .tar .tgz .tar.gz .zip .rar .rpm"

  96.         # if the file's suffix contain any package extension
  97.         # then cut it.

  98.         for i in $pkg_ext; do
  99.                 f=${f%$i}
  100.         done

  101.         if [ "$f" != "$1" ]; then
  102.                 return $TRUE
  103.         else
  104.                 return $FALSE
  105.         fi
  106. }

  107. # mktree
  108. # ======
  109. # The main function, that print the
  110. # dictionary structure in dos's tree
  111. # command style. It's runs in nesting.
  112. #
  113. mktree()
  114. {
  115.         local f
  116.        
  117.         for f in `$LSPROG -1 $1 2> /dev/null`; do
  118.                 f=${f%/}
  119.                 f=${f##*/}

  120.                 print_dash $LAYERS $f $1
  121.        
  122.                 # If dictionary then print it and enter
  123.                 # the nesting block.
  124.                 if [ -d $1/$f ]; then
  125.                         echo -e "${DIR}$f${NULL}"
  126.                         DIRCOUNT=`expr $DIRCOUNT + 1`

  127.                         LAYERS=`expr $LAYERS + 1`
  128.                         mktree $1/$f
  129.                 else
  130.                         # file is a symbol link
  131.                         if [ -L $1/$f ]; then
  132.                                 echo -e "${LNK}$f${NULL}"
  133.                         # file is executable
  134.                         elif [ -x $1/$f ]; then
  135.                                 echo -e "${EXE}$f${NULL}"
  136.                         # file is a device
  137.                         elif [ -c $1/$f -o -b $1/$f ]; then
  138.                                 echo -e "${DEV}$f${NULL}"
  139.                         # file is a socket
  140.                         elif [ -S $1/$f ]; then
  141.                                 echo -e "${SOCK}$f${NULL}"
  142.                         # file is a package
  143.                         elif `ispkg $f`; then
  144.                                 echo -e "${ZIP}$f${NULL}"
  145.                         else       
  146.                                 echo -e "$f"
  147.                         fi
  148.                        
  149.                         FILECOUNT=`expr $FILECOUNT + 1`
  150.                 fi
  151.         done

  152.         LAYERS=`expr $LAYERS - 1`
  153. }

  154. echo $ROOT

  155. mktree $ROOT
  156. echo "$DIRCOUNT directories, $FILECOUNT files"
复制代码


输出:

  1. .
  2. |-- out
  3. |-- public_html
  4. |   |-- cgi-bin
  5. |   |   |-- test
  6. |   |   |-- test2
  7. |   |   |-- test23
  8. |   |   |-- test234
  9. |   |   |-- test2345
  10. |   |   |-- test23455
  11. |   |   `-- x
  12. |   |   |   |-- 1
  13. |   |   |   |-- 12
  14. |   |   |   |-- 123
  15. |   |   |   |-- 1234
  16. |   |   |   `-- 12346
  17. |   |-- out.txt
  18. |   |-- tree
  19. |   `-- tree.sh
  20. |-- tree
  21. |-- tree.sh
  22. |-- tree.sh.1
  23. `-- tree2
  24. 3 directories, 19 files
复制代码


这里:

  1. |   |   |-- test23455
  2. |   |   `-- x
  3. |   |   |   |-- 1
  4. |   |   |   |-- 12
  5. |   |   |   |-- 123
  6. |   |   |   |-- 1234
  7. |   |   |   `-- 12346
  8. |   |-- out.txt
  9. |   |-- tree
  10. |   `-- tree.sh
复制代码

实际应该为:

  1. |   |   |-- test23455
  2. |   |   `-- x
  3. |   |       |-- 1
  4. |   |       |-- 12
  5. |   |       |-- 123
  6. |   |       |-- 1234
  7. |   |       `-- 12346
  8. |   |-- out.txt
  9. |   |-- tree
  10. |   `-- tree.sh
复制代码

那就实现了我想像的功能了。
 楼主| 发表于 2003-5-14 21:58:50 | 显示全部楼层
呵呵,你现在遇到的问题,也是我写该脚本时遇过的最大问题。不过既然你已经确定本层最后一个文件是否为目录,这个问题基本上已解决了(跟据这个判断对下层做相关处理就过关了)。;)
发表于 2003-5-19 19:52:32 | 显示全部楼层

我也来一个

#!/bin/bash
showdir()
{
        tab=$tab$singletab
        line=${tab%"$singletab"}"|----"
       
        for subdir in $@
        do
                if [ -d $(pwd)/$subdir ]
                then
                        echo -e "$line$subdir/"
                        cd $subdir
                        showdir $(ls)
                else
                        echo -e "$line$subdir"
                fi
        done
       
        cd ..
        tab=${tab%"$singletab"}
        line=${tab%"$singletab"}"|----"
}

singletab="|\t"

if [ -e $1 ]
then
        for dir in $@
        do
                echo $dir
                cd $dir
                showdir $(ls)
        done
else
        echo "No such directory"
fi
发表于 2003-5-30 09:24:19 | 显示全部楼层

有个问题,,文件名中有空格的不能正确显示

这怎么办之
 楼主| 发表于 2003-5-30 22:16:08 | 显示全部楼层
great
好问题,思考中............
发表于 2003-6-4 16:37:05 | 显示全部楼层
我修改了一下,能识别空格了

LSPROG -1 "$1" | while read f
do
...
done

但是碰到一个奇怪的问题,在while对FILECOUNT和DIRCOUNT作的改动,并没有改变全局变量FILECOUNT和DIRCOUNT。


  1. #!/bin/bash
  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. TRUE=0
  33. FALSE=1
  34. LAYERS=0
  35. FILECOUNT=0
  36. DIRCOUNT=0

  37. color=${TRUE}
  38. if [ "${1#-n}" != "${1}" ]; then
  39.         shift
  40.         color=${FALSE}
  41. fi

  42. ROOT=${1:-.}

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

  57.                 i=`expr $i + 1`
  58.         done

  59.         echo -n "|-- "
  60. }

  61. # ispkg
  62. # =====
  63. # Test if the file is a package like:
  64. # .gz .tar .tgz .tar.gz .zip .rar .rpm
  65. # and etc.
  66. #
  67. ispkg()
  68. {
  69.         local f=$1
  70.         local i       

  71.         # Package extension list, you can add your coustom
  72.         # extensions in it.
  73.         #
  74.         local pkg__ext=".gz .tar .tgz .tar.gz .zip .rar .rpm"

  75.         # if the file's suffix contain any package extension
  76.         # then cut it.

  77.         for i in $pkg__ext; do
  78.                 f=${f%$i}
  79.         done

  80.         if [ "$f" != "$1" ]; then
  81.                 return $TRUE
  82.         else
  83.                 return $FALSE
  84.         fi
  85. }

  86. # mktree
  87. # ======
  88. # The main function, that print the
  89. # dictionary structure in dos's tree
  90. # command style. It's runs in nesting.
  91. #
  92. mktree()
  93. {
  94.         #echo ***START $DIRCOUNT
  95.         local f
  96.         $LSPROG -1 "$1" | {
  97.         while read f; do
  98.                 #echo ***Start $DIRCOUNT
  99.                 f=${f%/}
  100.                 f=${f##*/}
  101.        
  102.                 # If dictionary then print it and enter
  103.                 # the nesting block.
  104.                 if [ -d "$1/$f" ]; then
  105.                         print_dash $LAYERS
  106.                         if [ ${COLOR} -eq ${TRUE} ]; then
  107.                                 echo -e "${DIR}$f${NULL}"
  108.                         else
  109.                                 echo -e "$f"
  110.                         fi
  111.                         DIRCOUNT=`expr $DIRCOUNT + 1`
  112.                         LAYERS=`expr $LAYERS + 1`
  113.                         mktree "$1/$f"
  114.                 else
  115.                         print_dash $LAYERS
  116.                         if [ ${COLOR} -eq ${TRUE} ]; then
  117.                                  # file is a symbol link
  118.                                 if [ -L "$1/$f" ]; then
  119.                                         echo -e "${LNK}$f${NULL}"
  120.                                 # file is executable
  121.                                 elif [ -x "$1/$f" ]; then
  122.                                         echo -e "${EXE}$f${NULL}"
  123.                                 # file is a device
  124.                                 elif [ -c "$1/$f" -o -b "$1/$f" ]; then
  125.                                         echo -e "${DEV}$f${NULL}"
  126.                                 # file is a socket
  127.                                 elif [ -S "$1/$f" ]; then
  128.                                         echo -e "${SOCK}$f${NULL}"
  129.                                 # file is a package
  130.                                 elif `ispkg $f`; then
  131.                                         echo -e "${ZIP}$f${NULL}"
  132.                                 else       
  133.                                         echo -e "$f"
  134.                                 fi
  135.                         else
  136.                                 echo -e "$f"
  137.                         fi
  138.                        
  139.                         FILECOUNT=`expr $FILECOUNT + 1`
  140.                 fi
  141.         done
  142.         }
  143.         LAYERS=`expr $LAYERS - 1`
  144.         #echo ***End $DIRCOUNT
  145. }

  146. echo $ROOT
  147. mktree $ROOT
  148. echo "\`"
  149. echo "$DIRCOUNT directories, $FILECOUNT files"
复制代码
发表于 2003-6-4 22:00:25 | 显示全部楼层
改了LYOO的,也能处理有空格的文件和目录了,就是不能统计文件和目录数。


  1. #!/bin/bash

  2. # Name: dirtree.sh
  3. # Dispaly your filesystem like a tree
  4. # You can use "-color" toggle the color
  5. # I wrote it in Woody
  6. # Special Thanks for plan9

  7. #########################
  8. # COLOR DEFINE
  9. # Thanks for plan9's code
  10. #########################

  11. DIR="\033[01;34m"
  12. EXE="\033[01;32m"
  13. DEV="\033[01;33m"
  14. LNK="\033[01;36m"
  15. ZIP="\033[01;31m"
  16. SOCK="\033[01;35m"
  17. NULL="\033[00m"



  18. #########################
  19. # Check File extension
  20. # Thanks for plan9's code
  21. #########################

  22. ispkg()
  23. {
  24.         local f=$1
  25.         local i       

  26.         # Package extension list, you can add your coustom
  27.         # extensions in it.
  28.         #
  29.         local pkg__ext=".gz .tar .tgz .tar.gz .zip .rar .rpm"
  30.         #local pic__ext=".png .jpg .jpeg .gif .tif"

  31.         # if the file's suffix contain any package extension
  32.         # then cut it.

  33.         for i in $pkg__ext; do
  34.                 f=${f%$i}
  35.         done

  36.         if [ "$f" != "$1" ]; then
  37.                 return $TRUE
  38.         else
  39.                 return $FALSE
  40.         fi
  41. }



  42. #########################
  43. # Select Color for files
  44. # Thanks for plan9's code
  45. #########################

  46. dircolor ()
  47. {
  48.         if [ $1 = "yes" ]; then
  49.        
  50.                 # file is directory
  51.                 if [ -d "$thisfile" ]; then
  52.                         colorfile=${DIR}$file/${NULL}
  53.                         echo -e $line$colorfile
  54.                 # file is link
  55.                 elif [ -L "$thisfile" ]; then
  56.                         colorfile=${LNK}$file${NULL}
  57.                         echo -e $line$colorfile
  58.                 # file is executable
  59.                 elif [ -x "$thisfile" ]; then
  60.                         colorfile=${EXE}$file${NULL}
  61.                         echo -e $line$colorfile
  62.                 # file is a device
  63.                 elif [ -c "$thisfile" -o -b $file ]; then
  64.                         colorfile=${DEV}$file${NULL}
  65.                         echo -e $line$colorfile
  66.                 # file is a socket
  67.                 elif [ -S "$thisfile" ]; then
  68.                         colorfile=${SOCK}$file${NULL}
  69.                         echo -e $line$colorfile
  70.                 # file is a package
  71.                 elif $(ispkg "$thisfile"); then
  72.                         colorfile=${ZIP}$file${NULL}
  73.                         echo -e $line$colorfile
  74.                 else       
  75.                         colorfile=$file
  76.                         echo -e $line$colorfile
  77.                 fi
  78.        
  79.         else
  80.                 echo -e $line$file
  81.         fi
  82. }



  83. ################
  84. # Directory Tree
  85. ################

  86. redir ()
  87. {
  88.        
  89.         tab=$tab$singletab
  90.         line=${tab%"$singletab"}"|-------"
  91.         local count=$1
  92.        
  93.         #for file in "$@"; do   //be changed
  94.         while read file; do
  95.                 thisfile=${thisfile:-$PWD}/$file
  96.                
  97.                 if [ -d "$thisfile" ]; then
  98.                         dircolor $colorswitch
  99.                         if [ $count -eq 1 ]; then
  100.                                 tab=${tab%"$singletab"}"\t"
  101.                                 #redir $(ls $thisfile)  //be changed
  102.                                 ls -1 "$thisfile" | redir $(ls -1 | wc -l)
  103.                         else
  104.                                 #redir $(ls $thisfile)  //be changed
  105.                                 ls -1 "$thisfile" | redir $(ls -1 | wc -l)
  106.                         fi
  107.                        
  108.                 else
  109.                         dircolor $colorswitch
  110.                 fi
  111.                
  112.                 thisfile=${thisfile%/*}
  113.                 let count=count-1       
  114.         done
  115.        
  116.         tab=${tab%"\t"}
  117.         tab=${tab%"|"}
  118.         line=${tab%"$singletab"}"|-------"
  119. }



  120. #################
  121. # THE MAIN SCRIPT
  122. #################

  123. singletab="|\t"
  124. colorswitch="no"

  125. if [ -n "$1" ]; then
  126.         # dose user want color ?
  127.         if [ $1 = "-color" ]; then
  128.                 colorswitch="yes"
  129.                 shift
  130.         fi
  131.         # make dirtrees
  132.         if [ -e $1 ]; then
  133.        
  134.                 for file in $1; do
  135.                         echo $file
  136.                         echo '|'
  137.                         if [ -d "$file" ]; then
  138.                                 cd $file
  139.                                 #redir $(ls)  //be changed
  140.                                 ls -1 | redir $(ls -1 | wc -l)
  141.                                 cd ..
  142.                         fi
  143.                 done
  144.        
  145.         else
  146.                 echo "the Directory doesn't exist"
  147.         fi

  148. else
  149.         echo "USEAGE: $0 [-color] Directory"
  150. fi
复制代码
发表于 2003-6-6 13:30:32 | 显示全部楼层
最初由 noword 发表
我修改了一下,能识别空格了

LSPROG -1 "$1" | while read f
do
...
done

但是碰到一个奇怪的问题,在while对FILECOUNT和DIRCOUNT作的改动,并没有改变全局变量FILECOUNT和DIRCOUNT。


这是因为你使用管道造成shell新开一个子进程来运行while...,所以while里面的变量就变成了局部的,你可以使用export将其导出到父进程中。

同时,我做了一点修改,现在可以显示空格文件了,感谢guangold发现这个问题。

下载:http://www.arbornet.org/~matthew/cgi-bin/code_bash.cgi

  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. TRUE=0
  33. FALSE=1
  34. LAYERS=0
  35. FILECOUNT=0
  36. DIRCOUNT=0

  37. color=${TRUE}
  38. if [ "${1#-n}" != "${1}" ]; then
  39.         shift
  40.         color=${FALSE}
  41. fi

  42. ROOT=${1:-.}

  43. if [ ${COLOR} -eq ${FALSE} ]; then
  44.         DIR=""
  45.         EXE=""
  46.         DEV=""
  47.         LNK=""
  48.         ZIP=""
  49.         SOCK=""
  50.         NULL=""
  51. fi

  52. # print_dash
  53. # ==========
  54. # Print the structure lines
  55. #
  56. print_dash()
  57. {
  58.         local i=0
  59.         local num=$1
  60.        
  61.         while [ $i -lt $num ]; do
  62.                 echo -n "|   "

  63.                 i=`expr $i + 1`
  64.         done

  65.         echo -n "|-- "
  66. }

  67. # ispkg
  68. # =====
  69. # Test if the file is a package like:
  70. # .gz .tar .tgz .tar.gz .zip .rar .rpm
  71. # and etc.
  72. #
  73. ispkg()
  74. {
  75.         local f=$1
  76.         local i       

  77.         # Package extension list, you can add your coustom
  78.         # extensions in it.
  79.         #
  80.         local pkg__ext=".gz .tar .tgz .tar.gz .zip .rar .rpm"

  81.         # if the file's suffix contain any package extension
  82.         # then cut it.

  83.         for i in $pkg__ext; do
  84.                 f=${f%$i}
  85.         done

  86.         if [ "$f" != "$1" ]; then
  87.                 return $TRUE
  88.         else
  89.                 return $FALSE
  90.         fi
  91. }

  92. # mktree
  93. # ======
  94. # The main function, that print the
  95. # dictionary structure in dos's tree
  96. # command style. It's runs in nesting.
  97. #
  98. mktree()
  99. {
  100.         local f
  101.         local OLDIFS=$IFS

  102.         IFS='
  103.         '
  104.         for f in `$LSPROG -1 $1`; do
  105.                 f=${f%/}
  106.                 f=${f##*/}
  107.        
  108.                 # If dictionary then print it and enter
  109.                 # the nesting block.
  110.                 if [ -d $1/$f ]; then
  111.                         print_dash $LAYERS
  112.                         echo -e "${DIR}$f${NULL}"
  113.                         DIRCOUNT=`expr $DIRCOUNT + 1`

  114.                         LAYERS=`expr $LAYERS + 1`
  115.                         mktree $1/$f
  116.                 else
  117.                         print_dash $LAYERS
  118.                         # file is a symbol link
  119.                         if [ -L $1/$f ]; then
  120.                                 echo -e "${LNK}$f${NULL}"
  121.                         # file is executable
  122.                         elif [ -x $1/$f ]; then
  123.                                 echo -e "${EXE}$f${NULL}"
  124.                         # file is a device
  125.                         elif [ -c $1/$f -o -b $1/$f ]; then
  126.                                 echo -e "${DEV}$f${NULL}"
  127.                         # file is a socket
  128.                         elif [ -S $1/$f ]; then
  129.                                 echo -e "${SOCK}$f${NULL}"
  130.                         # file is a package
  131.                         elif `ispkg $f`; then
  132.                                 echo -e "${ZIP}$f${NULL}"
  133.                         else       
  134.                                 echo -e "$f"
  135.                         fi
  136.                        
  137.                         FILECOUNT=`expr $FILECOUNT + 1`
  138.                 fi
  139.         done

  140.         LAYERS=`expr $LAYERS - 1`

  141.         export IFS=$OLDIFS
  142. }

  143. echo $ROOT
  144. mktree $ROOT
  145. echo "\`"
  146. echo "$DIRCOUNT directories, $FILECOUNT files"
复制代码

显示效果:

  1. .
  2. |-- hello world
  3. |-- out
  4. |-- [color=#0000ff]public_html[/color]
  5. |-- |-- index.html
  6. |-- [color=#00ff00]tree.sh[/color]
  7. `
  8. 1 directories, 4 files
复制代码
发表于 2003-6-6 14:05:37 | 显示全部楼层
一开始我也想修改IFS的,不过IFS="\n"不起作用,原来用
IFS='
'
就可以了。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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