What is the best way to emulate a do-while loop in Bash?
I could check for the condition before entering the while
loop, and then continue re-checking the condition in the loop, but that's duplicated code. Is there a cleaner way?
Pseudo code of my script:
while [ current_time <= $cutoff ]; do
check_if_file_present
#do other stuff
done
This doesn't perform check_if_file_present
if launched after the $cutoff
time, and a do-while would.
Two simple solutions:
Execute your code once before the while loop
actions() {
check_if_file_present
# Do other stuff
}
actions #1st execution
while [ current_time <= $cutoff ]; do
actions # Loop execution
done
Or:
while : ; do
actions
[[ current_time <= $cutoff ]] || break
done