rahulruns has asked for the wisdom of the Perl Monks concerning the following question:

I have tried assigning value of SHELL command to PERL variable by using back ticks as well as qx{} but still I am not able to assign the output of the SHELL COMMAND to a PERL variable. I tried working with system and exec though knowing that they assign exit status. Nothing is working hence anyone who could help. You will see ssh in code and it is passwordless so not a problem. The SHELL COMMAND is working properly on command prompt The SHELL COMMAND basically tries to get the status of service on other node where the service would be running or stopped

my $status = qx{ssh r01mn1 /etc/init.d/hadoop-0.20-mapreduce-jobtracke +r status | awk '{print $3}'}; print "$status is status of service"; if ( "$status" eq "stopped" ) { print " JOBTRACKER SERVICE is not running"; } output [root@r01mgt ~]# perl testscript.pl jobtracker is stopped is status of service SHELL COMMAND [root@r01mgt ~]# ssh r01mn1 /etc/init.d/hadoop-0.20-mapreduce-jobtrack +er status | awk '{print $3}' stopped

Replies are listed 'Best First'.
Re: Assign value of SHELL COMMAND to a variable in PERL
by ambrus (Abbot) on Jul 12, 2012 at 07:08 UTC

    You must quote the dollar sign in the qx literal so that perl does not interpret it.

    Updates: The other mistake is that you don't remove the newline so "$status" eq "stopped" won't ever be true. But you should probably test for the service running anyway, not for it being stopped. And it might be easier to use a regex match instead of a string equality.

    I wouldn't use awk either. (It just causes unnecessary problems with quoting interacting with ssh here.) (No it doesn't, because the pipeline is not passed through ssh but ran in the local shell. Sorry.) Just run the init script and match the output with a perl regex, such as

    my $status = qx{ssh r01mn1 /etc/init.d/hadoop-0.20-mapreduce-jobtracke +r status}; if ( "$status" !~ /\brunning\b/ ) { print "JOBTRACKER SERVICE is not running\n"; }

    Btw, I kept running to interpolation mistakes with $1 when writing perl scripts driving gnuplot, because I'm both interpolating values computed in perl to the gnuplot commands and passing literal $1 thingies that gnuplot interprets as column number expressions.

      warn provides the output below

      warn provides the output as status=(((jobtracker is stopped ))) at tes +tscript.pl line 7. jobtracker is stopped is status of service
Re: Assign value of SHELL COMMAND to a variable in PERL
by frozenwithjoy (Priest) on Jul 12, 2012 at 06:42 UTC
    I suspect that the problem might be that the system call is resulting in output to STDERR, rather than STDOUT. To redirect, try adding 2>&1 like this:
    my $status = qx{ ssh r01mn1 /etc/init.d/hadoop-0.20-mapreduce-jobtrack +er status 2>&1 | awk '{print $3}'};

    Be sure to put it before the pipe, otherwise you won't be piping anything to awk. Did that help at all?

      Good guess, it's definitely worth a try, but it seems the Debian init scripts do write the status output to stdout.

      The output is same even after change

      output [root@r01mgt ~]# perl testscript.pl jobtracker is stopped is status of service
        Out of curiosity, what do you get w/o the pipe to awk?
      May be this is a more complicated way, but works with me:
      my $lcmd="pwd"; open (INPUT,"($lcmd) 2>&1 |"); @ltext = <INPUT>; close (INPUT); my $result=$ltext[0]; print("Output of pwd: $result");

      *Jes