A possible solution for the countlogin.pl
program is as follows:
#! /usr/bin/perl # countlogin.pl - count the number of login processes for each user name # in the ps output saved in the file "psout" # # The "psout" file contains the output of the ps command. The # first line contains column headers. Each line after that contains # an entry for each process running on the machine. The first field # is the username (login id), the second field is the process id (PID), # and the last field is the command. You can determine that a process # is a login shell because it starts with a minus sign ("-"), followed # by the name of the shell. # # USER PID START TIME COMMAND # krish 396 Oct01 0:03 sshd: krish@pts/3 # krish 397 Oct01 0:00 -ksh # bedros 1299 Oct06 0:00 /bin/bash /g/home/pbs/torq... # # In this sample, only process # 397, which is owned by user "krish" # is a login shell. For this program, you can ignore all the other # processes which do not match the pattern for a login shell. # use strict; use warnings; use IO::File; # open the file "psout" for reading, $fh contains a filehandle # read the first line and ignore it (scalar prevents the read operation # from defaulting to reading the whole file as a list) # initialize the hash table you will use to count the logins my %count = (); # read the remainder of the file, line by line while (<$fh>) { next unless ( /\s-\w+$/ ); # skip if not a shell my ($login) = ( /^(\w+)\s/ ); # grab user name, first column $count{$login} = 0 # initialize hash entry if new $count{$login}++; # increment count for user } # close the file # print a list of the usernames and the process count, sorted by name { }