2

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

Calvin
  • 8,697
  • 7
  • 43
  • 51

2 Answers2

6

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

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • Unfortunately this isn't packaged with Ubuntu (and probably not other distros); on production machines where you may not have admin privileges this may not be an option. – Doktor J Jan 30 '22 at 04:56
0

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
informatorius
  • 624
  • 6
  • 7