CLOSE

A possible solution for the onlyshell.pl program is as follows:

  1. #! /usr/bin/perl
  2.  
  3. # onlyshell.pl - read from "psout", print first line, and only lines
  4. # which represent shell processes. A shell is a process in which
  5. # the COMMAND field (last field in "ps" output) is a
  6. # "csh" or "bash", which has a minus sign before it:
  7. #
  8. # kjacks 21255 Sep30 0:00 -bash
  9. #
  10. # but not:
  11. #
  12. # kjacks 24536 11:47 0:00 bash
  13.  
  14. use warnings;
  15. use strict;
  16.  
  17. my $filename = 'psout';
  18.  
  19. # open $filename for reading
  20. open PS, "<$filename"
  21. or die "open($filename) $!";
  22.  
  23. # read first line and print it
  24. my $line = <PS>;
  25. print $line;
  26.  
  27. # read remaining lines and print only those which match the shell pattern
  28. while (<PS>)
  29. {
  30. if ( m/\s-(bash|csh)$/ );
  31. }
  32.  
  33. # close the file
  34. close PS;
  35.  

CLOSE