Linux下用sheel脚本完整实现tree功能

来源:互联网 发布:找回淘宝账号密码 编辑:程序博客网 时间:2024/05/23 02:03

ubuntu下用sheel脚本完整实现tree功能,

1.1 该部分实现树形结构

这里写代码片!/bin/bashbranch_vline="│   "     #Branch vertical line branch_null="    "      #null separation branch middle_branch_end="├── "  #middle file branchlast_branch_end="└── "    #the last file branchbranch_sum="" tree()  {           #Define local variables, implement the number of files under each file, record the number of files,         #and pay attention to the location of this variable definition         local num=0         for file in *;          do          #Count the number of directory files for this file       thelastfile=`ls |wc -l`      num=$((num+1))      #When the file belongs to the last file in that directory, the output is appended to $last_branch_end         if [[ $thelastfile -eq $num ]]; then                if [ -f "$file" ]; then                    echo -e "${branch_sum}$last_branch_end$file"                             fi                #When the file is executable, the display file with green                    if [ -d "$file" ]; then                       echo -e "${branch_sum}$last_branch_end\033[34m$file\033[0m"                        branch_sum=${branch_sum}${branch_null}                        cd "$file"                        tree                          cd ..                          branch_sum=${branch_sum%${branch_null}}                fi                      else                            if [ -f "$file" ]; then                    echo "${branch_sum}${middle_branch_end}$file"                            fi                if [ -d "$file" ]; then                       echo -e "${branch_sum}${middle_branch_end}\033[34m$file\033[0m"                     branch_sum=${branch_sum}${branch_vline}                    cd "$file"                      tree                      cd ..                      branch_sum=${branch_sum%${branch_vline}}              fi                      fi         done  }  if [ -z "$1" ]; then    echo -e "\033[1;34m.\033[0m"else    echo -e "\033[1;34m$1\033[0m"    cd $1 fiif [[ $? -eq 0 ]]; then    treefi 

1.2 在1.1的基础上,稍作改进,将文件的属性在树形结构上用颜色体现,高仿tree功能

这里写代码片#!/bin/bash#sheel脚本语言实现tree功能,增加文件的颜色属性,增加文件统计branch_vline="│   "     #Branch vertical line branch_null="    "      #null separation branch middle_branch_end="├── "  #middle file branchlast_branch_end="└── "    #the last file branchbranch_sum="" filecount=0directorycount=0tree()  {       #Define local variables, implement the number of files under each file, record the number of files,     #and pay attention to the location of this variable definition     local num=0     for file in *;      do          #Count the number of directory files for this file         thelastfile=`ls |wc -l`        num=$((num+1))        #When the file belongs to the last file in that directory, the output is appended to $last_branch_end         if [[ $thelastfile -eq $num ]]; then                if [ -f "$file" ]; then                    filecount=$((filecount+1))                    #When the file is executable, the display file with green                    if [ -x "$file" ];then                        echo -e "${branch_sum}$last_branch_end\033[1;32m$file\033[0m"                    else                        echo -e "${branch_sum}$last_branch_end$file"                    fi                fi                    if [ -d "$file" ]; then                    directorycount=$((directorycount+1))                       echo -e "${branch_sum}$last_branch_end\033[1;34m$file\033[0m"                    branch_sum=${branch_sum}${branch_null}                    cd "$file"                    tree                      cd ..                      branch_sum=${branch_sum%${branch_null}}                fi                      else                            if [ -f "$file" ]; then                    filecount=$((filecount+1))                    if [ -x "$file" ];then                        echo -e "${branch_sum}$middle_branch_end\033[1;32m$file\033[0m"                    else                        echo "${branch_sum}${middle_branch_end}$file"                    fi                                   fi                if [ -d "$file" ]; then                     directorycount=$((directorycount+1))                       echo -e "${branch_sum}${middle_branch_end}\033[1;34m$file\033[0m"                     branch_sum=${branch_sum}${branch_vline}                    cd "$file"                      tree                      cd ..                      branch_sum=${branch_sum%${branch_vline}}              fi                      fi         done  } if [ -z "$1" ]; then    echo -e "\033[1;34m.\033[0m"else    echo -e "\033[1;34m$1\033[0m"    cd $1 fiif [[ $? -eq 0 ]]; then    tree    echo    echo "$directorycount directories,  $filecount files"fi

上述1.2代码运行结果如下:
(左侧为Linux下tree功能,右侧为1.2脚本程序运行,分别运行带参数以及不带参数的两种情况):
这里写图片描述

原创粉丝点击