Bash“not”:反转命令的退出状态

前端之家收集整理的这篇文章主要介绍了Bash“not”:反转命令的退出状态前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我知道我能做到这一点……
  1. if diff -q $f1 $f2
  2. then
  3. echo "they're the same"
  4. else
  5. echo "they're different"
  6. fi

但是,如果我想否定我正在检查的条件呢?即这样的事情(显然不起作用)

  1. if not diff -q $f1 $f2
  2. then
  3. echo "they're different"
  4. else
  5. echo "they're the same"
  6. fi

我可以这样做……

  1. diff -q $f1 $f2
  2. if [[ $? > 0 ]]
  3. then
  4. echo "they're different"
  5. else
  6. echo "they're the same"
  7. fi

我在哪里检查前一个命令的退出状态是否大于0.但这感​​觉有点尴尬.有没有比较惯用的方法呢?

  1. if ! diff -q $f1 $f2; then ...

猜你在找的Bash相关文章