【问题标题】:how to pass keyboard input along to linux command?如何将键盘输入传递给linux命令?
【发布时间】:2010-08-02 04:44:21
【问题描述】:

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

我一直想回答1,如何自动传递这个值?

【问题讨论】:

    标签: bash


    【解决方案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.
    

    【讨论】:

      【解决方案2】:

      只是一个想法:

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

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

      【讨论】:

        【解决方案3】:
        yes 1 | command
        

        【讨论】:

          猜你喜欢
          • 2013-11-27
          • 2019-11-14
          • 2014-07-02
          • 2020-05-14
          • 2013-10-11
          • 1970-01-01
          • 2022-01-23
          • 2020-05-16
          • 2016-03-25
          相关资源
          最近更新 更多