3 ms·
Another basic problem I run into is that grep returns whether it found a match or not as its exit status. This means if you, for example, run a script with bash
by zmmmmm 6y ago
Another basic problem I run into is that grep returns whether it found a match or not as its exit status. This means if you, for example, run a script with bash option
set -e
Then the session will exit unceremoniously on any grep that doesn't match. This often catches you out when you say, develop a script without -e and use it for a while, and then one day someone deploys the same thing with -e enabled because they think it will be more robust if the script terminates if a command fails - and boom, now suddenly your script is randomly broken depending on text matches of the files it is processing. It is even worse if you are sourcing the script somehow from within an existing session and it terminates your interactive shell!
- Anthony-G 6y agoI run scripts using both dash and bash and always use `set -eu`. Returning a false exit status is not a problem if you wrap the `grep` inside an `if` statement: if grep pattern file; then echo y; fi