in reply to Multiple commands with one system call

I'm not sure if I fully understand your statement of the task, but in case it helps, here's a technique I've used to good effect when I want a perl script to run a series of commands in sequential order, and the script has to wait until all the commands are done before moving on:
#!/usr/bin/perl use strict; use warnings; my @cmd_list = ( 'echo 1', 'echo 2', 'echo 3', 'echo 4' ); $|++; # turn off buffering my $shpid = open( my $sh, '|-', '/bin/bash' ) or die "Can't open a shell process: $!\n"; for my $cmd ( @cmd_list ) { print $sh "$cmd\n"; } print $sh "exit\n"; close $sh; waitpid( $shpid, 0 ); print "Shell's all done. Moving right along...\n";

Replies are listed 'Best First'.
Re^2: Multiple commands with one system call
by renzosilv (Novice) on Oct 12, 2011 at 16:08 UTC

    Hey!

    I looked around online for answers but I couldn't really find the correct format to open a bash script through perl and yours is giving me an error.

    my $shpid = open( my $sh, '|-', '/bin/bash' ) or die "Can't open a shell process: $!\n";

    It says that, the preceding line has errors. I don't know enough about bash to fix it. Any chance you could help me out ?

      Hey yourself!

      Is there something that makes you unable to paste the actual text of the error message into your post? What does "It" refer to? What does "the preceding line" refer to? What exactly did you try, and what exactly was the full error message? Were you using the code as I posted it, or did you try something "based on" my post? What OS are you using?

      If you could give more explicit information about the problem(s) you're having, you might get some useful help.

        The error is not very descriptive but here it is

        Too many arguments for open at /home/vpoint/testing.pl line 10, near " +'/bin/bash' ) " Execution of /home/vpoint/testing.pl aborted due to compilation errors +.

        I was trying to use the code exactly as you posted it. The only thing I removed was use warnings. The OS I am using is

         SunOS ferb 5.8 Generic_117350-21 sun4u sparc SUNW,Ultra-80

        This is the perl version in case it helps.

         perl, version 5.005_03 built for sun4-solari