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 "<<".
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 useexpect
, pretty good program for these kind of situations. – Olivier Dulac – 2012-12-28T14:26:48.287Good thinking Olivier! – MonkeyWidget – 2013-09-22T04:39:30.410