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

simple read in loop

Status
Not open for further replies.

vikmos

Technical User
Nov 27, 2001
7
US
Guys,
I've just started UNIX and basically I need script to read file in loop till the end, catch each line, assign values from each line (fields with separators) to some vars and do this till end-of-file.

Just curios is there simple command like "read" or I have to work with sed/awk.
Maybe somebody give a lead what is the most optimal way.

Thanks to everybody.
Bet it's nice question for weekend for most of audience..

Thanks to all + have a very nice blust on weekend.

Vittorio
 
Here's an example of reading a file into different variables where the fields are delimited with a colon :))...
[tt]
#!/bin/ksh

FILE=/etc/passwd

IFS=:

cat ${FILE} | while read USERID PW UID GID NAME HOME SHELL
do
if (( UID <= 100 )) ; then continue; fi
print &quot;${NAME} (${USERID}) has the home directory ${HOME}&quot;
done | sort -i
[/tt]
[tt]IFS[/tt] is the Inter Field Separator. This is how the fields are delimited.

Hope this helps.

 
Hi:

I prefer limiting the field separator only to the while loop:


#!/bin/ksh

FILE=/etc/passwd

while IFS=: read USERID PW UID GID NAME HOME SHELL
do
if (( UID <= 100 )) ; then continue; fi
print &quot;${NAME} (${USERID}) has the home directory ${HOME}&quot;
done < ${FILE} | sort -i
# end script

Regards,

Ed

 
Thanks a lot, guys !

Just question about line below:
if (( UID <= 100 )) ; then continue; fi

is it to catch end-of-file ?

Thanks again
Vittorio
 
No, the line...
Code:
   if (( UID <= 100 )) ; then continue; fi
...just skips user ids (UID) that are less than or equal to 100. These are usually system IDs (i.e. root is 0). I added this just to show how you can use one of the fields that have been read.

The end of file is handled automatically by the...
Code:
   while read USERID PW UID GID NAME HOME SHELL
As long as the &quot;[tt]read[/tt]&quot; actually reads something, it is true and the &quot;[tt]while[/tt]&quot; does another loop. As soon as the &quot;[tt]read[/tt]&quot; gets nothing, the &quot;[tt]while[/tt]&quot; loop is done and the script continues after the &quot;[tt]done[/tt].

Hope this helps.

 
Why not do this?


#!/bin/ksh

cat /your/file/name |
while read LINE
do
assign your values
done

Seams easier
 
Never mind, need to assign values for each line. Helps to read the entire question....lol
 
In addition to the above situation, how could I control the following case.

read /etc/passwd
while
do something
read in a tmp.file
while
do something else
done < tmp.file
done </etc/passwd

the outter loop stopped without reading in the second line and so on from /etc/passwd file
 
It may be clearer if you post your actual code?

You may have to open separate file descriptors if standard input is already being used by the outer loop.

Annihilannic.
 
Hi olded,

I agree with you: IFS should be set locally.
But in you example the IFS assignment is not limited to the while but to the read command. Another read inside the while will use the standard IFS.

[tt]
while IFS=: read USERID PW UID GID NAME HOME SHELL
do
read UPTOASPACE ENDOFLINE
done
[/tt]


So for me your example is even better than you said because IFS is set more locally.

Denis
 
Thanks guys...I finally find out the problem is due to the ssh2 remote call. The ssh2 call to a host and execute a shell command. The above original script will exit the outter loop upon the first execution. I am still looking into why.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top