0

I'm trying to make this work

#!/usr/bin/expect

spawn "(psql blabla \"blabla sql;\" &> /dev/null) && (pg_restore -d xx my_file.sql &> /dev/null)"

expect "Password for user my_base: "
send "v"

But I always get:

spawn (psql blabla "blabla sql;" &> /dev/null) && (pg_restore -d xx my_file.sql &> /dev/null)
couldn't execute "(psql blabla "blabla sql;" &> /dev/null) && (pg_restore -d xx my_file.sql &> /dev/null)": no such file or directory

What am I missing?

Olivier Pons
  • 622
  • 1
  • 5
  • 22

2 Answers2

1

1) don't put the spawn arguments in quotes, and 2) spawn doesn't understand shell syntax, you need to explicitly spawn a shell

spawn bash -c {(psql blabla "blabla sql;" &> /dev/null) && (pg_restore -d xx my_file.sql &> /dev/null)}

This uses braces, which are the expect/Tcl equivalent of shell's single quotes.

glenn jackman
  • 4,630
  • 1
  • 17
  • 20
  • Thanks a lot it works. I have to learn: `C#`, `JavaScript`, `HTML`, `CSS`, `Python`, `Django`, `docker`, `kubernetes`, not to talk about `git` and all this "months learning curve", and now `shell`............................ so I feel "a bit" overwhelmed.... – Olivier Pons Jan 17 '20 at 18:08
  • not to mention `expect` which does have a fairly steep learning curve. – glenn jackman Jan 17 '20 at 18:30
0

Although this is not really the answer to your question, but if you're trying to do a pg_restore without entering a password, you can also use the ~/.pgpass file to store credentials in.

Postgres documentation on ~/.pgpass -

https://www.postgresql.org/docs/current/libpq-pgpass.html

abhijit
  • 101
  • 1