perlreturn-valueexit-codersh

Accessing exit code of running a remote command through rsh, using Perl's qx


I'm using qx() to run a command on a remote windows machine through rsh. I need to access the exit code of the remote command. I followed the instructions here "Get return code and output from command in Perl", but using $? always returns 0 - seems like it is the exit code of the rsh command instead of the command run through rsh.

However when I use ssh, then $? actually returns the exit code of the command run through ssh.

So, how can I access the return value of the command run through rsh on a remote windows machine using qx ?

qx(rsh -l $username $host perl a.pl);     # say I run a perl script on remote machine 
my $returnValue =                         # need the return value of 'perl a.pl' here

Solution

  • Here is a workaround (in the case you cannot use ssh) that saves the exit code in a temp file:

    my $output = qx(rsh -l $username $host "perl a.pl; echo \\\$? > exitcode.txt");
    my $exitcode = qx(rsh -l $username $host "cat exitcode.txt");