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

后端 未结 11 2102
渐次进展
渐次进展 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:37

    To make it work with set -e surround it in a sub-shell with ( and ):

    $ cat test.sh 
    #!/bin/bash
    
    set -ex
    (! ls /tmp/dne)
    echo Success
    $ ./test.sh 
    + ls /tmp/dne
    ls: cannot access /tmp/dne: No such file or directory
    + echo Success
    Success
    $ mkdir /tmp/dne
    $ ./test.sh 
    + ls /tmp/dne
    $ 
    

提交回复
热议问题