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脚本程序运行,分别运行带参数以及不带参数的两种情况):
阅读全文
0 0
- Linux下用sheel脚本完整实现tree功能
- linux sheel脚本里面“$? -eq o”
- linux sheel command
- linux sheel脚本里面,在if语句里“$? -eq o” 这是什么意思!!!
- linux下如何实现用脚本备份
- 用C#实现完整文档打印功能
- Linux下实现定时器功能
- Linux下实现路由功能
- Linux下短信功能实现
- linux下,实现定时功能
- 练手,用Python实现Linux下的tree命令
- 使用java执行linux的sheel命令
- uboot下用SecureCRT脚本实现一键烧写linux
- java实现tree功能
- Linux下实现脚本的自动交互
- Linux下编写脚本实现Daemon
- linux下使用expect实现自动化脚本
- Linux下MongoDB实现自动备份脚本
- 为什么HashMap是线程不安全的
- 浅谈读入优化姿势
- 深入学习js之浅谈作用域之(eval()和with)
- Unity中查找脚本被哪些Prefab或场景引用
- Form认证timeout无效问题
- Linux下用sheel脚本完整实现tree功能
- webservice简介
- B
- Pyinstaller打包
- 前端PM必须知道的用户心理那些事儿
- HDU
- Treats for the Cows---区间dp--逆序推
- js特效之下星星
- 局部变量和成员变量(全局变量)