Marc Herbert a écrit :
> Chris F.A. Johnson a écrit :
>> For example, if you want to know whether user 'john' is in the
>> password file:
>>
>> grep -q ^john: /etc/passwd
>>
>> You want to get the return code and execute code depending on the result
>>
>> (which can be 0, 1 or >1; 'if grep ...' is not adequate).
>
> I find "if grep ..." adequate. Ignoring >1 here is just another small
> hole in the "set -e" safety net (and it will log an error message).
Just found this, even better: not even any extra hole in the "set -e"
safety net.
set -e
...
if grep -q ^john: /etc/passwd; then
echo "FOUND"
else grepstatus=$?
# inspect $grepstatus for errors
fi