gpt4 book ai didi

linux - 递归脚本中的目录深度

转载 作者:太空宇宙 更新时间:2023-11-04 12:07:42 25 4
gpt4 key购买 nike

您好,我想在我的 linux bash 家庭作业方面获得一些帮助。我必须制作一个脚本来获取目录并返回最深子目录的深度(每个目录+1)。我必须递归地做。我必须使用“list_dirs.sh”获取可行目录并回显其子目录。

这就是我到目前为止所得到的:

dir=$1
sub=`source list_dirs.sh`

((depth++))

for i in $sub
do
if [ -n "$sub" ] ; then
./depthScript $dir/$i
fi
done

if ((depth > max)) ; then
max=$depth
echo $max
fi

在用一个应该返回 3 的目录进行测试后,我得到了:

1
1
1
1

我的深度计数器似乎忘记了以前的值,我得到了输出 每个目录.. 需要一些帮助!

最佳答案

您可以使用 bash functions创建递归函数调用。

您的函数理想情况下在基本情况下回显 0,在没有子目录的目录上调用它,在存在某些子目录 $subdir 的情况。参见 this question on recursive functions in bash一个框架。

关于linux - 递归脚本中的目录深度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50029251/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com