Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 在Unix while do循环中,比较条件失败_Linux_Bash_Shell_Unix_Awk - Fatal编程技术网

Linux 在Unix while do循环中,比较条件失败

Linux 在Unix while do循环中,比较条件失败,linux,bash,shell,unix,awk,Linux,Bash,Shell,Unix,Awk,输入文件包括: 文件1的架构为: offer rank score lat lon eligiblecms 文件1: 10000,1,0.0,"-31.940742,115.86829",3760006987500 12345,2,0.0,"-31.940742,115.86829",3760006987500 13245,3,0.0,"-31.940742,115.86829",3760006987500 11111,4,0.0,"-31.940742,115.86829",37600069

输入文件包括:

文件1的架构为:

offer rank score lat lon eligiblecms
文件1:

10000,1,0.0,"-31.940742,115.86829",3760006987500
12345,2,0.0,"-31.940742,115.86829",3760006987500
13245,3,0.0,"-31.940742,115.86829",3760006987500
11111,4,0.0,"-31.940742,115.86829",3760006987500
11112,5,0.0,"-31.940742,115.86829",3760006987500
文件2的架构为:

offer1 rank1 score1 lat1 lon1 eligiblecms1
文件2

10000,1,0.0,"-31.940742,115.86829",3760006987500
12345,2,0.0,"-31.940742,115.86829",3760006987500
13245,3,0.0,"-31.940742,115.86829",3760006987500
11111,4,0.0,"-31.940742,115.86829",3760006987500
11112,5,0.0,"-31.940742,115.86829",3760006987500
32152,6,0.0,"-31.940742,115.86829",3760006987500
32153,7,0.0,"-31.940742,115.86829",3760006987500
32154,8,0.0,"-31.940742,115.86829",3760006987500
32163,9,0.0,"-31.940742,115.86829",3760006987500
32164,10,0.0,"-31.940742,115.86829",3760006987500
32165,11,0.0,"-31.940742,115.86829",3760006987500
32167,12,0.0,"-31.940742,115.86829",3760006987500
32170,13,0.0,"-31.940742,115.86829",3760006987500
32171,14,0.0,"-31.940742,115.86829",3760006987500
32182,15,0.0,"-31.940742,115.86829",3760006987500
32183,16,0.0,"-31.940742,115.86829",3760006987500
我的代码如下:

#!/bin/bash
while IFS=,
read offer rank score lat lon eligiblecms
#dataflag="F"
do
while IFS=,
read offer1 rank1 score1 lat1 lon1 eligiblecms1
do
 if [ "$offer" = "$offer1" ]
 then
echo "Details of Offer $offer   :"
      if [ "$lat" = "$lat1" ] && [ "$lon" = "$lon1" ]
      then
      echo "Expected latlong "$lat", "$lon" successfully  matched with the Actual  Latlong: "$lat1,$lon1" "
 else
      echo " Expected latlong "$lat","$lon" did not  matched with the Latlong of API output offer "$offer1" "
      fi
 if [ "$eligiblecms" = "$eligiblecms1" ]
 then
         echo " Expected UID "$eligiblecms" successfully  matched with the UID of API output offer "$eligiblecms1" "
 else

         echo " Expected UID "$eligiblecms" did not  matched with UID of API output offer "$offer1" "
         fi
 fi
done <$1
#if [ "$dataflag" = "F" ]
#then
#echo ""$offer" not found"
#fi
done <$2
我不确定为什么我不能在任何时候为我的代码获得指定的条件true,根据我的逻辑,它应该是true

"$eligiblecms" = "$eligiblecms1"
我得到的输出为:

Expected UID 3760006987500 did not  matched with UID of API output offer 11112
根据我的逻辑,输出应为:

Expected UID "$eligiblecms" successfully  matched with the UID of API output offer "$eligiblecms1

请提供帮助。

这是编写脚本的正确方法:

$ cat tst.awk                
BEGIN {
    FS = "[,\"]+"
    offer       = ++enum
    rank        = ++enum
    score       = ++enum
    lat         = ++enum
    lon         = ++enum
    eligiblecms = ++enum
}

NR==FNR { file1[$offer] = $0; next }
{
    if ( $offer in file1 ) {
        split(file1[$offer],expd)
        split($0,act)

        print "Details of Offer " act[offer] "   :"

        if ( (act[lat] == expd[lat]) && (act[lon] == expd[lon]) ) {
            print "Expected latlong " expd[lat] ", " expd[lon] " successfully  matched with the Actual  Latlong: " act[lat] "," act[lon]" "
        }
        else {
            print " Expected latlong " expd[lat] "," expd[lon] " did not  matched with the Latlong of API output offer " act[offer] " "
        }

        if ( act[eligiblecms] == expd[eligiblecms] ) {
            print " Expected UID " expd[eligiblecms] " successfully  matched with the UID of API output offer " act[eligiblecms] " "
        }
        else {
            print " Expected UID " expd[eligiblecms] " did not  matched with UID of API output offer " act[offer] " "
        }
    }
    else {
        print $offer, "not found"
    }
}


