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