我想在bash中编写递归脚本,它接收作为参数的单个路径并打印以此路径为根的目录树的深度。递归函数返回目录深度
这是list_dirs.sh
脚本:
ls -l $dir | grep dr..r..r.. | sed 's/.*:...\(.*\)/\1/'
这是isdir.sh
脚本:
if [ -d $1 ]; then
echo 1
elif [ -e $1 ]; then
echo 0
else
echo -1
fi
他们都工作良好。
这是脚本dir_depth.sh
,我写不工作:
if [ $# -eq 0 ]; then
echo "Usage: ./dir_depth.sh <path>"
exit1
fi
x=`source isdir.sh $1`
if [ $x -eq -1 ]; then
echo "no such path $1"
fi
dir=$1
maxD=0
dirs=`source list_dirs.sh`
for f in $dirs
do
if [ $x -ne 0 ]; then
x=`dir_depth.sh $f`
if [ "$x" -eq "$maxD" ]; then
maxD=x;
fi
fi
echo $f
done
echo $((maxD++))
我真的很新的bash脚本,我不知道如何调试或什么是错在我的脚本。
编辑您的Q可包括你需要处理的目录,并从你的脚本需要输出的一个样本。否则,我们必须猜测; - /,对吗?在每行代码/数据/错误消息的前面使用4个空格,或者高亮显示一段文本,并使用编辑框左上角的格式化工具将其格式化为代码/数据/输出。祝你好运。 – shellter