idk i我设法正确地复制了你的逻辑,但希望它足够清晰,你可以在必要的地方反复尝试。我尝试使用尽可能与您已经使用的语法相似的语法,以便您在不了解awk的情况下最容易理解。

[“$dataflag”=“F”]
是一个错误,因为它总是正确的。你不是在测试你认为你是什么。将其更改为
[“$dataflag”=“F”]
。另一方面,您正在将兼容POSIX的
[
与仅限Bash的
=
相结合。我建议将
[
=
结合使用。如果您因为熟悉其他语言而更喜欢
=
,请使用
[[
。错误消息告诉您问题出在哪里:您需要在变量周围使用双引号:
[$eligiblecms=$eligiblecms1]
应该是
[“$eligiblecms”=“$eligiblecms1”]
。或者,如果使用Bash的扩展测试,您不需要引号(如果您愿意,可以使用
==
):
[[$eligblecms=$eligblecms1]]
。谢谢Tom,我已经重新表述了我的问题,我相信现在你可以有一个更好的想法。使用
set-x
检查正在执行的内容,并确保你的变量具有你期望的值。另外,作为一条一般性建议,将
echo“Expected latlong”$lat“,$lon”成功更改为
echo“Expected latlong”\“$lat\”,“$lon\”成功…
。每次你在shell中编写循环只是为了操纵文本时,你的方法都是错误的。发明UNIX shell来对UNIX工具进行顺序调用的人发明了UNIX工具awk来操纵文本。不要让他们失望:-)。谢谢@Ed Morton,它看起来很棒,你能解释一下代码吗,那会非常有帮助。@Anusn不,这都是非常基本的awk,所以只要参考Arnold Robbins的《有效的awk编程》一书,我很乐意回答任何具体问题。
$ cat tst.awk                
BEGIN {
    FS = "[,\"]+"
    offer       = ++enum
    rank        = ++enum
    score       = ++enum
    lat         = ++enum
    lon         = ++enum
    eligiblecms = ++enum
}

NR==FNR { file1[$offer] = $0; next }
{
    if ( $offer in file1 ) {
        split(file1[$offer],expd)
        split($0,act)

        print "Details of Offer " act[offer] "   :"

        if ( (act[lat] == expd[lat]) && (act[lon] == expd[lon]) ) {
            print "Expected latlong " expd[lat] ", " expd[lon] " successfully  matched with the Actual  Latlong: " act[lat] "," act[lon]" "
        }
        else {
            print " Expected latlong " expd[lat] "," expd[lon] " did not  matched with the Latlong of API output offer " act[offer] " "
        }

        if ( act[eligiblecms] == expd[eligiblecms] ) {
            print " Expected UID " expd[eligiblecms] " successfully  matched with the UID of API output offer " act[eligiblecms] " "
        }
        else {
            print " Expected UID " expd[eligiblecms] " did not  matched with UID of API output offer " act[offer] " "
        }
    }
    else {
        print $offer, "not found"
    }
}
$ awk -f tst.awk file1 file2
Details of Offer 10000   :
Expected latlong -31.940742, 115.86829 successfully  matched with the Actual  Latlong: -31.940742,115.86829 
 Expected UID 3760006987500 successfully  matched with the UID of API output offer 3760006987500 
Details of Offer 12345   :
Expected latlong -31.940742, 115.86829 successfully  matched with the Actual  Latlong: -31.940742,115.86829 
 Expected UID 3760006987500 successfully  matched with the UID of API output offer 3760006987500 
Details of Offer 13245   :
Expected latlong -31.940742, 115.86829 successfully  matched with the Actual  Latlong: -31.940742,115.86829 
 Expected UID 3760006987500 successfully  matched with the UID of API output offer 3760006987500 
Details of Offer 11111   :
Expected latlong -31.940742, 115.86829 successfully  matched with the Actual  Latlong: -31.940742,115.86829 
 Expected UID 3760006987500 successfully  matched with the UID of API output offer 3760006987500 
Details of Offer 11112   :
Expected latlong -31.940742, 115.86829 successfully  matched with the Actual  Latlong: -31.940742,115.86829 
 Expected UID 3760006987500 successfully  matched with the UID of API output offer 3760006987500 
32152 not found
32153 not found
32154 not found
32163 not found
32164 not found
32165 not found
32167 not found
32170 not found
32171 not found
32182 not found
32183 not found
$