Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simulate user input in bash script [closed]

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?

like image 318
Werulz Avatar asked Feb 03 '13 08:02

Werulz


People also ask

What is $@ in bash script?

bash [filename] runs the commands saved in a file. $@ refers to all of a shell script's command-line arguments. $1 , $2 , etc., refer to the first command-line argument, the second command-line argument, etc. Place variables in quotes if the values might have spaces in them.

What is $_ in bash?

$_ (dollar underscore) is another special bash parameter and used to reference the absolute file name of the shell or bash script which is being executed as specified in the argument list. This bash parameter is also used to hold the name of mail file while checking emails.

What is interactive mode in bash?

A bash shell is considered as an interactive shell when it reads and writes data from a user's terminal. Most startup scripts examine the shell variable called PS1. Usually, PS1 is set in interactive shells, and it is unset in non-interactive shells.


1 Answers

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

  • See here for an example
  • More examples here

A very rough example:

#!/usr/bin/expect set pass "mysecret"  spawn /usr/bin/passwd  expect "password: " send "$pass" expect "password: " send "$pass" 
like image 127
FreudianSlip Avatar answered Oct 06 '22 12:10

FreudianSlip