Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

for loop in Bourne compared to Korn

Status
Not open for further replies.

goldenradium2001

Technical User
Mar 22, 2002
91
US
For the for loop in the Korn shell, I know that it's possible to read from a file and use the contents of the file as input to the for loop. FOr example, the following is possible in the Korn shell:

for variable in $(< file)
do
echo $variable
done

Within file is the following:
apple
orange
pear
peach

So the output from running the for loop in Korn would be:
apple
orange
pear
peach

However, is there a way to do this on the Bourne shell or is this strictly Korn shell.

Thanks.
 
Golden:

This works whether it's sh or ksh:

#!/bin/sh

while read variable
do
echo $variable
done < filename

 
another way is

for i in `cat filename`
do
echo $i
done
 
pavNell:

for i in `cat filename`
do
echo $i
done

is an example of a Useless Use of Cat, UUOC. If you've never heard of it before, don't feel bad. I had to ask the first time I was accused of it.

If you post in another forum such as comp.unix.shell expect to be flamed. I won't do that, but I will say that using the while loop is more efficient because you save creating a subshell for the &quot;cat&quot;.

Regards,

Ed
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top