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!

KSH - reading a file 2

Status
Not open for further replies.

gallows

Technical User
Jun 12, 2004
223
US
I need to check for passwd expiration and am trying to use ksh. What is the best way to read a file, assign variables values etc? I was thinking about something like this but I don't know how ksh knows it is at the end of the file. I don't want it to loop forever.

for i in `cat $SHADOW_WORK`
do

USER_NAME=`echo $i | awk -F: '{print \$1}`
STA_TUS=`echo $i | awk -F: '{print \$2}`
LAST_CHANGE=`echo $i | awk -F: '{print \$3}`
integer DAYS_TILL="$EPOCH_DATE - $LAST_CHANGE"

if something
then
elif
then
elif then

else
done

Any suggestions are greatly appreciated.

gallows


 
Why don't you use a while loop? This program reads the passwd file and lists the fields:

#!/bin/ksh

while IFS=":" read f1 f2 f3 f4 f5 f6
do
echo "$f1"
echo "$f2"
echo "$f3"
echo "$f4"
echo "$f5"
echo "$f6"
done < /etc/passwd
#
 
Thanks olded. I certainly could. I have 2 questions.

1. Wouldn't I still need the cat /etc/passwd as the first line?

2. How does this while loop know when to stop? From what I have read on the while loop, it loops until the condition is no longer true. So the first entry would be read, there is no colon after f6 (as an example) so wouldn't processing stop there?

Sorry for the newbie questions. This is my first script and I need to understand the basics. I have been reading tons of stuff on tutorials etc and a lot of them are way out dated. When I try the examples provided, some just don't work because the shell has changed:)) Can you recommend a good source of info for ksh?

Thanks,

Gallows
 
1 - No, you wouldnt need to cat the file first. If you look at the while loop, you will see that the password file is being "redirected" into it. When it tries to read f1 thru f6, it will get its input from this redirection.

2 - The while loop processes a line at a time. So while there is no more data after f6, if there is another line, it will read and process it. Once all the lines in the password (or other) file have been read, the while loop would then exit. Any commands after the while loop would then execute.
 
Thanks very much sbrews. I noticed the redirect in after I posted, but since it was on the done statement, it didn't make sense. I would have thought you would have had to redirect in first:))

Gallows
 
Thanks very much olded for the example and tutorial.
Much appreciated,

Gallows
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top