bash 如何将键盘输入传递给 linux 命令?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3385049/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to pass keyboard input along to linux command?
提问by KJW
I run a linux command that sometimes asks for user input (press 1 or 2).
我运行一个 linux 命令,有时会要求用户输入(按 1 或 2)。
I always want to answer 1, how can I pass this value automatically ?
我总是想回答 1,如何自动传递这个值?
回答by John Kugelman
Use the pipe |operator to connect the output of one command to the input of another.
使用管道|运算符将一个命令的输出连接到另一个命令的输入。
echo 1 | command
If you want to repeat some input to a command, you can use yes. By default it sends the string "y" repeatedly but it also repeat a different string of your choice.
如果要重复某个命令的某些输入,可以使用yes. 默认情况下,它会重复发送字符串“y”,但也会重复您选择的不同字符串。
yes | cp * /tmp # Answer "y" to all of cp's "Are you sure?" prompts.
yes 1 | command # Answer "1" repeatedly until the command exits.
回答by Jesse Dhillon
Just a thought:
只是一个想法:
echo "1" | linux_command --with-arguments <&0
This works for commands which want input from stdin, because 0 is the descriptor for standard input. This question might better belong on Server Fault however...
这适用于需要从 stdin 输入的命令,因为 0 是标准输入的描述符。这个问题可能更好地属于服务器故障但是......
回答by Bertrand Marron
yes 1 | command

