Bash IF 条件中的感叹号是否代表否定?
Does the Exclamation point represent negation in Bash IF condition?
我看到了一些类似
的东西
if ! some-command; then
#do something
fi
感叹号有什么作用?我看到如果你使用括号,it can be used for negation。
这里也是一样的效果吗?
如 man bash
中所述:
If the reserved word !
precedes a pipeline, the exit status of that pipeline is the logical negation of the exit status as described above.
正确。
这是一个代码示例:
anny ~> if ! grep $USER /etc/passwd
More input> then echo "your user account is not managed locally"; fi
your user account is not managed locally
anny > echo $?
0
anny >
来源:http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html
我看到了一些类似
的东西if ! some-command; then
#do something
fi
感叹号有什么作用?我看到如果你使用括号,it can be used for negation。
这里也是一样的效果吗?
如 man bash
中所述:
If the reserved word
!
precedes a pipeline, the exit status of that pipeline is the logical negation of the exit status as described above.
正确。
这是一个代码示例:
anny ~> if ! grep $USER /etc/passwd
More input> then echo "your user account is not managed locally"; fi
your user account is not managed locally
anny > echo $?
0
anny >
来源:http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html