3 Replies Latest reply: Dec 15, 2013 8:24 AM by Amit Shil RSS

    awk problem -- while loop quirkiness

    Amit Shil

      Hi All,

       

      I am new to awk, have been working on a very simple script with an objective of reading the /etc/password file from the system and then using the who command to match and display the login time & date using awk.

      Question - The while loop in script is something that am not able to work correctly, it just executes once for all the records of /tmp/who and then doesn't enter again!! .. if i just execute the below, here's the output.

       


      OUTPUT - it does OK for the first iteration (for i=0) but then it goes off to a endless loop!! ... the contents of /tmp/who is below, can somebody explain this behavior and suggest the resolution please?


      [root@Gateway awk]# cat /tmp/who

      testuser:pts/2:2013-12-15:0142:(192.168.25.1)

      root:pts/1:2013-12-15:0132:(192.168.25.1)

      root:pts/0:2013-12-15:0132:(192.168.25.1)


      [root@Gateway awk]# awk -f userreport.awk -F: /etc/passwd

       

      Contents of /etc/passwd file dumped using awk

      ---------------------------------------------

      Username        UID     GID     HomeDir                 Shell                   Latest Login Date/Time

      1

      outside while $1-name -- root*root

      inside first while==bin&root

      inside first while==daemon&root

      inside first while==adm&root

      inside first while==lp&root

      inside first while==sync&root

      inside first while==shutdown&root

      inside first while==halt&root

      inside first while==mail&root

      inside first while==uucp&root

      inside first while==operator&root

      inside first while==games&root

      inside first while==gopher&root

      inside first while==ftp&root

      inside first while==nobody&root

      inside first while==dbus&root

      inside first while==vcsa&root

      inside first while==rpc&root

      inside first while==abrt&root

      inside first while==nscd&root

      inside first while==tcpdump&root

      inside first while==oprofile&root

      inside first while==haldaemon&root

      inside first while==nslcd&root

      inside first while==saslauth&root

      inside first while==postfix&root

      inside first while==ntp&root

      inside first while==rpcuser&root

      inside first while==nfsnobody&root

      inside first while==sshd&root

      inside first while==avahi-autoipd&root

      inside first while==mysql&root

      inside first while==apache&root

      inside first while==cacti&root

      inside first while==nagios&root

      inside first while==192.168.25.3$&root

      inside first while==testuser&root

      inside first while==testuser&root

      inside first while==testuser&root

      inside first while==testuser&root

       


       

      Script below -

       

       

      [root@Gateway awk]# vi userreport.awk

            1 BEGIN {

            2

            3         system("clear")

            4

            5         printf "\nContents of /etc/passwd file dumped using awk \n"

            6         printf "---------------------------------------------\n"

            7         printf "Username\tUID\tGID\tHomeDir\t\t\tShell\t\t\tLatest Login Date/Time\n"

            8         system("who | tr -s ' ' | sed 's/://g' | tr ' ' : | sort -t: -k4 -r > /tmp/who ")

            9

           10 i=0

           11 }

           12 {

           13

           14 i++

           15 print i

           16

           17         #printf "\n%-10s\t%d\t%d\t%-20s\t%-10s",$1,$3,$4,$6,$7

           18

           19         name=$1

           20         print "outside while $1-name -- " $1 "*"  name

           21                while ( getline "/tmp/who" ) {

           22                         print "inside first while=="$1 "&" name

           23         #               if($1 == name)

           24         #                       print "inside IF=="$1 "&" name

           25         #                       printf "\t\t%-10s %-10s",$3,$4

           26         #               print "IF loop $1-name -- " $1 "*"  name

           27         #               else

           28         #                       print "Inside Else=="$1 "&" name

           29         #                       printf "***Not Logged in***"

           30                 }

           31                 close("/tmp/who")

           32         #}

           33

           34 }

           35

           36

           37

           38 END {

           39         printf "\n---------------------------------------------\n"

           40         printf "End of Report\n"

           41 }

        • 1. Re: awk problem -- while loop quirkiness
          Dude!

          I’m not really sure what you are trying to accomplish. Please explain what output you are expecting.

           

          Actually I would rather avoid any complex use of awk and think that using awk for the purpose of parsing the password file is somewhat awkward.

           

          Perhaps you will find the following useful:

          https://forums.oracle.com/message/11275698

          • 2. Re: awk problem -- while loop quirkiness
            Amit Shil

            Hi Dude,

             

            Just want to publish the login/date time of the genuine users from /etc/passwd (whosoever is permitted to login), Something like the below. This works only for one iteration but messes up for multiple users due to the while issue described in this post..

             

            [root@Gateway awk]# awk -f userreport.awk -F: /etc/passwd

             

            Contents of /etc/passwd file dumped using awk

            ---------------------------------------------

            Username        UID     GID     HomeDir                 Shell                   Latest Login Date/Time

             

            root            0       0       /root                   /bin/bash               2013-12-15 0432                 2013-12-15 0424

            bin             1       1       /bin                    /sbin/nologin

            daemon          2       2       /sbin                   /sbin/nologin

            adm             3       4       /var/adm                /sbin/nologin

            lp              4       7       /var/spool/lpd          /sbin/nologin

            sync            5       0       /sbin                   /bin/sync

            shutdown        6       0       /sbin                   /sbin/shutdown

            halt            7       0       /sbin                   /sbin/halt

            mail            8       12      /var/spool/mail         /sbin/nologin

            uucp            10      14      /var/spool/uucp         /sbin/nologin

            operator        11      0       /root                   /sbin/nologin

            games           12      100     /usr/games              /sbin/nologin

            gopher          13      30      /var/gopher             /sbin/nologin

            ftp             14      50      /var/ftp                /sbin/nologin

            nobody          99      99      /                       /sbin/nologin

            dbus            81      81      /                       /sbin/nologin

            vcsa            69      69      /dev                    /sbin/nologin

            rpc             32      32      /var/cache/rpcbind      /sbin/nologin

            abrt            499     499     /etc/abrt               /sbin/nologin

            nscd            28      28      /                       /sbin/nologin

            tcpdump         72      72      /                       /sbin/nologin

            oprofile        16      16      /home/oprofile          /sbin/nologin

            haldaemon       68      68      /                       /sbin/nologin

            nslcd           65      55      /                       /sbin/nologin

            saslauth        498     498     /var/empty/saslauth     /sbin/nologin

            postfix         89      89      /var/spool/postfix      /sbin/nologin

            ntp             38      38      /etc/ntp                /sbin/nologin

            rpcuser         29      29      /var/lib/nfs            /sbin/nologin

            nfsnobody       65534   65534   /var/lib/nfs            /sbin/nologin

            sshd            74      74      /var/empty/sshd         /sbin/nologin

            avahi-autoipd   170     170     /var/lib/avahi-autoipd  /sbin/nologin

            mysql           27      27      /var/lib/mysql          /bin/bash

            apache          48      48      /var/www                /sbin/nologin

            cacti           497     495     /var/www/cacti          /bin/sh

            nagios          500     501     /home/nagios            /bin/bash

            192.168.25.3$   501     701     /dev/null               /bin/false

            testuser        502     702     /home/testuser          /bin/bash

            ---------------------------------------------

            End of Report

            • 3. Re: awk problem -- while loop quirkiness
              Amit Shil

              Hi, not sure what i did but it seems to be working now!! .. the script and o/p below, will close this thread now as answered. Thanks

               

              [root@Gateway awk]# vi userreport.awk

                    1 BEGIN {

                    2

                    3         system("clear")

                    4

                    5         printf "\nContents of /etc/passwd file dumped using awk \n"

                    6         printf "---------------------------------------------\n"

                    7         printf "Username\tUID\tGID\tHomeDir\t\t\tShell\t\t\tLatest Login Date/Time\n"

                    8         system("who | tr -s ' ' | sed 's/://g' | tr ' ' : | sort -t: -k4 -r > /tmp/who ")

                    9

                   10 #i=0

                   11 }

                   12 {

                   13

                   14 #i++

                   15 #print i

                   16

                   17         printf "\n%-10s\t%d\t%d\t%-20s\t%-10s",$1,$3,$4,$6,$7

                   18

                   19         name=$1

                   20         #print "outside while $1-name -- " $1 "*"  name

                   21                 while ( getline < "/tmp/who" > 0) {

                   22         #               print "inside first while=="$1 "&" name

                   23                         if($1 == name)

                   24         #                       print "inside IF=="$1 "&" name

                   25                                 printf "\t\t%-10s %-10s",$3,$4

                   26         #               else

                   27         #                       print "Inside Else=="$1 "&" name

                   28         #                       printf "***Not Logged in***"

                   29                 }

                   30                 close("/tmp/who")

                   31

                   32 }

                   33

                   34

                   35

                   36 END {

                   37         printf "\n---------------------------------------------\n"

                   38         printf "End of Report\n"

                   39 }

               

              [root@Gateway awk]# awk -f userreport.awk -F: /etc/passwd

               

              Contents of /etc/passwd file dumped using awk

              ---------------------------------------------

              Username        UID     GID     HomeDir                 Shell                   Latest Login Date/Time

               

              root            0       0       /root                   /bin/bash               2013-12-15 0432                 2013-12-15 0424

              bin             1       1       /bin                    /sbin/nologin

              daemon          2       2       /sbin                   /sbin/nologin

              adm             3       4       /var/adm                /sbin/nologin

              lp              4       7       /var/spool/lpd          /sbin/nologin

              sync            5       0       /sbin                   /bin/sync

              shutdown        6       0       /sbin                   /sbin/shutdown

              halt            7       0       /sbin                   /sbin/halt

              mail            8       12      /var/spool/mail         /sbin/nologin

              uucp            10      14      /var/spool/uucp         /sbin/nologin

              operator        11      0       /root                   /sbin/nologin

              games           12      100     /usr/games              /sbin/nologin

              gopher          13      30      /var/gopher             /sbin/nologin

              ftp             14      50      /var/ftp                /sbin/nologin

              nobody          99      99      /                       /sbin/nologin

              dbus            81      81      /                       /sbin/nologin

              vcsa            69      69      /dev                    /sbin/nologin

              rpc             32      32      /var/cache/rpcbind      /sbin/nologin

              abrt            499     499     /etc/abrt               /sbin/nologin

              nscd            28      28      /                       /sbin/nologin

              tcpdump         72      72      /                       /sbin/nologin

              oprofile        16      16      /home/oprofile          /sbin/nologin

              haldaemon       68      68      /                       /sbin/nologin

              nslcd           65      55      /                       /sbin/nologin

              saslauth        498     498     /var/empty/saslauth     /sbin/nologin

              postfix         89      89      /var/spool/postfix      /sbin/nologin

              ntp             38      38      /etc/ntp                /sbin/nologin

              rpcuser         29      29      /var/lib/nfs            /sbin/nologin

              nfsnobody       65534   65534   /var/lib/nfs            /sbin/nologin

              sshd            74      74      /var/empty/sshd         /sbin/nologin

              avahi-autoipd   170     170     /var/lib/avahi-autoipd  /sbin/nologin

              mysql           27      27      /var/lib/mysql          /bin/bash

              apache          48      48      /var/www                /sbin/nologin

              cacti           497     495     /var/www/cacti          /bin/sh

              nagios          500     501     /home/nagios            /bin/bash

              192.168.25.3$   501     701     /dev/null               /bin/false

              testuser        502     702     /home/testuser          /bin/bash               2013-12-15 0440

              ---------------------------------------------

              End of Report