I would like to have the echo
command executed when cat /etc/passwd | grep \"sysa\"
is not true.
What am I doing wrong?
if
What am I doing wrong?
$(...)
holds the value, not the exit status, that is why this approach is wrong. However, in this specific case, it does indeed work because sysa
will be printed which makes the test statement come true. However, if ! [ $(true) ]; then echo false; fi
would always print false
because the true
command does not write anything to stdout (even though the exit code is 0). That is why it needs to be rephrased to if ! grep ...; then
.
An alternative would be cat /etc/passwd | grep "sysa" || echo error
. Edit: As Alex pointed out, cat is useless here: grep "sysa" /etc/passwd || echo error
.
Found the other answers rather confusing, hope this helps someone.