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:
Box Box Box Box
  • 5,094
  • 10
  • 49
  • 67
Vishal
  • 1,442
  • 3
  • 29
  • 48
  • 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,454
  • 1
  • 14
  • 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
  • 332
  • 1
  • 6
  • 30
  • 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