heredoc prompts not passed in

3

I'm trying to automate the creation of a PostgreSQL user with a bash script.

I'm specifying the initial password as prompted by the "createuser" command, and I'd like to do it in bash, which I think implies a here document.

Yet the prompt answers I've specified don't make it to the "createuser" command! Here is directly entering the script on the command line:

-bash-4.1$ createuser mynewuser -P <<ENDBEFORETHIS
> fakepassword
> fakepassword
> n
> n
> n
> ENDBEFORETHIS
Enter password for new role: 
-bash-4.1$ 

Why is that?

  • How can I get the input to the createuser script?
  • is there a more effective way of doing this?

For those just joining us, the "<<" is known as a here-document or a "heredoc", which essentially pipes the next few lines to the command as stdin. It runs until the matching label specified after the "<<".

MonkeyWidget

Posted 2012-12-21T01:21:23.483

Reputation: 133

I don't understand your example. Do you want the user to actually type a bunch of stuff followed by SETPASSWORD? – None – 2012-12-21T04:34:12.113

I'll update the question to explain what a heredoc is – MonkeyWidget – 2012-12-21T19:23:31.407

for clarity, you should change both "SETPASSWORD" to "ENDBEFORETHIS" ^^ that way non-familiar users of bash won't be expecting bash to incorporate the "SETPASSWORD" string into the input stream. – Olivier Dulac – 2012-12-28T14:23:41.507

can a : { echo fakepassword ; sleep 1s ; ....... ; } | createuser mynewuser -P work ? (I can't test right now). Or do use expect, pretty good program for these kind of situations. – Olivier Dulac – 2012-12-28T14:26:48.287

Good thinking Olivier! – MonkeyWidget – 2013-09-22T04:39:30.410

Answers

1

The password prompt can be avoided by issuing directly the CREATE USER SQL command, which is what the createuser command would ultimately do, anyway.

~$ psql << EODOC
>create user foo password 'bar';
>EODOC
CREATE ROLE
~$

Daniel Vérité

Posted 2012-12-21T01:21:23.483

Reputation: 1 225

2

Is there a more effective way of doing this?

You should probably look into using expect(1).

Carl Norum

Posted 2012-12-21T01:21:23.483

Reputation: 2 621

probably, especially if there really is some "don't accept through stdin" thing on these password answers – MonkeyWidget – 2012-12-21T19:21:59.363

1

For security reasons passwords will not be accepted through stdin. using expect will help u on this.

Kannan Mohan

Posted 2012-12-21T01:21:23.483

Reputation: 398

what's weird is this worked a few months ago. I can imagine it perhaps previously working only due to a security bug? – MonkeyWidget – 2012-12-21T19:23:00.800