比较三个或更多变量在bash中是不相等的

时间:2014-02-25 21:26:30

标签: bash equality

我无法弄清楚如何检测平等并返回相等的变量,尝试了很多方法thread

    tag="AA"
prst_tag[1]="BB"
prst_tag[2]="CC"
prst_tag[3]="AA"
prst_tag[4]="EE"

我到底想做什么:

if $tag or ${prst_tag[1]} or ${prst_tag[2]} or ${prst_tag[3]} or ${prst_tag[4]} have equal value; then
    echo "equal TAG found"
    echo "tag: $tag"
    echo "prst_tag[1]: ${prst_tag[1]}"
    echo "prst_tag[2]: ${prst_tag[2]}"
    echo "prst_tag[3]: ${prst_tag[3]}"
    echo "prst_tag[4]: ${prst_tag[4]}"
fi

帮助表示赞赏

1 个答案:

答案 0 :(得分:0)

我真的不明白你的问题,但我想你想要比较字符串。以下是如何将字符串与字符串数组进行比较的示例:

tag="AA"
prst_tag[1]="BB"
prst_tag[2]="CC"
prst_tag[3]="AA"
prst_tag[4]="EE"
found=false

for i in "${prst_tag[@]}"
do
   if [ "$tag" = "$i" ]; then
     found=true
   fi 
done

if [ "$found" = "true" ];then
   echo "equal TAG found"
else 
   echo "equal TAG not found"
fi