Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

How to run bash file from perl

by bndgyawali (Initiate)
on May 17, 2011 at 14:30 UTC ( [id://905283]=perlquestion: print w/replies, xml ) Need Help??

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

Hi friends,

I need to run a bash file like learn.sh and pass some arguments for it. I need to call the execution from within the perl script. Please let me know how can I do the execution of it from perl script.

Thanks in advance.
Binod

Replies are listed 'Best First'.
Re: How to run bash file from perl
by anonymized user 468275 (Curate) on May 17, 2011 at 15:02 UTC
    There are plenty of ways to do this. The easiest if errors and warnings can be ruled out and the output is small is the backtick operator which is also a similar operator in bash:
    my $output = `thing.sh args`;
    If output is extensive, but nevertheless nothing is expected to be printed to *nix channel 2, a pipe can be opened:
    my $pid = open my $ph, "thing.sh args |" or die $!; while ( <$ph> ) { # process output from thing.sh } close $ph; waitpid $pid, 0;
    If errors and output need separate processing (otherwise can append 2>&1 to the command in option 2), see IPC::Run3 or IPC::Open3, the latter of which takes three filehandles for channels 0 thru 2, the 0 channel needing therefore undef() instead of a filehandle, but otherwise is similar to option 2 above. Finally, if no I/O system communication is required with the process, just pass the commandline to be executed in bash (in this case the autoinvocation with arguments" to the "system" function as a single argument.

    One world, one people

Re: How to run bash file from perl
by ciderpunx (Vicar) on May 17, 2011 at 15:39 UTC
    Just to add another couple of options to anonymized user 468275's post. You can do this, if you don't care about the output from learn.sh:
    @args = ("learn.sh", "arg1", "arg2"); system(@args) == 0 or die "system @args failed: $?"
    Whereas this does much the same as using backticks (``)
    my $output = qx/learn.sh arg1 arg2/


      my $afile='dir1/firstfile.txt'; my $bfile='dir2/secondfile.txt'; my $bash_command='learn.sh '.$afile.' '.$bfile.' |'; open(my $output,$bash_command) or die $!; while(my $line=<$output>){ print "$line"; }
      my $afile='dir1/firstfile.txt'; my $bfile='dir2/secondfile.txt'; @args = ("learn.sh", $afile, $bfile); system(@args) == 0 or die "system @args failed: $?"
      my $output=`learn.sh $afile $bfile`;
      I tried three different ways as you all have suggested, but I couldn't make my script work. Please help me what is wrong in it

      Binod
Re: How to run bash file from perl
by raybies (Chaplain) on May 17, 2011 at 15:13 UTC

    This solution might not be fancy enough... but...

    if it must be bash, then just as you start a perl script with the #!, you start your bash shell script with its own...

    #!/bin/bash

    (assuming that bash is in the /bin dir).

    Then just do a system() call to your learn.sh script from within Perl with the commandline args...

Re: How to run bash file from perl
by bndgyawali (Initiate) on May 17, 2011 at 18:50 UTC
    Method 1:
    my $afile='dir1/firstfile.txt';<br> my $bfile='dir2/secondfile.txt';<br> my $bash_command='learn.sh '.$afile.' '.$bfile.' |';<br> open(my $output,$bash_command) or die $!;<br> while(my $line=<$output>){<br> print "$line";<br> }
    Method 2:
    my $afile='dir1/firstfile.txt';<br> my $bfile='dir2/secondfile.txt';<br> @args = ("learn.sh", $afile, $bfile); system(@args) == 0 or die "system @args failed: $?"
    Method 3:
    my $output=`learn.sh $afile $bfile`;
    I tried three different ways as you all have suggested, but I couldn't make my script work. Please help me what is wrong in it

    Binod
      Hi Binod

      Would you like to tell us how it doesn't work? What are you expecting to see? What do you see? Are there any error messages? The more specific you are the more likely folk will be to help you out. You might want to review How do I post a question effectively?

      If it hangs, it probably needs input, in which case IPC::Open2 or IPC::Open3 and print to the input channel of the subprocess, close it and read the output. But you still need to know what it expects and what to expect back. Try running the command directly and saving sample I/O to files, e.g. stdin.dat stdout.dat and stderr.dat. Then use these files as a template for designing your program around open2 or open3.

      hint; to save input to a file, use:

      echo ' stuff ' | tee stdin.dat | command.sh args >stdout.dat 2>stderr.dat

      One world, one people

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (3)
As of 2024-07-25 03:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.