在shell中使用if

时间:2014-02-20 17:14:50

标签: shell

我需要比较2个不同数组中的值。我试过写代码。但它不能正常工作。我是shell脚本的新手。请帮忙 ! CODE ::

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。

input1的内容

   /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

input2的内容

   /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

2 个答案:

答案 0 :(得分:0)

只是比较你可以做的2个阵列:

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

答案 1 :(得分:0)

这一行:

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

错过了所需的空间。它应该是:

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