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

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

    The problem with the grep answers is that if the file is empty you also get a clean response, as if the file had a perfect. So personally I gave up on grep for this and used awk.

    awk 'BEGIN{ef=2}; /STATUS: Perfect/{ ef=0;}; /STATUS: Busted/{ print;eff=3;}; END{exit (ef+eff)}' a.txt ; echo $?
    
    This has exit status:
     0 :  Perfect and !Busted
     2 : !Perfect and  Busted
     3 :  Perfect and  Busted
     5 : !Perfect and !Busted
    

提交回复
热议问题