发一个小脚本自己改写的

sh/bash/dash/ksh/zsh等Shell脚本
回复
头像
7rack
帖子: 96
注册时间: 2011-08-17 22:49

发一个小脚本自己改写的

#1

帖子 7rack » 2011-12-13 23:06

在书上看到一个tree.sh的脚本,功能只能以树的形式显示directories.就从源里安装了个tree,可是中文的文件和文件夹都是\351\230\262\347\201\253\345\242\231\347\232之类的,不知道是什么原因。不过功能蛮强大的...
就在原来的tree.sh上改改加了颜色和显示文件 :em01 下面是代码欢迎吐槽

代码: 全选

#!/bin/bash
   #************************************************#
   #                   filetree.sh                  #
   #                  edited by 7rack               #
   #                December 13, 2011               #
   #                                                #
   #  This script  depends on the tree.sh of 	    #
   # the Advanced Bash Scripting Guide.Without the  #
   # permission!Just for study:)		              #
   #************************************************#
 
#color
NORMAL="\033[00m" # global default, although everything should be something. 
FILE="\033[00m" # normal file
DIR="\033[01;34m" # directory
LINK="\033[01;36m" # symbolic link
FIFO="\033[40;33m" # pipe
SOCK="\033[01;35m" # socket
EXEC="\033[01;32m"  #execute
BLK="\033[40;33;01m" # block device driver
CHR="\033[40;33;01m" # character device driver
ORPHAN="\033[01;05;37;41m" # orphaned syminks
MISSING="\033[01;05;37;41m" # ... and the files they point to  
  
  search () {
  for i in  *
  do
    zz=0                    # ==> Temp variable, keeping track of directory level.
  
    while [ $zz != $1 ]     # Keep track of inner nested loop.
      do
        echo -n "| "        # ==> Display vertical connector symbol,
                            # ==> with 2 spaces & no line feed in order to indent.
        zz=`expr $zz + 1`   # ==> Increment zz.
      done
    if [ -d "$i" ] ; then # ==> If it is a directory (-d)...
      if [ -L "$i" ] ; then # ==> If directory is a symbolic link...
        echo  -e "+--${LINK}$i${NORMAL}" `ls -l $i | sed 's/^.*'$i' //'`
        # ==> Display horiz. connector and list directory name, but...
        # ==> delete date/time part of long listing.
      else

        echo  -e "+--${DIR}$i${NORMAL}"       # ==> Display horizontal connector symbol...
        # ==> and print directory name.
        numdirs=`expr $numdirs + 1` # ==> Increment directory count.
        if cd "$i" ; then         # ==> If can move to subdirectory...
          search `expr $1 + 1`      # with recursion ;-)
          # ==> Function calls itself.
          cd ..
        fi
      fi
   else 
      if [ -L "$i" ] ; then #If file is a symbolic link
        echo -e "---${LINK}$i${NORMAL}" `ls -l $i | sed 's/^.*'$i' //'`
      elif [ -p "$i" ] ; then #If file  is a named pipe 
	echo -e "---${FIFO}$i${NORMAL}"
      elif [ -S "$i" ] ; then #If file is a socket
	echo -e "---${SOCK}$i${NORMAL}"
      elif [ -x "$i" ] ; then #If file's execute (or search) permission is granted
	echo -e "---${EXEC}$i${NORMAL}"
      elif [ -b "$i" ] ; then #If file is block special
	echo -e  "---${BLK}$i${NORMAL}"
      elif [ -c "$i" ] ; then #If file is character special
	echo -e  "---${CHR}$i${NORMAL}"
      else
	echo -e "---${FILE}$i${NORMAL}"
      fi
      ((numfiles+=1)) 	#Increment file count
    fi
  done
  }
  
  if [ $# != 0 ] ; then
    cd $1 # move to indicated directory.
    #else # stay in current directory
  fi
  
  echo "Initial directory = `pwd`"
  numdirs=0
  numfiles=0
 
  search 0
  echo "Total directories = $numdirs ,total files = $numfiles"
  exit 0
演示

代码: 全选

$ ./filetree ~/下载
Initial directory = /home/cat/下载
---aliedit.sh
---install-depot-multisystem.sh
+--OS
| ---archlinux-2011.08.19-core-x86_64.iso
。。。
Total directories = 6 ,total files = 29
头像
枫叶饭团
帖子: 14683
注册时间: 2010-06-16 1:05
系统: Mac OS X
来自: Tencent
联系:

Re: 发一个小脚本自己改写的

#2

帖子 枫叶饭团 » 2011-12-13 23:27

路过,有空研究研究。
头像
josephyoung
帖子: 158
注册时间: 2011-11-05 18:53
来自: 南极圈

Re: 发一个小脚本自己改写的

#3

帖子 josephyoung » 2011-12-13 23:31

:em06 这个真猛,我在~下执行了一次差点没死机,貌似包括所有的子文件夹全列出来了,幸好没在/下执行,不然我这小机子得挂
头像
7rack
帖子: 96
注册时间: 2011-08-17 22:49

Re: 发一个小脚本自己改写的

#4

帖子 7rack » 2011-12-13 23:34

josephyoung 写了::em06 这个真猛,我在~下执行了一次差点没死机,貌似包括所有的子文件夹全列出来了,幸好没在/下执行,不然我这小机子得挂
:em05 就是哦,这个是不是和算法中空间和时间复杂度有关啊,我不懂这些东东
回复