LinuxSir.cn,穿越时空的Linuxsir!

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

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

[复制链接]
 楼主| 发表于 2003-5-4 11:19:40 | 显示全部楼层
看你的帖子觉得是正常的。每个着色目录名都包含在颜色标签中(如:^[[01;34m文件名^[[00m)。下面是我做的实验,很正常

屏幕输出:
/home/lyoo/GNUstep
|-- Defaults
|   |-- WindowMaker
|   |-- WMGLOBAL
|   |-- WMRootMenu
|   |-- WMState
|   |-- WMWindowAttributes
|   |-- WPrefs
|-- Library
|   |-- Icons
|   |-- WindowMaker
|   |   |-- autostart
|   |   |-- Backgrounds
|   |   |-- IconSets
|   |   |-- Pixmaps
|   |   |-- plmenu
|   |   |-- Sounds
|   |   |-- SoundSets
|   |   |-- Styles
|   |   |-- Themes
`
11 directories, 8 files

重定向输出:
/home/lyoo/GNUstep
|-- ^[[01;34mDefaults^[[00m
|   |-- WindowMaker
|   |-- ^[[01;32mWMGLOBAL^[[00m
|   |-- WMRootMenu
|   |-- WMState
|   |-- WMWindowAttributes
|   |-- WPrefs
|-- ^[[01;34mLibrary^[[00m
|   |-- ^[[01;34mIcons^[[00m
|   |-- ^[[01;34mWindowMaker^[[00m
|   |   |-- ^[[01;32mautostart^[[00m
|   |   |-- ^[[01;34mBackgrounds^[[00m
|   |   |-- ^[[01;34mIconSets^[[00m
|   |   |-- ^[[01;34mPixmaps^[[00m
|   |   |-- plmenu
|   |   |-- ^[[01;34mSounds^[[00m
|   |   |-- ^[[01;34mSoundSets^[[00m
|   |   |-- ^[[01;34mStyles^[[00m
|   |   |-- ^[[01;34mThemes^[[00m
`
11 directories, 8 files
发表于 2003-5-4 12:37:25 | 显示全部楼层
能不能在输出重定向后不要这些颜色选项呀?如果能才叫完美呀!
 楼主| 发表于 2003-5-4 12:49:56 | 显示全部楼层
当然可以,你可以给plan9兄去信,也可以自己动手。我也在改进我的脚本。
发表于 2003-5-6 10:02:19 | 显示全部楼层
更新了一下,在tree.sh后加上参数-n就可以不显示颜色,例如:
linux$ ./tree.sh -n /etc


  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.                 for j in 1 2 3; do
  64.                         echo -n " "
  65.                 done

  66.                 i=`expr $i + 1`
  67.         done

  68.         echo -n "|-- "
  69. }

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

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

  84.         # if the file's suffix contain any package extension
  85.         # then cut it.

  86.         for i in $pkg__ext; do
  87.                 f=${f%$i}
  88.         done

  89.         if [ "$f" != "$1" ]; then
  90.                 return $TRUE
  91.         else
  92.                 return $FALSE
  93.         fi
  94. }

  95. # mktree
  96. # ======
  97. # The main function, that print the
  98. # dictionary structure in dos's tree
  99. # command style. It's runs in nesting.
  100. #
  101. mktree()
  102. {
  103.         local f
  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. }

  142. echo $ROOT
  143. mktree $ROOT
  144. echo "\`"
  145. echo "$DIRCOUNT directories, $FILECOUNT files"
复制代码
发表于 2003-5-6 12:26:56 | 显示全部楼层
呵呵,你搞错了,我说得是输出重定向,你这样是在终端都没颜色了
发表于 2003-5-7 09:20:43 | 显示全部楼层
重定了向当然在屏幕上就不能显示了呀。你要重定向的时候就加上-n参数,这样就不会显示出颜色,而如果是非重定向,就不加-n参数这样就还是彩色的呀。
比如:
重定向: linux$ ./tree.sh -n /etc > out
正常:   linux$ ./tree.sh /etc
 楼主| 发表于 2003-5-7 11:43:35 | 显示全部楼层
报告plan9:
俺申请把你的ispkg()和mktree()的一部分移到自己的脚本中了,实现目录着色。
发表于 2003-5-7 14:59:24 | 显示全部楼层
最初由 LYOO 发表
报告plan9:
俺申请把你的ispkg()和mktree()的一部分移到自己的脚本中了,实现目录着色。

LYOO兄严重了,你直接移就可以了,我贴出来就是大家共享的嘛,等你做好了,Mail一份给我:matthew@linuxforum.net,谢谢!
 楼主| 发表于 2003-5-7 19:42:15 | 显示全部楼层
呵呵,用plan9的代码就是省力,不怎么改就可以显示色彩了。不过计算目录数和文件数的代码不好移,写这个脚本时没考虑到计数,现在还真不好加。
$ ./dirtree.sh /your/dir----------------------默认为不上色显示
$ ./dirtree.sh -color /your/dir------------加上-color选项就可以上色显示了

  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.         tab=$tab$singletab
  89.         line=${tab%"$singletab"}"|-------"
  90.         local count=$#

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



  117. #################
  118. # THE MAIN SCRIPT
  119. #################

  120. singletab="|\t"
  121. colorswitch="no"

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

  144. else
  145.         echo "USEAGE: $0 [-color] Directory"
  146. fi
复制代码
发表于 2003-5-9 09:45:57 | 显示全部楼层
呵呵,恭喜,恭喜。

现在我正在想办法能不能解决最后文件显示的问题,LYOO兄有没有办法,指点一下?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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