linux - 否定 bash 脚本中的 if 条件

我是 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

! 分别反转以下表达式的返回值。

https://stackoverflow.com/questions/26475358/

相关文章:

python - sum() 之类的函数是什么,但用于乘法?产品()?

linux - 在管道 grep 到 grep 后保留颜色

python - 为什么提早返回比其他方法慢?

python - 在列表中查找属性等于某个值的对象(满足任何条件)

linux - 使用 Unix 排序对多个键进行排序

linux - 如何更改 bash 历史完成以完成已经上线的内容?

python - 在多个上下文管理器上创建一个 "with" block ?

python - 使用代码存储库时如何引用资源的相对路径

linux - 如何使 rpm 自动安装依赖项

windows - 互斥锁和临界区有什么区别?