How to check grep
is empty when set -e
is enabled?
set -e
# ... lots of other lines that need set -e
echo foo | grep bar
EXIT_CODE=$?
if [ "${EXIT_CODE}" -eq 0 ]; then
echo "is there"
else
echo "isn't there"
fi
# ... lots of other lines that need set -e
The script will stop when EXIT_CODE != 0
, instead of printing "isn't there", which is the wanted behavior in this snippet.
Obviously, I can just unset e (set +e
) before and after the grep check.
set -e
# ... lots of other lines that need set -e
set +e
echo foo | grep bar
EXIT_CODE=$?
set -e
if [ "${EXIT_CODE}" -eq 0 ]; then
echo "is there"
else
echo "isn't there"
fi
# ... lots of other lines that need set -e
Is there another better more elegant solution?