2014-02-25 36 views
-1

等於我找不出如何檢測相等,然後返回等於增值經銷商,嘗試了很多方式與此thread比較三個或更多的變量不是在bash

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 

幫助表示讚賞

+0

您究竟想要做什麼? – pfnuesel

+0

我想讓舒爾沒有像AA那樣的變數。 – Eelisland

+0

你嘗試了什麼? – pfnuesel

回答

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 
+0

對不起,我的英語知識不容易,我編輯了我的問題。我已經測試了鏈接線程中的類似代碼,但這不完全是我想要做的。 – Eelisland

+0

我編輯我的答案檢查它,也許這就是你想要的 –

+0

謝謝你的努力,但那不是我想要的。我想檢測'$ tag $ {prst_tag [1]} $ {prst_tag [2]} $ {prst_tag [3]} $ {prst_tag [4]}'是否具有相同的值。 – Eelisland