Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

How to run a shell script from a Perl program?

by calsaint (Initiate)
on Aug 24, 2011 at 18:25 UTC ( [id://922164]=perlquestion: print w/replies, xml ) Need Help??

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

This is rather another question, I am trying call abc.sh from a perl program which has this: ---
echo enter your name: read a echo enter your last name read b echo your name is $a echo your last name is $b
--- how do I achieve this. Cant change abc.sh though

Replies are listed 'Best First'.
Re: How to run a shell script from a Perl program?
by toolic (Bishop) on Aug 24, 2011 at 18:39 UTC
Re: How to run a shell script from a Perl program?
by ww (Archbishop) on Aug 24, 2011 at 20:43 UTC
    "Cant change abc.sh though"

    Wrong attitude, unless you have some extraordinary justification for taking that stance; one which you haven't stated.

    Your shell script does nothing that Perl can't do as easily and for most purposes, better.

    Worse, this looks suspiciously like homework that wasn't so-labeled.

      A slightly more complex example

      use warnings; use strict; open(ABC, "echo How to run a shell script from a Perl program |./abc.s +h & 2>1|"); print <ABC>; close ABC;
      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

        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

        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.

Re: How to run a shell script from a Perl program?
by perl.j (Pilgrim) on Aug 24, 2011 at 19:23 UTC

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2024-04-24 02:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found