Vishal
Vishal

Reputation: 1492

Is there a way to input automatically when running a script?

I am trying to execute a script file which asks for password as input. Is there any way we give the command the input it needs automatically? It's in psql which has parameter -U for username but no parameter for password.

Example:

exa00009@exa00009:~$ sh redshift.sh
Password for user xyz:

Upvotes: 1

Views: 260

Answers (2)

oakymax
oakymax

Reputation: 1474

You can use expect for this. Here is simple example:

#!/usr/bin/expect -f
set PASSWORD "1"
set USER "dvp"
spawn psql -U ${USER} -W
expect "*?assword*"
send -- "${PASSWORD}\r"
expect eof

Probably you need to install expect first:

sudo apt-get install expect

Upvotes: 1

Amol Bais
Amol Bais

Reputation: 359

You can provide command line argument for your script like below: sh redshift.sh yourInput and in your script get it as var input=$1 and use accordingly.

This question is already answered here

Upvotes: 0

Related Questions