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.


In reply to getting the bash exit code by xorl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.