March 2

Something perl

You know what I’m really sick of?  Books which purport to teach the reader something quickly about Perl which is useful, but doesn’t actually teach you how to do something useful at any sort of rapid pace.

This post attempts to break that cycle.

This example has no practical purpose, but does show how to run a system command and capture the output, then use that output as input to another command.  Because one of the handiest things I’ve used Perl for and one of the most common functions of any sysadmin, is to run a system command and store the output into an array so I can do something useful with that output.

Let’s set aside the fact that it’s probably not a good idea to bundle shell awk commands inside of Perl, let’s just go ahead and show you how to do something useful.

#!/usr/bin/perl -w

# Run a system command and build an array out the result.
# Do something with every item in that array and store the
# result in another arry.

# Get all my process ID numbers and put them into an array called @pids.
my @pids = `ps -ef|grep kate|awk ‘{print \$2}’`;  # I had to escape the $ in the awk command or Perl will
# try to interpret it. (\$2)
# For every item in that array
for $pid (@pids) {
# Take off the trailing newline character (\n)
chomp ($pid);
# Use each item as a component of another command
# and store the output from that command into another array.
@pidlist = `ps -ef|grep $pid|egrep -v grep|wc -l`;
# For every item in that array, run another command
# and store the output into yet another array.
for $linecount (@pidlist) {
chomp ($linecount);
# Then show us some information
print “My process $pid has $linecount line(s).\n”;

What the above code does, is:

run: ps -ef|grep kate

Which produces output similar to:

[kate@concord scripts]$ ps -ef|grep kate
kate      2691     1  0 Feb22 ?        00:00:00 /usr/bin/gnome-keyring-daemon –daemonize –login
kate      2701  2684  0 Feb22 ?        00:00:00 gnome-session

(output truncated)

The process ID associated with my gnome-session is 2701.  So I want to filter the ps command so I get just that number.  I do that by piping the output of my grep command into awk so I can get just the PID associated with my tasks.  The PID is the 2nd column in the output so I’ve asked awk to display only the 2nd field. ($2)

When this command executes, it will take every PID associated with my ID and stack them up into an array called @pids.

The next step takes each PID back out of the array and runs another command with it as a component.  This part of the command runs another process search but now instead of searching for ‘kate’, it’s searching for each of the process ID’s stored in the array @pids.

ps -ef|grep $pid

(The rest of the command, |egrep -v grep eliminates the grep command itself from the search, and wc -l counts the number of lines of output returned.)

The output from this command is stored in another array called @pidlist.

Finally, we take the items back out of @pidlist and use them to display some information to the screen.

You’re welcome.

Copyright © 2016 Katherine Loux. All rights reserved.

Posted March 2, 2015 by Kate in category "GeekStuff