简体   繁体   English

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

[英]How to implement tree command in bash/shell script?

I tried to implement in bash some code which output would be similar to the "tree" command in the terminal.我尝试在 bash 中实现一些代码,其中 output 类似于终端中的“树”命令。

Here it is:这里是:

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"

Running the script I give: ./scriptName.sh directoryName where scriptName is my script and directoryName is the argument which is the name of the directory from which the code should start.运行我给出的脚本: ./scriptName.sh directoryName 其中 scriptName 是我的脚本,directoryName 是参数,它是代码应该从中开始的目录的名称。

I would like to see the output like this:我希望看到这样的 output:

Directory: Bash/Test1Dir
    File: Bash/Test1Dir/Test1Doc.txt
Directory: Bash/Test2Dir
    Directory: Bash/Test2Dir/InsideTest2DirDir
        File: Bash/Test2Dir/insideTest2DirDoc.txt
File: Bash/test.sh

But I have some troubles in completing this code.但是我在完成这段代码时遇到了一些麻烦。 Could someone help me figure it out why it isn't working and what should I change?有人可以帮我弄清楚为什么它不起作用以及我应该更改什么吗?

Will be grateful.将不胜感激。

A correct and efficient implementation might look like: 正确而有效的实现可能看起来像:

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
}

Note: 注意:

  • Instead of counting spaces, we use the call stack to track the amount of whitespace, appending on each recursive call. 除了使用空格,我们还使用调用堆栈来跟踪空白量,并将其附加在每个递归调用上。 This avoids needing to count the number of / s in each name. 这样避免了需要计算每个名称中的/ s数。
  • We quote all parameter expansions, except in one of the limited number of contexts where string-splitting and glob expansions are implicitly avoided. 我们引用了所有参数扩展,除了在有限数量的上下文之一中隐式避免了字符串拆分和glob扩展之外。
  • We avoid attempts to use $? 我们避免尝试使用$? for anything other than its intended purpose of tracking numeric exit status. 除了用于跟踪数字出口状态的预期目的外,还可以用于其他目的。
  • We use printf %q whenever uncontrolled data (such as a filename) is present, to ensure that even malicious names (containing newlines, cursor-control characters, etc) are printed unambiguously. 每当出现不受控制的数据(例如文件名)时,我们都会使用printf %q ,以确保即使是恶意名称(包含换行符,光标控制字符等)也能被明确打印。

if you want a visual representation without the Directory and File leaders, then the following is a simple one-liner (wrapped in a shell function).如果你想要一个没有DirectoryFile领导者的视觉表示,那么下面是一个简单的单行代码(包装在 shell 函数中)。

treef() (
    [ -d "$1" ] && { dir="$1"; shift; } || dir='.'
    find "$dir" "$@" | sed -e 's@/@|@g;s/^\.|//;s/[^|][^|]*|/ |/g;/^[. |]*$/d'
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM