Last active
June 2, 2022 07:11
-
-
Save netikras/f7b78d5fc011ad93bb91b911a6d1d196 to your computer and use it in GitHub Desktop.
A shell function that repeats an action until either some condition is met or timeout occurs. NOTE: blocking actions/condition checks will block the function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## USAGE: | |
## do_until true "ls -l /var/run/dockerd.sock" | |
## do_until true "ls -l /var/run/dockerd.sock 10 1" | |
## a=5; do_until 'a=$((a+1))' 'echo "a=${a}"; [ ${a} -ge 10 ]' 7 1 | |
do_until() { | |
local task=${1:?Task missing}; | |
local condition=${2:?Condition missing}; | |
local timeout=${3:-60}; | |
local pause=${4:-5}; | |
local end_at=$(($(date +%s)+timeout)); | |
local ret=; | |
local now=; | |
while [ -z "${ret}" ] ; do | |
eval "${task}"; | |
now=$(date +%s); | |
if eval "${condition}" ; then | |
ret=0 ; | |
elif [ ${end_at} -le ${now} ] ; then | |
ret=1; | |
else | |
sleep ${pause}; | |
fi; | |
done; | |
return ${ret}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment