Loading ...
Sorry, an error occurred while loading the content.
 

Re: Capturing output of a command

Expand Messages
  • daymobrew@yahoo.com
    ... # Redirect error output to /dev/null. $ping = `ping -c 2 10.11.11.100 2 /dev/null`; # $? is return code of last command. if ( $? != 0 ){ print SUCCESS n ;
    Message 1 of 2 , Nov 24, 2004
      --- In perl-beginner@yahoogroups.com, <lakshmi.sailaja@w...> wrote:
      > Hello,
      >
      > I need to execute ping command and display if success or failure. Hence
      > I use Backquotes to capture the output of ping command and check its
      > return value and display if success or not.
      >
      > I am using a Linux machine. I have written the following code snippet:
      >
      > ############################################
      >
      > $ping = `ping -c 2 10.11.11.100`;
      >
      > $ping_ret = `echo $?`;
      >
      > if ($ping_ret){
      > print "SUCCESS\n";
      > }else{
      > print "FAILURE\n";
      > }
      >
      > ############################################
      >
      > The above script works fine, when the ping command is successful but if
      > I give any invalid IP address, the error message generated by the ping
      > command is displayed on the screen.
      >
      > Hence, can anyone please suggest me how I can avoid displaying the error
      > message generated by ping command if it fails?
      >
      > Thanks for your time and response.

      # Redirect error output to /dev/null.
      $ping = `ping -c 2 10.11.11.100 2>/dev/null`;

      # $? is return code of last command.
      if ( $? != 0 ){
      print "SUCCESS\n";
      }else{
      print "FAILURE\n";
      }

      You might want to consider the standard module Net::Ping.
      Some info at:
      perldoc Net::Ping
      or
      http://www.cpan.org/modules/by-module/Net/Net-Ping-2.10.readme
    Your message has been successfully submitted and would be delivered to recipients shortly.