ksh while read loop breaks after one record - AIX


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting ksh while read loop breaks after one record - AIX
# 1  
Old 10-21-2013
ksh while read loop breaks after one record - AIX

Code:
#!/bin/ksh
for SRV in imawasp01 \
imawasp02 \
imawasp03 \
imawasp04 \
imawasp05 \
imawasp06 \
imawasp07 \
imawasp08 \
imawasp09
do
  print "${SRV}"
  while read PASSLINE
  do
      SRVNAME=`echo ${PASSLINE} | awk -F\: '{print $1}'`
      LASTLOGIN=`ssh ${SRV} lsuser ${SRVNAME} | tr ' ' '\n' | awk -F= '$1 ~ /time_last_login/ {print $2}'`
      LASTFAILEDLOGIN=`ssh ${SRV} lsuser ${SRVNAME} | tr ' ' '\n' | awk -F= '$1 ~ /time_last_unsuccessful_login/ {print $2}'`
      echo "${SRVNAME},${LASTLOGIN},${LASTFAILEDLOGIN}" >> /tmp/${SRV}_logintimes.txt
  done < ${SRV}_passwd.txt
  print "=================="
done

Code:
${SRV}_passwd.txt

is a local copy of /etc/password file from each server SRV

Setting LASTLOGIN or LASTFAILEDLOGIN causes that inner do loop to break out to the next server after reading just the first line from the password file. If I comment out those two lines the do loop will cycle for every line in the password file.

I don't understand what is causing this behavior.

Thank you for your help.
# 2  
Old 10-21-2013
It is because of ssh. It tries to read input from standard input -- which happens to be "${SRV}_passwd.txt" in this case, eating all your input in one go and breaking the loop.

To make it not do that, ssh -n ... or ssh ... </dev/null

Also, you don't need awk to split upon : here

Code:
while IFS=":" read SRVNAME RESTOFLINE
do
...
done < inputfile

This User Gave Thanks to Corona688 For This Post:
# 3  
Old 10-21-2013
Thank you!
This User Gave Thanks to port43 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read file input in while loop does not work on AIX system

I'm working on Aix 6.1 and using ksh shell. The below works fine on Linux bash or ksh shell . while IFS= read -r dirpath ; do echo "Hi" done <<<"$var" However, any such while loop that reads the input from file or variable using <<< fails on Aix system with the below error: Below... (2 Replies)
Discussion started by: mohtashims
2 Replies

2. Shell Programming and Scripting

ksh loop to read input until QUIT

Hi I'm looking to write a simple ksh loop reading user input (and write it to a file) until the user enters QUIT at which point I want it to continue. Does anyone have an example of this type of loop? Any help much appreciated Cheers (2 Replies)
Discussion started by: Grueben
2 Replies

3. UNIX for Dummies Questions & Answers

GREP function in ksh which ignores LINE Breaks

Hello I am using a grep command with two patterns in my KSH script. File has line breaks in it and both the patterns are in different lines. Here is the command grep -l 'RITE AID.*ST.820' natriter820u.20140914 Pattern1 - RITE AID Pattern2 - ST*820 I am not getting any results from... (24 Replies)
Discussion started by: Raghav Garg
24 Replies

4. How to Post in the The UNIX and Linux Forums

GREP function in ksh which ignores LINE Breaks

I am using a grep command with two patterns in my KSH script. File has line breaks in it and both the patterns are in different lines. Here is the command - grep -l 'RITE AID.*ST.820' natriter820u.20140914 Pattern1 - RITE AID Pattern2 - ST*820 I am not getting any results from this,... (3 Replies)
Discussion started by: Raghav Garg
3 Replies

5. AIX

Loop breaks through ssh in script

hello all, i have an AIX6.1 machine and INFORMIX 11.7 database server. i have a script to create users on 3 machines and also i need to grant this user access to a specific database. the script works and it does what i want it to do but the loop doesnt work. for example if i insert 10 lines in... (5 Replies)
Discussion started by: omonoiatis9
5 Replies

6. Shell Programming and Scripting

Update file record inside read loop

Hi, I am reading file records inside a while loop, and want to update the record when certain condition is met. How can I update a file while being read? I want to avoid using temporary files, copy, rename, ... while IFS=',' read -r f1 f2 do function(f1,f2) if then <add... (1 Reply)
Discussion started by: ysrini
1 Replies

7. Shell Programming and Scripting

Unable to read the first space of a record in while loop

I have a loop like while read i do echo "$i" . . . done < tms.txt The tms.txt contians data like 2008-02-03 00:00:00 <space>00:00:00 . . . 2010-02-03 10:54:32 (2 Replies)
Discussion started by: machomaddy
2 Replies

8. Shell Programming and Scripting

Aix .ksh for loop script.

Hi, I'm trying to write a for loop to run through a list of servers and for each server copy a file to a backup file. But I can't seem to get it to run through my server list. It work for individual servers, please see below. #!/bin/ksh SSH_USERID=khcuser webservers="server1 server2" ... (2 Replies)
Discussion started by: elmesy
2 Replies

9. Shell Programming and Scripting

ssh breaks loop

Here is the smallest extract to demonstrate the problem that I experience. #!/bin/bash r=$1 while read ip do if ] ;then x=`ssh $ip echo "$ip"` else x=`echo "$ip"` fi echo $x done << EOF 192.168.8.241 192.168.8.241 EOF # Any IP with public key set (0 Replies)
Discussion started by: ivolvo
0 Replies

10. UNIX for Advanced & Expert Users

"while read ..." loop exiting after reading only one record

Greeting, The following script completes after reading only one record from the input file that contains many records. I commented out the "ssh" and get what I expect, an echo of all the records in the input.txt file. Is ssh killing the file handle? On the box "uname -a" gives "SunOS... (2 Replies)
Discussion started by: twk
2 Replies
Login or Register to Ask a Question