我有一个想要运行的脚本
$ myscript < mydata.dat
在内部,myscript
我需要将STDIN分叉/管道到多个目的地
#!/usr/bin/env bash
php script1.php > out1
php script2.php > out2
php script3.php > out3
每个人都需要一份STDIN。可能吗?
就像是
# obviously this is broken ...
STDIN | php script1.php > out1
STDIN | php script2.php > out2
STDIN | php script3.php > out3
要将stdin复制到多个进程,请使用tee
和进程替换:
tee >(script1 > out1) >(script2 >out2) ... | lastscript >outlast
该构造>(...)
称为过程替换。它创建一个tee
可以写入的类似文件的对象。将执行括号内的命令,并且对其进行的任何tee
写操作都会以stdin的形式提供给该命令。
bash,ksh和zsh支持进程替换。它不是POSIX,在破折号下将不起作用。
让我们考虑这个简单的脚本:
$ cat myscript
#!/bin/bash
tee >(grep 1 >out1) >(grep 2 >out2) | grep 3 >out3
我们可以运行它并验证结果:
$ seq 10 | bash myscript
$ cat out1
1
10
$ cat out2
2
$ cat out3
3
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句