将目录中的文件名与Array的字符串进行比较

时间:2017-10-11 06:34:14

标签: shell

我想将目录中的文件名与数组中的字符串进行比较,并且找不到echo,如果没有任何文件名,则会中断 - 我写的代码是

#!/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

然而,我无法追踪我应该在哪里保持其他回声“未找到”并打破。请帮忙

1 个答案:

答案 0 :(得分:0)

您可以简单地使用:

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
相关问题