我是 bash 新手,一直在尝试否定以下命令:
wget -q --tries=10 --timeout=20 --spider http://google.com
if [[ $? -eq 0 ]]; then
echo "Sorry you are Offline"
exit 1
如果我已连接到互联网,则此 if 条件返回 true。我希望它以相反的方式发生,但将 !
放在任何地方似乎都不起作用。
最佳答案
您可以选择:
if [[ $? -ne 0 ]]; then # -ne: not equal
if ! [[ $? -eq 0 ]]; then # -eq: equal
if [[ ! $? -eq 0 ]]; then
!
分别反转以下表达式的返回值。
关于linux - 否定 bash 脚本中的 if 条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26475358/