我运行一个Linux命令,有时会要求用户输入(按1或2)。
我总是想自动回答1,该怎么办?
使用管道 |
连接符将一个命令的输出连接到另一个命令的输入。
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.
只是一个想法:
echo "1" | linux_command --with-arguments <&0
这适用于需要从标准输入读取输入的命令,因为0是标准输入的描述符。然而,这个问题可能更适合放在服务器故障上...
yes 1 | command