【发布时间】:2014-10-18 05:47:31
【问题描述】:
我不知道我花了很多时间来处理这个问题。我需要写脚本。脚本应递归循环当前目录中的子目录。它应该检查每个目录中的文件数。如果文件数大于 10,则应将这些文件的所有名称写入名为“BigList”的文件中,否则应写入文件“ShortList”中。这应该看起来像
---<directory name>
<filename>
<filename>
<filename>
<filename>
....
---<directory name>
<filename>
<filename>
<filename>
<filename>
....
仅当子目录不依次包含子目录时,我的脚本才有效。 我对此感到困惑。因为它不像我预期的那样工作。对于我来说,用任何编程语言编写这个都需要不到 5 分钟的时间。 请帮助解决这个问题,因为我不知道该怎么做。 这是我的脚本
#!/bin/bash
parent_dir=""
if [ -d "$1" ]; then
path=$1;
else
path=$(pwd)
fi
parent_dir=$path
loop_folder_recurse() {
local files_list=""
local cnt=0
for i in "$1"/*;do
if [ -d "$i" ];then
echo "dir: $i"
parent_dir=$i
echo before recursion
loop_folder_recurse "$i"
echo after recursion
if [ $cnt -ge 10 ]; then
echo -e "---"$parent_dir >> BigList
echo -e $file_list >> BigList
else
echo -e "---"$parent_dir >> ShortList
echo -e $file_list >> ShortList
fi
elif [ -f "$i" ]; then
echo file $i
if [ $cur_fol != $main_pwd ]; then
file_list+=$i'\n'
cnt=$((cnt + 1))
fi
fi
done
}
echo "Base path: $path"
loop_folder_recurse $path
【问题讨论】:
-
您的代码的缩进令人费解且难以理解。
-
很抱歉,请在不使用 find 的情况下帮助执行此操作