A perl function that runs a Unix command and returns its exit status

A perl function that runs a Unix command and returns its exit status

Here's the source code for a Perl function I created this morning. The purpose of this function/method is to run a specific Unix command, and then return the Unix exit status of that command. As usual with Unix and Linux systems, a zero exit status indicates success, and a non-zero exit status indicates some type of problem.

Without any further ado, here's the source code for my Perl function:

#
# a function to run a unix/linux command and return the
# exit status of that command.
#
sub do_command
{
  my($arg) = shift;
  system("ps -ef | grep -v grep | grep $arg > /dev/null 2>&1");
  return $? >> 8;
}

#
# an example of how you call this function
#
print &do_command('foo');

If you're comfortable with Linux and Perl, possibly the only tricky part of this source code is this line:

return $? >> 8;

As it's described on the system documentation page, you have to "shift right by eight" to get to the usual Unix error status. To be specific, the system command documentation says "The return value is the exit status of the program as returned by the wait call. To get the actual exit value, shift right by eight."

As a final note, when I call my method I'm passing in a string named foo, which the method then stores in the local variable named $arg. This variable is then used in the Unix command that I run with the Perl system function.