我经历了一些bash i / o教程,但大多数都涉及将流重定向到文件或从文件重定向。
我的问题如下:如何将stdin / stdout / stderr重定向到脚本(或程序)。
例如我有脚本“parentScript.sh”。在那个脚本中,我想调用blackbox“childScript.sh”,它接受少量参数-arg1 -arg2 ...并从stdin读取输入。
我的目标是使用parentScript.sh中的一些输入提供childScript.sh:
...
childScript.sh -arg1 -arg2
????? < "input1"
????? < "input2"
...
另一种情况是我打电话给几个节目,我希望他们这样说话:
...
program1 -arg1 -arg2
program2 -arg1 -arg9
(program1 > program2)
(program2 > program1)
etc...
...
如何解决这2起案件?感谢
编辑: 更具体。我想创建自己的管道(命名或未命名)并使用它们连接多个程序或脚本,以便它们相互通信。
例如:program1写入program2和program3并从program2接收。 program2写入program1和program3并从program1接收。 program3只接收表单program1和program2。
答案 0 :(得分:2)
管|
是你的朋友:
./script1.sh | ./script2.sh
将stdout从script1.sh
发送到script2.sh
。如果你想发送stderr:
./script1.sh 2>&1 | ./script2.sh
只有stderr:
./script1.sh 2>&1 >/dev/null | ./script2.sh
你也可以在这里制作文件:
./script2.sh << MARKER
this is stdin for script2.sh.
Variable expansions work here $abc
multiply lines works.
MARKER
./script2.sh << 'MARKER'
this is stdin for script2.sh.
Variable expansions does *not* work here
$abc is literal
MARKER
MARKER
几乎可以是任何内容:EOF
,!
,hello
,...有一点需要注意的是,前面不能有任何空格/标签结束标记。
在bash中你甚至可以使用<<<
,它的工作方式与此类文档非常相似,如果有人能够澄清它,将会非常感激:
./script2.sh <<< "this is stdin for script2.sh"
./script2.sh <<< 'this is stdin for script2.sh'
答案 1 :(得分:0)
您可以使用HEREDOC语法,例如:
childScript.sh -arg1 -arg2 <<EOT
input1
EOT
childScript.sh -arg1 -arg2 <<EOT
input2
EOT
并将第一个脚本的输出转发到第二个输入:
program1 -arg1 -arg2 | program2 -arg1 -arg9