如何将键盘输入传递给linux命令?

时间:2010-08-02 04:44:22

标签: bash

我运行linux命令,有时会要求用户输入(按1或2)。

我总是想回答1,我该如何自动传递这个值?

3 个答案:

答案 0 :(得分:12)

使用管道|运算符将一个命令的输出连接到另一个命令的输入。

echo 1 | command

如果要重复输入命令,可以使用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.

答案 1 :(得分:3)

只是一个想法:

echo "1" | linux_command --with-arguments <&0

这适用于需要从stdin输入的命令,因为0是标准输入的描述符。这个问题可能更适合服务器故障但是......

答案 2 :(得分:1)

yes 1 | command