[bash] Simulate user input in bash script

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?

This question is related to bash input emulation user-input

The answer is


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"

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!"

Examples related to bash

Comparing a variable with a string python not working when redirecting from bash script Zipping a file in bash fails How do I prevent Conda from activating the base environment by default? Get first line of a shell command's output Fixing a systemd service 203/EXEC failure (no such file or directory) /bin/sh: apt-get: not found VSCode Change Default Terminal Run bash command on jenkins pipeline How to check if the docker engine and a docker container are running? How to switch Python versions in Terminal?

Examples related to input

Angular 4 - get input value React - clearing an input value after form submit Min and max value of input in angular4 application Disable Button in Angular 2 Angular2 - Input Field To Accept Only Numbers How to validate white spaces/empty spaces? [Angular 2] Can't bind to 'ngModel' since it isn't a known property of 'input' Mask for an Input to allow phone numbers? File upload from <input type="file"> Why does the html input with type "number" allow the letter 'e' to be entered in the field?

Examples related to emulation

Waiting for Target Device to Come Online Android emulator not able to access the internet Intel X86 emulator accelerator (HAXM installer) VT/NX not enabled Google Play Services Missing in Emulator (Android 4.4.2) Is there a real solution to debug cordova apps Genymotion Android emulator - adb access? How to get the sign, mantissa and exponent of a floating point number Simulate user input in bash script Enabling WiFi on Android Emulator How do I rotate the Android emulator display?

Examples related to user-input

Simple InputBox function Asking the user for input until they give a valid response Converting String Array to an Integer Array How to save user input into a variable in html and js Command line input in Python Javascript - User input through HTML input tag to set a Javascript variable? Simulate user input in bash script How to scanf only integer and repeat reading if the user enters non-numeric characters? How to get input from user at runtime Why can't I enter a string in Scanner(System.in), when calling nextLine()-method?