Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: shell script via perl (clarification)

by reasonablekeith (Deacon)
on Dec 07, 2005 at 10:48 UTC ( [id://514792]=note: print w/replies, xml ) Need Help??


in reply to shell script via perl (clarification)

You can run a command in perl using (for example) backticks or the system function.

Both of these will invoke a new shell, which will run in a new child process under the process currently running your perl script. The child will inherit any enviroment variables set by your perl script, but any enviroment varaibles set by the child will be lost when the child ends.

you can check the return code of the command in perl using $?.

system("./some_command_that_might_set_ENVs.sh"); if ($? == -1) { print "failed to execute: $!\n"; }
---
my name's not Keith, and I'm not reasonable.

Replies are listed 'Best First'.
Re^2: shell script via perl (clarification)
by serf (Chaplain) on Dec 07, 2005 at 12:05 UTC
    "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; }
Re^2: shell script via perl (clarification)
by Anonymous Monk on Feb 16, 2016 at 21:22 UTC
    Is there any possible way, where i can use environment variable set by child shell script in parent PERL script?

      Short version: no, but do read the FAQ.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2024-04-18 19:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found