Linux 从标准输入到标准输出的短划线重定向

Linux 从标准输入到标准输出的短划线重定向,linux,bash,shell,Linux,Bash,Shell,这个问题的关键值与使用破折号“-”重定向标准输入数据流有关 我在下一个代码中有基本的测试脚本: #!/bin/bash # This causes the shell to await user input. file - # Need to emulate user input here... 在这种情况下,哪一个shell命令可以模拟用户输入 谢谢。根据定义,管道cmd1 | cmd2将cmd1的标准输出文件描述符连接为cmd2的标准输入文件描述符 echo foo | file - 这

这个问题的关键值与使用破折号“-”重定向标准输入数据流有关

我在下一个代码中有基本的测试脚本:

#!/bin/bash
# This causes the shell to await user input.
file -
# Need to emulate user input here...
在这种情况下,哪一个shell命令可以模拟用户输入


谢谢。

根据定义,管道
cmd1 | cmd2
cmd1
的标准输出文件描述符连接为
cmd2
的标准输入文件描述符

echo foo | file -
这个特别的例子几乎没有用处;
file
命令对管道中的临时数据运行没有多大意义。一个更有用的例子是在一个文件上运行
file
。如果您绝对希望文件成为标准输入,那么简单的输入重定向就可以做到这一点

file - </path/to/example/file
其中,
locate
是一个命令示例,该命令打印您可能想要的文件的完整路径,作为
文件的实际输入参数,您可能想要:

#/bin/bash
#这会导致shell等待用户输入。

文件-检查这篇文章,我想它包含了对你问题的回答。你是在问关于shebang中的bash,还是问题标题中的dash?他们在这方面是兼容的;但是你应该问一下Bourne
sh
和compatibles。谢谢你的回答。这对我理解输入功能非常有帮助。非常感谢。你的两个答案都很有帮助。这就是我要找的。
file $(locate example/file)
#!/bin/bash
# This causes the shell to await user input.
file - <<'EOF'
This is what the user
might have typed
if it wasn't in a here document.
${BASH:=head} $(ls | wc -l)
EOF