开发者

Inserting text in a user prompt when executing a command from shell (bash) script? [duplicate]

开发者 https://www.devze.com 2023-02-24 06:30 出处:网络
This question already has answers here: Passing arguments to an interactive program non-interactively (5 answers)
This question already has answers here: Passing arguments to an interactive program non-interactively (5 answers) Closed 1 year ago.

I have a bash script that executes another program. This program prompts the user for additional input. Question is, how do I automate that input from my bash script?

For example, running the s3cmd command from 开发者_如何学Gomy script prompts me for:

Enter new values or accept defaults in brackets with Enter.
Refer to user manual for detailed description of all options.

Access key and Secret key are your identifiers for Amazon S3
Access Key: _

In my shell script, how would I insert a string for the Access Key prompt, then simulate the "enter" key?

EDIT: for clarity


You would typically use the expect command, or one of its workalikes. The code is now available from SourceForge, it seems.


You can use the echo command to send input keys to a script (using \n for enter key)

echo "MyAccessKey\nSecretKey\n" | ./myscript.sh

Or if a script asks several questions for yes(y)/no(n) and you want to answer: yes/no/yes

echo "yny" | ./myscript.sh
0

精彩评论

暂无评论...
验证码 换一张
取 消