[英]Bash “not”: inverting the exit status of a command
我知道我可以做到这一点...
if diff -q $f1 $f2
then
echo "they're the same"
else
echo "they're different"
fi
但是如果我想否定我正在检查的条件怎么办? 即像这样的东西(显然不起作用)
if not diff -q $f1 $f2
then
echo "they're different"
else
echo "they're the same"
fi
我可以做这样的事情...
diff -q $f1 $f2
if [[ $? > 0 ]]
then
echo "they're different"
else
echo "they're the same"
fi
哪里检查上一条命令的退出状态是否大于0。不过这个感觉有点别扭。 有没有更惯用的方法来做到这一点?
if ! diff -q "$f1" "$f2"; then ...
如果你想否定,你正在寻找!
:
if ! diff -q $f1 $f2; then
echo "they're different"
else
echo "they're the same"
fi
或(简单地反转 if/else 操作):
if diff -q $f1 $f2; then
echo "they're the same"
else
echo "they're different"
fi
或者,尝试使用cmp
执行此操作:
if cmp &>/dev/null $f1 $f2; then
echo "$f1 $f2 are the same"
else
echo >&2 "$f1 $f2 are NOT the same"
fi
否定使用if ! diff -q $f1 $f2;
if ! diff -q $f1 $f2;
. 记录在man test
:
! EXPRESSION
EXPRESSION is false
不太确定为什么你需要否定,因为你处理这两种情况......如果你只需要处理它们不匹配的情况:
diff -q $f1 $f2 || echo "they're different"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.