【问题标题】:Bash script type inputs when prompted提示时输入 Bash 脚本类型
【发布时间】:2016-03-19 03:52:12
【问题描述】:

编辑:我正在重写这个,因为第一次有点不清楚。

假设我有一个程序(可执行文件),当我运行它时,它会提示我输入输入。

比如我执行./myProgram

程序提示:Please enter your username:

在这里,我将输入我的用户名。

现在,我将如何编写一个 bash 脚本,以便在启动上述程序后,我可以输入输入?

类似这样的事情:

#!/bin/bash

path/to/myProgram

# And here I would enter the commands, such as providing my username

谢谢

【问题讨论】:

标签: linux bash shell input stdin


【解决方案1】:

如果myProgram从标准输入读取,您可以使用here-document

path/to/myProgram <<\END
username
more input if needed
END

【讨论】:

    【解决方案2】:

    reading 交互值在 *nix 脚本中相当少见,并且被那些想要完全按照您正在尝试做的事情的人所反对。执行此操作的标准方法是myProgram 更改为接受arguments此时执行此操作很简单。

    如果你真的需要使用这种模式,你需要使用一些工具,比如expectas pointed out by @EricRenouf

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-14
      • 1970-01-01
      • 1970-01-01
      • 2021-02-10
      • 2017-11-21
      • 1970-01-01
      相关资源
      最近更新 更多