我尝试管道几个脚本,但是真的不明白如何正确地完成它。
mkfifo pipe1
cat ./script1 > pipe1 &
cat ./script2 > pipe1 &
cat ./script3 > pipe1 &
./script1 < pipe1
所以问题是我可以这样做吗?我的意思是将所有脚本写在一个管道中,并在我需要时只读取其中一个
答案 0 :(得分:1)
cat
可以处理多个文件,并依次读取每个文件:
cat ./script1 ./script2 ./script3 | ./videoplaylist
更新:在问题的修订版中,目标是script1
本身:
cat ./script1 ./script2 ./script3 | ./script1
以下代码将运行三个脚本,合并其输出并将其发送到masterscript
:
mkfifo pipe1
bash ./script1 > pipe1 &
bash ./script2 > pipe1 &
bash ./script3 > pipe1 &
bash ./masterscript < pipe1
让我们从四个脚本开始:
$ 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
现在让我们执行它们:
$ 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
如您所见,FIFO成功将每个脚本的输出发送到主脚本。