0

如果我做:

if grep someexpression somefile >/dev/null; then
   ...
fi

一切正常。

做一个“不”,我知道我可以做到这一点(使用 [] 这是测试命令):

if [ '!' somecondition ] ; then
   ...
fi

一个简单的解决方法:

grep someexpression somefile >/dev/null
if [ '!' $? ]; then
   ...
fi

但是上面的解决方法不适用于while循环,所以我需要编写一个函数,这很烦人。我该如何正确地使用上面的 grep 东西?

4

1 回答 1

3

尝试:

if ! grep someexpression somefile >/dev/null; then
   ...
fi

请注意,后面的空格!是强制性的,否则它将(尝试)调用历史扩展。

于 2012-04-20T11:27:40.097 回答