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

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

    if anyone gets here looking for a bash return code manipulation:

    (grep   || exit 0 && exit 123;)
    

    this will return 0 (success) when grep finds nothing, and return 123 (failure) when it does. The parenthesis are in case anyone test it as is on the shell prompt. with parenthesis it will not logout on the exit, but just exit the subshell with the same error code.

    i use it for a quick syntax check on js files:

    find src/js/ -name \*js -exec node \{\} \; 2>&1 | grep -B 5 SyntaxError || exit 0 && exit 1;
    

提交回复
热议问题