check
check

Reputation: 39

create a pipe for several processes

I try to pipe several scripts, but dont really understand how to do it correctly.

  mkfifo pipe1
  cat ./script1 > pipe1 &
  cat ./script2 > pipe1 &
  cat ./script3 > pipe1 &
 ./script1 < pipe1

So the question is can I do it thay way? I mean to write all scripts in one pipe and read just one of the when I need it

Upvotes: 0

Views: 64

Answers (1)

John1024
John1024

Reputation: 113844

Answer for Original Question

cat can handle multiple files and will read each in turn:

cat ./script1 ./script2 ./script3 | ./videoplaylist

Update: In the revised version of the question, the target is script1 itself:

cat ./script1 ./script2 ./script3 | ./script1

Answer for Revised Question

The following code will run three scripts, merge their output and send it to masterscript:

mkfifo pipe1
bash ./script1 > pipe1 &
bash ./script2 > pipe1 &
bash ./script3 > pipe1 &
bash ./masterscript < pipe1

Working Example

Let's start with four scripts:

$ cat script1
#!/bin/sh
while sleep 1;do echo $0 $(date); done
$ cat script2
#!/bin/sh
$ cat masterscript 
#!/bin/sh
while read line; do echo "$0 received: $line"; done
while sleep 1;do echo $0 $(date); done
$ cat script3
#!/bin/sh
while sleep 1;do echo $0 $(date); done

Now let's execute them:

$ mkfifo pipe1
$ bash script1 >pipe1 & bash script2 >pipe1 & bash script3 >pipe1 &
[1] 29154
[2] 29155
[3] 29156
$ bash masterscript <pipe11
masterscript received: script2 Sat Apr 11 15:39:37 PDT 2015
masterscript received: script1 Sat Apr 11 15:39:37 PDT 2015
masterscript received: script3 Sat Apr 11 15:39:37 PDT 2015
masterscript received: script2 Sat Apr 11 15:39:38 PDT 2015
masterscript received: script1 Sat Apr 11 15:39:38 PDT 2015
masterscript received: script3 Sat Apr 11 15:39:38 PDT 2015
masterscript received: script1 Sat Apr 11 15:39:39 PDT 2015
masterscript received: script3 Sat Apr 11 15:39:39 PDT 2015
masterscript received: script2 Sat Apr 11 15:39:39 PDT 2015
masterscript received: script1 Sat Apr 11 15:39:40 PDT 2015
masterscript received: script2 Sat Apr 11 15:39:40 PDT 2015
masterscript received: script3 Sat Apr 11 15:39:40 PDT 2015
^C

As you can see, the FIFO succeeds in sending output of each the three scripts to the master script.

Upvotes: 1

Related Questions