0

我创建了一个脚本来比较两个文件中的值并在两种情况下(成功或失败)输出消息。但后来我意识到,即使成功,脚本也会继续将主变量(jbid)与第二个文件中的所有其他变量(bsid)进行比较,这是我不想要的。我希望第二个 for 循环在“if”成功时结束。

我将 tmpcsv2 中的“jbsid”与文件 bkid 中的“bsid”进行比较。

for jbid in `cat tmpcsv`; do #1st Loop

    for bsid in `cat bkid`; do #2nd Loop

        ckid=`echo $bsid | cut -d "," -f1`
        jbsiz=`echo $bsid | cut -d "," -f2`

        if [ "$jbid" -eq "$ckid" ]; then
          printf "There's a match at $ckid\n"  
        else
          printf "No Match Found at $ckid\n"
        fi

    done

    printf "Check for ID $jbid done\n"
    sleep 2
done

我可能使用了错误的方式来执行此操作,并且也打开了更短/更清晰的表单!

4

3 回答 3

1

只需在真正的条件分支中添加“break”即可。

另外,我对 jbid 和 ckid 之间的比较感到困惑。jbsiz 是干什么用的?

for jbid in `cat tmpcsv`; do #1st Loop

    for bsid in `cat bkid`; do #2nd Loop

        ckid=`echo $bsid | cut -d "," -f1`
        jbsiz=`echo $bsid | cut -d "," -f2`

        if [ "$jbid" -eq "$ckid" ]; then
          printf "There's a match at $ckid\n"
          break   
        else
          printf "No Match Found at $ckid\n"
        fi

    done

    printf "Check for ID $jbid done\n"
    sleep 2
done
于 2013-03-16T19:12:47.990 回答
1
if [ "$jbid" -eq "$ckid" ]; then
    printf "There's a match at $ckid\n"   
    break

这应该使第二个 for 循环在匹配时结束。

于 2013-03-16T19:12:56.477 回答
0
if [ "$jbid" -eq "$ckid" ]; then
          printf "There's a match at $ckid\n"
          break   
        else
          printf "No Match Found at $ckid\n"
        fi

then - else在;之间添加中断 就像上面的代码一样。

于 2013-03-16T19:16:43.487 回答