出现提示时,Bash脚本类型输入

时间:2015-12-13 17:20:10

标签: linux bash shell input stdin

编辑:我重写了这篇文章,因为第一次有点不清楚。

我们说我有一个程序(可执行程序),这样当我运行它时,它会提示我输入一个输入。

例如,我执行./myProgram

并且程序提示:Please enter your username:

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

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

有些事情如下:

#!/bin/bash

path/to/myProgram

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

由于

2 个答案:

答案 0 :(得分:0)

以交互方式

read值在* nix脚本中并不常见,并且那些想要完全按照您要做的事情的人不赞成。执行此操作的标准方法是更改myProgram以接受arguments此时执行此操作非常简单。

如果您确实需要使用此模式,则需要使用expectas pointed out by @EricRenouf等工具。

答案 1 :(得分:0)

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

path/to/myProgram <<\END
username
more input if needed
END
相关问题