Rerunning a command in a script that failed?

I have a script that occasionally has a command here and there that fails and I would like to set my script up to just re run the command if the exit code is 1.

Is there a simple way to do that without if/thens or redirecting to the command again?

How would you know if your script failed or not without testing? you can always manually run it :smiley:

You COULD:

until [ $status == 0 ]
do
somecommand someargs
status=$?
done

echo "Exited with status:$status"

but it would loop indefinitely if the command always failed. Would probabally want to put some limit to the number of times to retry.

This is really not a great shell coding best practice but

somecommand arg1 || somecommand arg1

executes somecommand1 an additional time if it fails the first time.

This will NOT work with pipes i.e., command1 | command2 because the status returned is from the rightmost element in the line, but others may fail. Some implementations have a workaround for this problem.

files will be missing if a specific command fails - I get emailed the files thats how I know. its a reporting script and the apps reporting command sometimes just randomly fails for no reason what so ever... It would be great if there was an easy way to just rerun that one failed command.

I guess I will be a big boy and add in flow control for those commands failing :frowning:

Ill give this a try, Ill need to test it in my environment, this report is kind of heavy on the system so I will defiantly only need to make sure it runs 2x at the most :smiley: