Simulate user input in bash script

Werulz picture Werulz · Feb 3, 2013 · Viewed 105.1k times · Source

I am creating my own bash script, but I am stuck at the moment. Basically, the script would be used to automate server setup in CentOS. Some software normally asks the user to type a password. I want the script to put the password that I have generated and stored as a variable instead of asking the user.

When the message "New password:" appears during install, how can I make the script put the value stored in a variable $key as if the user had typed it, with a bash script?

Answer

FreudianSlip picture FreudianSlip · Feb 3, 2013

You should find the 'expect' command will do what you need it to do. Its widely available. See here for an example : http://www.thegeekstuff.com/2010/10/expect-examples/

(very rough example)

#!/usr/bin/expect
set pass "mysecret"

spawn /usr/bin/passwd

expect "password: "
send "$pass"
expect "password: "
send "$pass"