ksh while read loop breaks after one record - AIX

#!/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
${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.

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

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

Thank you!

1 Like