Inserting text in a user prompt when executing a command from shell (bash) script? [duplicate]
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
精彩评论