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

HI , I am running a perl script which will login and execute the commands on the host.After executing each command i want to find out whether the command execute is sucess or not..I am not able to find it! Below is the code..
#!/usr/local/bin/perl use Net::SSH::Expect; my $ssh = Net::SSH::Expect->new ( host => "$host", user => 'xxxx', timeout=>7, raw_pty => 1 ); $password="yyyyy"; Login in to the host ------------------------- $prompt="[Pp]assword"; $ssh->run_ssh(); #Added newly $ssh->waitfor('qr/\(yes\/no\)\?$/',2); $ssh->send("yes\n"); $ssh->waitfor('qr/$prompt:\s*$/',5); $ssh->send("$password"); Executing commands on host -------------------------------- $cmd="ls"; my $output=$ssh->exec($cmd,"5") or die "Error\n"; eval { $mov=$ssh->exec("echo $?\n","2"); }; warn $@ if $@; print "value is $mov\n"; BY default this value is 0,even if we execute + the invalid commands. $ssh->close();
Thanks, Bala

Replies are listed 'Best First'.
Re: echo $? in perl
by Corion (Patriarch) on Nov 30, 2007 at 20:19 UTC

    Last time you asked this question in the CB, a long part of back and forth was spent on the difference between

    print "echo $?"; # $ssh->exec("echo $?\n");

    and

    print 'echo $?'; # $ssh->exec('echo $?' . "\n")

    You seem to have misunderstood the point that bart and I were trying to make. There are differences in Perl between strings in double quotes (") and strings in single quotes (''). To get a formal description of these differences, see perlop about "Quotes and Quotelike Operators".

    Also during the last discussion, you claimed that this second approach did not work either. Maybe it does now for ou, but if it still does not, there was mention of things you could try next:

    First approach - execute both commands in one ->exec statement:

    $ssh->exec('your-program; echo $?');

    Second approach - put all statements to execute in a shell script and execute that:

    $ssh->exec('./shellscript-that-runs-your-program-and-then-outputs-doll +ar-question-mark.sh'); # where shellscript-that-runs-your-program-and-then-outputs-dollar-que +stion-mark.sh # contains the lines: # # #!/bin/sh # your-program # echo $?
      Hi Corion, Thanks! $ssh->exec('echo $?' . "\n"); perfectly working for me.. Thanks a lot! Regards, Bala
Re: echo $? in perl
by roboticus (Chancellor) on Nov 30, 2007 at 20:59 UTC
    bala_999:

    As you were told repeatedly in the CB yesterday, your problem is that you insist on persist in using:

    eval { $mov=$ssh->exec("echo $?\n","2"); }; warn $@ if $@;
    instead of:
    eval { $mov=$ssh->exec('echo $?\n',"2"); }; warn $@ if $@;
    In the first, perl is evaluating the $? for you, so the remote machine sees something utterly different. It's seeing 'echo 0\n' instead of 'echo $?\n'. So of course you're always going to get a zero result...

    ...roboticus

    Update: Upon rereading, the tone was harsher than intended.