Reputation: 4701
I have a loop in my KornShell (ksh) script that I want to execute at least once, and I want an elegant way of doing it, however while I have found plenty of stuff on how to write a while loop, there does not seem to be anything for a do...while loop out there.
What I am doing is reading in a comma-delimited list of files from a configuration file and processing them. If the list of files is empty, then I want to process all files in the directory.
What is a good way to do this?
EDIT: Here is what I have currently. I grab the filename, then remove it from the string for the next pass. If the list of Files is empty, I quit the loop. BUT, if the list is empty to begin with, I want it to still run once.
while [[ -n "${FILES%%,*}" ]]; do
FILE="${FILES%%,*}"
FILES="${FILES#*,}"
done
Upvotes: 2
Views: 24726
Reputation: 8456
Yes, the standard while
loop in ksh
supports this out of the box:
while ...; do ...; done
The standard while
loop has code blocks before and after do
.
Each block may contain multiple commands. Conventionally we use only a single command for the first block, and its exit status determines whether the loop terminates or is continued.
When we use multiple commands, only the status of the last command matters.
while
echo do this always # replace with your code
[[ -n "${FILES%%,*}" ]]
do
FILE="${FILES%%,*}"
FILES="${FILES#*,}"
done
Upvotes: 10
Reputation: 247062
you could fake it:
do_once=true
while $do_once || [[ other condition ]]; do
: your stuff here
do_once=false
done
Upvotes: 4
Reputation: 5959
There is no such construct in ksh. You can emulate this by break
(or continue
) at the end of a while true; do ... ; done
loop.
Upvotes: 1