63

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?

Konrad Borowski
  • 11,584
  • 3
  • 57
  • 71
Werulz
  • 751
  • 1
  • 6
  • 7
  • Exactly what you need: https://askubuntu.com/questions/338857/automatically-enter-input-in-command-line – Leviathan Aug 04 '22 at 15:52

2 Answers2

66

You should find the 'expect' command will do what you need it to do. It's widely available.

A very rough example:

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

spawn /usr/bin/passwd

expect "password: "
send "$pass"
expect "password: "
send "$pass"
Saikat
  • 14,222
  • 20
  • 104
  • 125
FreudianSlip
  • 2,870
  • 25
  • 24
  • dude here is the "New password:" appears while the script is installing stuff with yum(e.g like mysql}.Could you plz show me a code snippet for this particular case.I use sed mostly and never used expect so far.From tutorial you provided , its seems you can use expect when user is suppose to enter something – Werulz Feb 03 '13 at 08:39
  • 1
    Look at the `#!` line - this is an Expect *script*. The whole thing is run by `expect`, not by `bash`, so it's written in the Expect language (which is basically Tcl). The important commands are there - `spawn` to start the command you're interacting with, `expect` to wait for it to send a specific string, and `send` to send a reply. If you want to turn control back over to the user, you can do so with `interact`. – Mark Reed Feb 03 '13 at 14:23
  • 1
    what do you do if you don't have spawn, expect, or send ?? how do we do this with tools we don't have to install? – user1747935 Oct 22 '14 at 18:55
  • 3
    @user1747935 if you have `expect` command, then you have everything you need. Notice the hashbang `#!/usr/bin/expect` - you aren't supposed to run this script from bash shell. – Jezor Oct 27 '17 at 07:50
  • @user1747935: here's a [superuser answer](https://superuser.com/a/380159/734854) on how to avoid `expect` scripts: `(echo "multiple"; echo "inputs to" ) | your_executable_script.sh` – Steven Kalt Feb 24 '18 at 02:39
  • If my command is complex, like `(psql blabla -c "blabla;" &> /dev/null) && (time (pg_restore blabla &> /dev/null))` i can't launch it I have errors: `(psql blabla -... /dev/null)): no such file or directory` – Olivier Pons Jan 17 '20 at 08:50
4

Here is a snippet I wrote; to ask for users' password and set it in /etc/passwd. You can manipulate it a little probably to get what you need:

echo -n " Please enter the password for the given user: "
read userPass
useradd $userAcct && echo -e "$userPass\n$userPass\n" | passwd $userAcct > /dev/null 2>&1 && echo " User account has been created." || echo " ERR -- User account creation failed!"
kevoroid
  • 5,052
  • 5
  • 34
  • 43
  • 4
    You guys are gettting me wrong.I don't want to read user input.All i want to do is when (yum install something) requires user input for password , it should *place* the value found in variable $key as input instead of asking user to input something – Werulz Feb 03 '13 at 08:44
  • Like when "New Password:" prompt appears during install, then set value in $key as pass – Werulz Feb 03 '13 at 08:45
  • in that, only expect will do the job. for instance, I used expect to pass a value when an external program asks for input from user. like when i want to add my ssh key to my ssh-agent, i automatically pass the pass-phrase to ssh-add via expect. – kevoroid Feb 03 '13 at 08:47
  • Thank you....also does expect already "stimulate" enter key press after it set the pass or i have to create another command for enter. – Werulz Feb 03 '13 at 08:59