如何在bash/shell脚本中实现tree命令?

Jey*_*Key 1 linux bash shell

我尝试在 bash 中实现一些代码,其输出类似于终端中的“tree”命令。

这里是:

listContent() {
    local file
    for file in "$1"/*; do
        if [ -d $file ]; then
            countSpaces $file
            echo $?"Directory: $file"
            listContent "$file"
        elif [ -f $file ]; then
            countSpaces $file
            echo $?"File: $file"
        fi
    done
}

countSpaces() {
    local space="   "
    for (( i=0; i<${#$1}; i++ )); do
        if [ ${file:$i:1} = "/" ]; then
        space = space + space
        return space
    done
}

listContent "$1"
Run Code Online (Sandbox Code Playgroud)

运行我给出的脚本: ./scriptName.sh directoryName 其中 scriptName 是我的脚本,directoryName 是参数,它是代码应该启动的目录的名称。

我希望看到这样的输出:

Directory: Bash/Test1Dir
    File: Bash/Test1Dir/Test1Doc.txt
Directory: Bash/Test2Dir
    Directory: Bash/Test2Dir/InsideTest2DirDir
        File: Bash/Test2Dir/insideTest2DirDoc.txt
File: Bash/test.sh
Run Code Online (Sandbox Code Playgroud)

但我在完成这段代码时遇到了一些麻烦。有人可以帮我弄清楚为什么它不起作用以及我应该改变什么吗?

将不胜感激。

Cha*_*ffy 5

正确且有效的实现可能如下所示:

listContent() {
  local dir=${1:-.} whitespacePrefix=$2 file
  for file in "$dir"/*; do
    [ -e "$file" ] || [ -L "$file" ] || continue
    if [ -d "$file" ]; then
      printf '%sDirectory %q\n' "$whitespacePrefix" "${file##*/}"
      listContent "$file" "${whitespacePrefix}    "
    else
      printf '%sFile %q\n' "$whitespacePrefix" "${file##*/}"
    fi
  done
}
Run Code Online (Sandbox Code Playgroud)

笔记:

  • 我们不计算空格,而是使用调用堆栈来跟踪空格数量,并附加到每个递归调用上。这避免了需要计算每个名称中 s 的数量/
  • 我们引用所有参数扩展,除了隐式避免字符串分割和全局扩展的有限数量的上下文之一。
  • 我们避免尝试将$?其用于跟踪数字退出状态的预期目的之外的任何用途。
  • printf %q只要存在不受控制的数据(例如文件名),我们就会使用它,以确保即使是恶意名称(包含换行符、光标控制字符等)也会被明确打印。