2017-10-11 105 views
0

我想在一個陣列中的目錄文件名比較字符串和回聲未發現和突破,如果任何一個文件名不存在 - 代碼我寫的是比較在目錄中的文件名數組的字符串

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

然而我無法追蹤我應該在哪裏迴應「未找到」並中斷。請幫助

回答

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 
+0

但是,這給了我沒有發現每次迭代檢查 – RCoder

+0

做一件事..給出目標文件夾的絕對路徑在DIR2爲如。 'DIR2 =「/ home/abc/xyz」'(不要把'/'放在末尾,因爲我們在'if'條件下使用'/' – batMan

+0

@RCoder:我已經更新瞭解決方案!我知道是否有任何問題。 – batMan