2014-02-20 32 views
0

我需要比較兩個不同數組中存在的值。我曾嘗試編寫代碼。但它不能正常工作。我對shell腳本非常陌生。請幫忙 ! CODE ::如果在shell中使用

declare -a ARR1 
declare -a ARR2 
val1=`cat input1.txt` 
val2=`cat input2.txt` 
ARR1=($val1) 
ARR2=($val2) 
num1=`wc -w input1.txt | sed 's/input1.txt//g'` 
num2=`wc -w input2.txt | sed 's/input2.txt//g'` 
for ((i=0;i<$num1;i++)) 
do 
    for ((j=0;j<$num2;j++)) 
    do 
     if ["${ARR1[i]}" == "${ARR2[j]}"];then 
      echo "EQUAL" 
      break 
     fi 
    done 
done 

輸入將是兩個文件input1和input2。

輸入1

/ns/GSCT_ASNShipmentInfo_E1/E1/svcUpdateVR01ForOP/flow.xml 
    /ns/GSCT_ASNShipmentInfo_E1/E1/Utils/svcPublishWMOS_CTL/flow.xml 
    /ns/GSCT_ASNShipmentInfo_E1/E1/Utils/svcMapIDPDataForPRandCA/flow.xml 

內容的輸入2

/ns/GSCT_ASNShipmentInfo_E1/E1/svcReprocessASNManifest/flow.xml 
    /ns/GSCT_ASNShipmentInfo_E1/E1/svcUpdateVR01ForOP/flow.xml 
    /ns/GSCT_ASNShipmentInfo_E1/E1/Utils/svcPublishWMOS_CTL/flow.xml 
    /ns/GSCT_ASNShipmentInfo_E1/E1/Utils/svcMapIDPDataForPRandCA/flow.xml 

回答

0

這條線:

if ["${ARR1[i]}" == "${ARR2[j]}"];then 

錯過必需的空格。它應該是:

if [ "${ARR1[i]}" == "${ARR2[j]}" ];then 
0

的內容只是爲了比較2個數組,你可以這樣做:

[[ $(printf "%s" "${array1[@]}") == $(printf "%s" "${array2[@]}") ]]