我尝试在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"
运行我给出的脚本:./www.example.com directoryName,其中scriptName是我的脚本,directoryName是参数,它是代码应该从中启动的目录的名称。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.
我希望看到这样的输出:
Directory: Bash/Test1Dir
File: Bash/Test1Dir/Test1Doc.txt
Directory: Bash/Test2Dir
Directory: Bash/Test2Dir/InsideTest2DirDir
File: Bash/Test2Dir/insideTest2DirDoc.txt
File: Bash/test.sh
但是我在完成这段代码时遇到了一些麻烦。有人能帮我弄清楚为什么它不工作,我应该做些什么改变吗?
会很感激的。
2条答案
按热度按时间wn9m85ua1#
正确而有效的实现可能如下所示:
注:
/
的数量。$?
用于跟踪数字退出状态这一预期目的之外的任何其他用途。printf %q
,以确保即使是恶意名称(包含换行符、光标控制字符等)也能明确地打印出来。py49o6xq2#
如果您想要一个没有
Directory
和File
前导的可视化表示,那么下面是一个简单的一行程序( Package 在shell函数中)。