"The perl script does not need to know what env variables are being set, only that the shell script ran."

If you need to know that it ran then don't go for backticks... but reasonablekeith has correctly offered here the answer to the bit you said you needed - the exit code of the script - which you can control with the exit command.

#!/bin/sh # # shell_script.sh # MY_VAR="something" ./simulated_binary_executable.pl echo "Returned $?" echo "Exporting MY_VAR" export MY_VAR ./simulated_binary_executable.pl echo "Returned $?" exit 0
and (to simulate the binary you are calling from the shell script) try.pl like this
#!/usr/bin/perl # # simulated_binary_executable.pl # use strict; use warnings; if ( $ENV{MY_VAR} ) { print "MY_VAR is '$ENV{MY_VAR}'\n"; exit 0; } else { print "MY_VAR is not set\n"; exit 1; }
Wrapping this in a dummy of your outer perl script:
#!/usr/bin/perl # # Dummy of outer perl script that is calling shell script # use strict; use warnings; # You could use $? later instead of defining $return my $return = system("./shell_script.sh"); if ($return == -1) { print "failed to execute: $!\n"; } elsif ($return & 127) { printf "child died with signal %d, %s coredump\n", ($return & 127), ($return & 128) ? 'with' : 'without'; } else { printf "child exited with value %d\n", $return >> 8; }
Or rather doing away with the intermediate shell script and setting the variables it would have set from within perl:
#!/usr/bin/perl # # Dummy of outer perl script # This time calling the binary without shell wrapper script # use strict; use warnings; # # Export the environment variable for the child binary # directly from Perl # $ENV{MY_VAR} = "something else"; my $binary = "./simulated_binary_executable.pl"; system($binary); # Run the external program if ($? == -1) { print "Child '$binary' failed to execute: $!\n"; } elsif ($? & 127) { printf "Child '%s' died with signal %d, %s coredump\n", $binary, ($? & 127), ($? & 128) ? 'with' : 'without'; } elsif ($? == 0) { print "Child '%s' exited successfully.\n"; } else { printf "Child '%s' exited with value %d\n", $binary, $? >> 8; }

In reply to Re^2: shell script via perl (clarification) by serf
in thread shell script via perl (clarification) by toronto75

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.