2011-05-24 94 views

回答

4

当测试的grep -q的结果,你想测试$?不grep的输出,这将是空

at=$(echo "$1" | grep -q "@") 
if [ $? -ne 0 ]; then ... 

或者干脆

if echo "$1" | grep -q "@"; then ... 

,或者更多的bash-LY

if grep -q "@" <<< "$1"; then ... 

或者,如果不调用grep:

if [[ "$1" == *@* ]]; then ... 

case "$1" in 
    *@*) echo "match" ;; 
    *) echo "no match" ;; 
esac 
+0

当测试grep -q的结果时,您想要测试$? :完全谢谢你的目标,我想我今天学到了一些东西:)特别喜欢“不grep”语法。顺便一提,谢谢 – user767990 2011-05-24 16:35:32

3

-ne外部的微小的工作是比较整数。使用!=来比较字符串。

+0

感谢了很多人!它工作正常,但它需要[“$ at”!= 0];感谢您的快速回复:) - – user767990 2011-05-24 15:47:26

相关问题