1

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:
Ashish Ahuja
  • 4,913
  • 10
  • 51
  • 65
Vishal
  • 1,327
  • 2
  • 26
  • 41
  • Possible duplicate of [passing arguments to an interactive program non interactively](http://stackoverflow.com/questions/14392525/passing-arguments-to-an-interactive-program-non-interactively) – Xiong Chiamiov Jul 19 '16 at 21:52

2 Answers2

1

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
oakymax
  • 1,394
  • 1
  • 12
  • 21
0

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

Community
  • 1
  • 1
Amol Bais
  • 317
  • 1
  • 4
  • 26
  • can u elaborate , my .sh file has psql command which excepts username parameter but not password . i need to automate it so that i just have to run script – Vishal Jul 18 '16 at 06:46