欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-05-09 21:24:35
...

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

1.1 该部分实现树形结构

这里写代码片
!/bin/bash
branch_vline="│   "     #Branch vertical line 
branch_null="    "      #null separation branch 
middle_branch_end="├── "  #middle file branch
last_branch_end="└── "    #the last file branch
branch_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 
fi
if [[ $? -eq 0 ]]; then
    tree
fi 

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

这里写代码片
#!/bin/bash
#sheel脚本语言实现tree功能,增加文件的颜色属性,增加文件统计

branch_vline="│   "     #Branch vertical line 
branch_null="    "      #null separation branch 
middle_branch_end="├── "  #middle file branch
last_branch_end="└── "    #the last file branch
branch_sum="" 
filecount=0
directorycount=0
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
                    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 
fi
if [[ $? -eq 0 ]]; then
    tree
    echo
    echo "$directorycount directories,  $filecount files"
fi



上述1.2代码运行结果如下:
(左侧为Linux下tree功能,右侧为1.2脚本程序运行,分别运行带参数以及不带参数的两种情况):
Linux下用sheel脚本完整实现tree功能