Of course, we need a loop to repeat running the program. (This isAn alternative to command substitution would be to use a bit uglytemporary file to store the error output and could possibly be made prettierlook into it afterwards.) This makes it easier to have the standard output of the command visible, too, so let's do that:
#!/bin/bash
re="known error message$"
retries=0
max_retries=5
while errormsg=$errorfile=$(mktemp) # temp file for the errors
trap 'rm "$errorfile"' EXIT # remove the temp file on exit
while cmd "$@" 2>&12> >/dev/null"$errorfile"; ) # run the command
ret=$?; # save the exit code
[[ $errormsg$( < $errorfile ) =~ $re ]]; do # check the output
cat "$errorfile" >&2 # print the errors to
# the script's stderr, too
retries=$((retries+1)) # retry a couple of times
echo -n "cmd failed with known message"
if [[ $retries -lt $max_retries ]]; then
echo ", retrying."
else
echo ", exit."
exit 1
fi
done
cat "$errorfile" >&2
echo "cmd completed succesfully";with status $ret"; # use saved exit code, since it might
exit "$ret" # have failed for other reasons
(while cmd1; cmd2; do ... runs cmd1 at the very start, then runs cmd2 as the loop condition.)
An alternative would be to use a temporary file to store the error output and look into it afterwards. This would make it easier to have the standard output of the command visible, too.