2011-11-22 38 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"