比较在目录中的文件名数组的字符串
问题描述:
我想在一个阵列中的目录文件名比较字符串和回声未发现和突破,如果任何一个文件名不存在 - 代码我写的是比较在目录中的文件名数组的字符串
#!/bin/bash
# find all json files recursively; cp to destination folder; try to parameterised/read from a file
#find -name '*.json' -exec cp {} destination_folder/ \;
Dir2="destination_folder/*"
declare -a JSON_MandatoryFiles=('abc.json' 'bcd.json' 'efg.json' 'ijk.json');
for i in $Dir2; do
for j in "${JSON_MandatoryFiles[@]}"; do
if [ $j == $(basename $i) ]; then
echo $j "Found"
break
fi
done
done
然而我无法追踪我应该在哪里回应“未找到”并中断。请帮助
答
您可以简单地使用:
Dir2="destination_folder" #remove /* from here
declare -a JSON_MandatoryFiles=('abc.json' 'bcd.json' 'efg.json' 'ijk.json');
for j in "${JSON_MandatoryFiles[@]}"; do
if [ -f "${Dir2}/$j" ]; then
echo "$j exists";
else
echo "$j does not exist"; # include exit; if you want to exit
fi
但是,这给了我没有发现每次迭代检查 – RCoder
做一件事..给出目标文件夹的绝对路径在DIR2为如。 'DIR2 =“/ home/abc/xyz”'(不要把'/'放在末尾,因为我们在'if'条件下使用'/' – batMan
@RCoder:我已经更新了解决方案!我知道是否有任何问题。 – batMan