Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

getting the bash exit code

by xorl (Deacon)
on Aug 24, 2012 at 18:45 UTC ( [id://989610]=perlquestion: print w/replies, xml ) Need Help??

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

Yes there are a number of posts and sites that talk about using perl to run other scripts and to get the exit status. However I haven't seen any that explain the behavior I'm seeing and I'm just plain confused.

I have a bash script prints out some stuff and then at the end does an "exit 101". I want my perl script to capture both the content of what the script prints and the 101 value of the exit code.

So of course I tried:
myscript.sh

#!/bin/bash echo "foo" exit 101
my_perl_script.pl
my $cmd = "myscript.sh"; my $content = `$cmd`; my $exitcode = $?; print "Content: $content\n"; print "Exit Code: $exitcode\n";
Running my_perl_script.pl results in:
Content: foo
Exit Code: 25856

Now I have no idea how it got 25856 from 101 (well it does seem to be 101 times that magical 256 number, but I'm sure it isn't that simple).

In bash if I run myscript.sh and echo $? I get the expected 101. So something weird I think is happening with the backticks.

Replies are listed 'Best First'.
Re: getting the bash exit code
by Perlbotics (Archbishop) on Aug 24, 2012 at 18:54 UTC

    Now I have no idea how it got 25856 from 101 (well it does seem to be 101 times that magical 256 number, but I'm sure it isn't that simple).

    Yes, it's that simple. perlvar explains that $? is set like explained in system where the following snippet is copied from:

    Quote from system:

    if ($? == -1) { print "failed to execute: $!\n"; } elsif ($? & 127) { printf "child died with signal %d, %s coredump\n", ($? & 127), ($? & 128) ? 'with' : 'without'; } else { printf "child exited with value %d\n", $? >> 8; }
    HTH

    Update:

    Bit# 15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00 | | | | | | | | | | | | | | | | X7 X6 X5 X4 X3 X2 X1 X0 CD S6 S5 S4 S3 S2 S1 S0 | | | +-- exit code (8bit) | +-- signal code (7bit) | +-- core-dumped flag

    Update2 (in response to node below):

    So for quick and dirty every day use it sounds like I can just say something like: $exit_status = $?/255;
    No.
    my $exit_status = int( $? / 256 ); # quick and dirty exit-state # decimal arithmetic so to speak # (works for positive numbers)
    Use int() to get rid of potential fractional parts when the process terminated due to a signal or core-dump.
    The >>-operator does this in a single step.
    I might note, that I assumed a *NIX background. The Windows emulation might not cover the whole interface (exit-code plus signals). Other monks will know better...

    So X >> Y is most of the time int( X / (2**Y) ) for positive numbers.

    Here are some starters for binary arithmetic: wikibooks, Khan Academy - Binary Numbers, Slideshare - Binary Arithmetic, ... Search also MIT OpenCourseWare - I am sure, they have something in their repertoire.

      Thanks for the answer. I wish you had stopped at "Yes It's that simple".

      So for quick and dirty every day use it sounds like I can just say something like:
      $exit_status = $?/255;

      I've never understood what bitwise AND (the '&' operator) does. Plus I've never encountered the ">>" operator before (I looked it up it is called a shift operator but the explanation of what it does was way beyond me). From your answer I get the impression I'd need to go back to college and take some pretty advance math or maybe computer science classes to understand them.

Re: getting the bash exit code
by aitap (Curate) on Aug 24, 2012 at 18:56 UTC
    $?
    This is just the 16-bit status word returned by the traditional Unix "wait()" system call (or else is made up to look like it). Thus, the exit value of the subprocess is really ("$? >> 8")
    Sorry if my advice was wrong.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://989610]
Approved by Perlbotics
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (5)
As of 2024-03-29 00:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found