Your syntax is faulty as you can't just compare the standard error of some previously executed command with ==
like that.
One suggestion is to save the error stream to a file and then parse that:
#!/bin/bash
if ! my-bin-file-i-run 2>error.log; then
if ! grep -q -F 'name_to_handle_at' error.log; then
echo 'some error message' >&2
exit 1
fi
fi
This would run the command and redirect the standard error stream to a file called error.log
. If the command terminates with an error, grep
is used to look for the string name_to_handle_at
in the log file. If that can't be found, an error message is printed and the script terminates with a non-zero exit status.
In any other case, the script terminates with a zero exit status.
If you want the error.log
file to be removed when your script terminates, you may do so explicitly with rm error.log
in the appropriate places, or with an EXIT
trap:
#!/bin/bash
trap 'rm -f error.log' EXIT
if ! my-bin-file-i-run 2>error.log; then
if ! grep -q -F 'name_to_handle_at' error.log; then
echo 'some error message' >&2
exit 1
fi
fi
if out=$(my-bin-file-i-run 2>&1); then ... elif [[ $out == .... ]] ...
– muru Aug 16 '19 at 07:31