Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re^2: How to run a shell script from a Perl program?

by calsaint (Initiate)
on Aug 24, 2011 at 21:24 UTC ( #922200=note: print w/ replies, xml ) Need Help??


in reply to Re: How to run a shell script from a Perl program?
in thread How to run a shell script from a Perl program?

its a vendor provided script which we are trying to stick into a a series of other things we are doing along with it.
sorry I should have given those details...

#!/usr/local/bin/perl use IPC::Run3; my $cmd = qq(/oracle/pieces/abc.sh); my @params = ('y','y','y'); my $params = join "\n", @in; run3([$cmd], \$params)

This has worked for me quite well.
I see a problem when I tested with IPC::Run3.
it appears that its not installed by default on all machines and I cant go about installing IPC::Run3.
appreciate if you could share any other method to do this


Comment on Re^2: How to run a shell script from a Perl program?
Download Code
Re^3: How to run a shell script from a Perl program?
by armstd (Friar) on Aug 24, 2011 at 23:50 UTC

    You can install and distribute IPC::Run3 along with your app instead of with each host's Perl distribution. See the "PREFIX and LIB attribute" section here: ExtUtils::MakeMaker

    --Dave

Re^3: How to run a shell script from a Perl program?
by juster (Friar) on Aug 25, 2011 at 13:58 UTC

    You might try using IPC::Open3 or IPC::Open2 which are core perl modules and have the same gist as IPC::Run3.

    If you only care about sending input to the shell script, which seems to be your intention, then you don't really need any of those modules. You can just use a piped open process. Piped processes have the disadvantage of only being able to read process output or send processes input, not both at once. Hence the need for the above modules. Sometimes that doesn't matter though...

    #!/usr/bin/env perl use warnings; # you forgot use strict; # these two babies open my $abc, '|-', '/oracle/pieces/abc.sh' or die "open: $!"; print $abc "y\n" for 1 .. 3; close $abc or die "close: $!"; # You probably want to check to make sure abc did not fail, even if yo +u use # modules to start the process for you. if ($? != 0) { if ($? & 127) { printf STDERR "abc died with signal %d\n", $? & 12 +7 } else { printf STDERR "abc exited with error code %d\n", $? >> 8 } exit 1; }

    This will print the output of abc to STDOUT. If you don't want this, you can add a pipe to devnull (>/dev/null) after the command.

    edit: After posting this I remembered there is also the unix "yes" command, which echoes "y" forever. You pipe its output to the program of choice. For example on the shell: yes | /oracle/pieces/abc.sh. Perl might be unnecessary unless your needs are more sophisticated.

    another edit: I had 0x127 in the code instead of 127. Not good.

      Thanks juster. Looks like this fits the bill perfectly. Appreciate your help. let me implement and let you know...

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2014-07-30 03:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (229 votes), past polls