'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 my 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



Solution 1:[1]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1