How to retry and log the final result in Bash

I know there are lots of how-to write retry logic in Bash, such as here.

I’ve decided to use the one in the accepted answer above.
However I wanted to add another logic to log the final result as well, so I needed to modify this snippet as sleep alw…


This content originally appeared on DEV Community and was authored by rinaxsumomo

I know there are lots of how-to write retry logic in Bash, such as here.

I've decided to use the one in the accepted answer above.
However I wanted to add another logic to log the final result as well, so I needed to modify this snippet as sleep always returns exit code 0 at last that logging depending on the exit code would not work as it always returns 0, even the command to be retried ended up with failure.

So here is my snippet. Below runs YOUR_COMMAND until success, 4 times for the maximum, and echo the result based on the exit code. This might not work depending on how YOUR_COMMAND returns a result, but it worked in my case.

NEXT_WAIT_TIME=0
RESULT=0
until [ ${NEXT_WAIT_TIME} -eq 4 ] || YOUR_COMMAND
do
    sleep $(( NEXT_WAIT_TIME++ ))
done
if [[ ${NEXT_WAIT_TIME} -eq 4 ]]; then
    RESULT=1
fi
if [[ ${RESULT} -eq 0 ]]; then
    echo "Command Successful"
else
    echo "Command Failed"
fi


This content originally appeared on DEV Community and was authored by rinaxsumomo


Print Share Comment Cite Upload Translate Updates
APA

rinaxsumomo | Sciencx (2021-09-28T07:23:22+00:00) How to retry and log the final result in Bash. Retrieved from https://www.scien.cx/2021/09/28/how-to-retry-and-log-the-final-result-in-bash/

MLA
" » How to retry and log the final result in Bash." rinaxsumomo | Sciencx - Tuesday September 28, 2021, https://www.scien.cx/2021/09/28/how-to-retry-and-log-the-final-result-in-bash/
HARVARD
rinaxsumomo | Sciencx Tuesday September 28, 2021 » How to retry and log the final result in Bash., viewed ,<https://www.scien.cx/2021/09/28/how-to-retry-and-log-the-final-result-in-bash/>
VANCOUVER
rinaxsumomo | Sciencx - » How to retry and log the final result in Bash. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/09/28/how-to-retry-and-log-the-final-result-in-bash/
CHICAGO
" » How to retry and log the final result in Bash." rinaxsumomo | Sciencx - Accessed . https://www.scien.cx/2021/09/28/how-to-retry-and-log-the-final-result-in-bash/
IEEE
" » How to retry and log the final result in Bash." rinaxsumomo | Sciencx [Online]. Available: https://www.scien.cx/2021/09/28/how-to-retry-and-log-the-final-result-in-bash/. [Accessed: ]
rf:citation
» How to retry and log the final result in Bash | rinaxsumomo | Sciencx | https://www.scien.cx/2021/09/28/how-to-retry-and-log-the-final-result-in-bash/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.