我有一个包含子目录的目录,每个子目录都包含一个特定的脚本和支持文件。我需要验证每个目录中是否都包含正确的文件。这些目录可以随时更改,因此我想使用bash(我认为)并存储以下命令,该命令在数组中返回正确的子目录
find . -maxdepth 1 -type d -not -name home -not -name lhome -print
然后验证每个目录都包含正确的文件:
file1 file2 file3.sh file4.conf
如果特定目录中不包含这些文件,则我需要知道问题所在的目录以及缺少的文件。实现该目标的最佳/正确方法是什么?也许bash是错误的工具,而perl还是更好的选择?
可能有更综合的方法,但这是我的看法:
while read -rd '' directory; do
files=("file1" "file2" "$directory.sh" "$directory.conf")
for file in "${files[@]}"; do
if [ ! -e "$directory/$file" ]; then
echo "$directory is missing $file"
fi
done
done < <(find . -maxdepth 1 -type d -not -name home -not -name lhome -print0)
请注意,这find
还会返回当前目录。如果您希望避免这种情况,则可能要添加一个-mindepth 1
选项。
另外,要使其成为脚本,您可能希望用替换该find
替换.
,$1
以便可以更灵活地指定目标。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句