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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Looping statement ?

Status
Not open for further replies.

pmcmicha

Technical User
May 25, 2000
353
I'm not very good with these yet, so sorry if this is a bit basic. I am trying to come up with a looping construct based that can be adjusted as needed per server it is installed on.

For example....

cat TestFile|wc -l

Based on that output, the looping construct would then only loop that many times.

Then my other question is that if the looping construct can be set up that way, I would also need it to process different variables that I have defined to process just one line at a time, or would the looping construct take care of this already?
 
This should loop through the file, processing one line at a time ...
[tt]
cat TestFile |
while read line
do
set $line
# args now set to contents of one line of TestFile ...
# procesing here ...
done
[/tt]

(I'm sure better coders than me will post something better - I just hack my code from other scripts that are already working :)) One by one, the penguins steal my sanity. X-)

 
Another way would be like this

#/usr/bin/ksh

LOOPNUM=`cat TestFile | wc-l`
COUNTER=0
while [ $COUNTER -ne $LOOPNUM ] ; do
let COUNTER=$COUNTER+1
# Your processing goes here
echo "ok"

done

This will take the number of lines in the TestFile and run whatever you setup in the while loop that many times. So if the TestFile had 20 lines in it, "ok" would print out 20 times on the screen. Hope this helps.

crowe
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top