如何在Linux上使用grep以冗长的方式报告文件中是否存在字符串?


14

我想知道ack_enabled not defined特定文件中是否存在all_defs.txt并返回某些内容,通过它我可以了解字符串是否存在。

谁能告诉我该怎么做?

Answers:


19

只需检查的退出代码grep-q使它静音,!否定退出代码:

if ! grep -q 'ack_enabled not defined' all_defs.txt ; then
    echo Not found.
fi

7

更细的单线

下面的一线更加冗长:

grep -q 'ack_enabled not defined' all_defs.txt && echo 'string found' || echo 'string not found'
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.