Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
#!/usr/local/bin/perl -w # perl script to do a piping between up to 3 commands commands # use a # to separate commands on the command line $cmd = <STDIN>; #get input from the keyboard and store in variable +cmd chomp ($cmd); #remove carriage return @cmd = split (#, $cmd) #take array and decide on the pipe symbol wh +ich is # to separate commands $max = @cmd; #return the number of items in cmd $max = $max - 1; #max start at 0 # $pipe = $max - 1; #pipe start at 0 for ($j = 1; $j < $max - 1; $j++) # for loop used to create pipes { $INPIPE [$j] = $j; $OUTPIPE [$j] = $j; pipe $INPIPE [$j], $OUTPIPE [$j]; } for ($x = 1; $x < $max - 1; $x++) { #go through each part of the ar +ray if (fork == 0) { #first command for ($n = 1; $n < $max -1; $n++) #close all input pipes close INPIPE[$n]; for ($p = 2; $p < $max -1; $p++) #close all the output pipes close OUTPIPE[$p]; #except the last one close STDOUT; #close STDOUT and connect it to the open STDOUT ">&OUTPIPE[1]"; #first output pipe still open exec cmd[1]; #execute the command } elsif (x != 1 && x != max -1) #middle commands { for ($n = 1; $n < $max -1; $n++) #close all input pipes except +the if n != x -1 #previous one close INPIPE [$n]; if n != x #close all output pipes except the last one close OUTPIPE [$n]; close STDIN; #close STDIN and connect to previous pipe open STDIN ">&INPIPE[n -1]; close STDOUT; #close STDOUT and connect to last pipe open STDOUT ">&OUTPIPE[n]; exec cmd[x]; #execute the middle commands } elsif (x = max - 1) #last command { for ($n = 1; $n < $max -2; $n++) #close all input pipes except +the close INPIPE[$n]; #last one for ($p = 1; $p < $max -1; $p++) #close all output pipes close INPIPE[$p]; close STDIN; open STDIN ">&INPIPE[max -1]; exec cmd[$max - 1]; #execute the last command } } for ($d = 1; $d< $max - 1; $d++) { close $INPIPE[$d]; #close both input pipes close $OUTPIPE[$d]; #close both output pipes } for ($s = 1; $s < $max - 1; $s++) { wait; #wait for each child process }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: home work help
by mbond (Beadle) on Jun 20, 2001 at 16:43 UTC | |
|
Re: home work help
by buckaduck (Chaplain) on Jun 20, 2001 at 17:30 UTC | |
by mbond (Beadle) on Jun 20, 2001 at 18:04 UTC | |
| |
|
Re: home work help
by Davious (Sexton) on Jun 20, 2001 at 19:36 UTC | |
|
Re: home work help
by bikeNomad (Priest) on Jun 20, 2001 at 19:42 UTC |