Bash: One-liner to exit with the opposite status of a grep command?

后端 未结 11 2098
渐次进展
渐次进展 2021-02-06 22:08

How can I reduce the following bash script?

grep -P \"STATUS: (?!Perfect)\" recess.txt && exit 1
exit 0

It seems like I should be able

11条回答
  •  情话喂你
    2021-02-06 22:38

    Since someone already posted a Puppet solution, I might as well add how to invert a shell command run by Ansible:

      - name: Check logs for errors
        command: grep ERROR /var/log/cassandra/system.log
        register: log_errors
        failed_when: "log_errors.rc == 0"
    

    I.e. you just set the failed condition to the return code being 0. So this command fails if we do find the word ERROR in our logs.

    I chose this rather than grep -v as that also inverts grep's output, so we would receive all DEBUG/INFO/WARN lines in log_errors.stdout_lines which we do not want.

提交回复
热议问题