我不知道如何检测相等并返回相等的变量,用这个线程尝试了很多方法
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
感谢帮助
我真的不明白你的问题,但我假设你想比较字符串。下面是如何将字符串与字符串数组进行比较的示例:
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