KSH For Loop Examples

Q. How do I use for loop in Korn Shell under UNIX / Linux / BSD / OS X operating systems?A.The main advantage of ksh over the traditional Unix shell is in its use as a programming language. KSH support for loop.

KSH Scripting: for loop syntax

The syntax is as follows:

for{Variable}in{lists}doecho${Variable}done

Here is sample shell script to print welcome message 5 times:

#!/bin/kshfor i in12345doecho"Welcome $i times"done

Run script as follows:$ chmod +x script.ksh $ ./script.ksh OR$ ksh script.ksh You can easily set ranges (1 to 10) as follows:

#!/bin/kshfor i in{1..10}doecho"Welcome $i times"done

You can also use variables to define the item list. They will be checked ONLY ONCE, when you start the loop.

What if you have 5 or 6 files, specific files .biz in a directory and you want to store them one at the time in a variable $mirage, and want to process $mirage until no more .biz file are found. How will you modify the script accordingly