2011-11-22 42 views
0

我有一個名爲l的變量,它包含一串字符 例如 echo $ l輸出將是a1,a2,a3,a4,a5,a6,a7,a8,b1, B2,B3從變量中提取文本

我如何搜索B1,並將其存儲在一個新的臨時將被用於條件檢查。我不能使用awk,因爲定位會改變一切。

所以我的理論會是這樣的,從該字符串得到b1和我將存儲在變量J

$ J = B1

如果[$ J = 'B1']

然後

echo "This is a true statment by b1" 

其他

 echo "This is a false statement not by b1" 

fi

有沒有更快的方法來做到這一點,因爲我認爲我重複了這些步驟。

感謝

回答

1

我認爲這可能爲你工作:

l="a1,a2,a3,a4,a5,a6,a7,a8,b1,b2,b3" 
j="b1" 
if [[ $l =~ $j ]]; then 
    echo "This is a true statement by $j" 
else 
    echo "This is a false statement by $j" 
fi 

[[ $l =~ $j ]] && echo "This is a true by $j" || echo "This is false by $j"