User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Shell Scripting section within the Software Development category of DaniWeb, a massive community of 456,446 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,659 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Shell Scripting advertiser: Programming Forums
Views: 2094 | Replies: 4
Reply
Join Date: Oct 2007
Posts: 2
Reputation: toronto is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
toronto toronto is offline Offline
Newbie Poster

Help su - postgres within shell script

  #1  
Oct 29th, 2007
Hello Guys,

I am new to Shell Scrpting but I am learning quickly. My script looks as follows:


#!/bin/sh
su - postgres
echo "Please enter the password for a2billing user and confirm it: "
createuser -W -S -d -r -e a2billinguser
createdb -e -O a2billinguser a2billing
exit 0


With above, the script should prompt for a password to create a user and then ask for the same password again to create database. However, my output stops after using command "su - postgres". What can I do to curb this? is there an alternative?

Thanks,
Bruce
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Feb 2006
Posts: 1,509
Reputation: masijade is a glorious beacon of light masijade is a glorious beacon of light masijade is a glorious beacon of light masijade is a glorious beacon of light masijade is a glorious beacon of light 
Rep Power: 10
Solved Threads: 136
masijade's Avatar
masijade masijade is offline Offline
Posting Virtuoso

Re: su - postgres within shell script

  #2  
Oct 29th, 2007
Because su opens an interactive shell you might try placing everything below the su command into a seperate script (lets imagine we call it newScript.ksh), then change this script to the following:
#!/bin/sh
su - postgres -c "/path/to/newScript.ksh"
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote  
Join Date: Oct 2007
Posts: 306
Reputation: eggi is on a distinguished road 
Rep Power: 2
Solved Threads: 29
eggi eggi is offline Offline
Posting Whiz

Re: su - postgres within shell script

  #3  
Oct 29th, 2007
Hi there,

It's probably expecting a reply to the password: prompt from su.

You can make it all one line and get away with it, but you'd still have to enter 3 passwords (assuming you're not su-ing postgres as root) - one for the su - one for the createuser and one for the createdb call)

su - postgres -c "echo "Please enter the password for a2billing user and confirm it:";createuser -W -s -d -r -e a2billinguser;createdb -e -O a2billinguser a2billing"

Hope it helps

, Mike
Linux and Unix Tips, Tricks and Individual Advice - The Linux and Unix Menagerie!
------------------------------------------------------------------------
Having trouble passing cert exams? Check out How To Pass Any Computer Certification Test!
Reply With Quote  
Join Date: Oct 2007
Posts: 2
Reputation: toronto is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
toronto toronto is offline Offline
Newbie Poster

Re: su - postgres within shell script

  #4  
Oct 29th, 2007
Thanks you very much Masijade and Eggi. I tried both methods and find the one line easier because of not creating another file; however, I understand that as the file gets bigger then it's better to have it chunks (more organized). You guys are AMAZING.
THANKS
Reply With Quote  
Join Date: Feb 2006
Posts: 1,509
Reputation: masijade is a glorious beacon of light masijade is a glorious beacon of light masijade is a glorious beacon of light masijade is a glorious beacon of light masijade is a glorious beacon of light 
Rep Power: 10
Solved Threads: 136
masijade's Avatar
masijade masijade is offline Offline
Posting Virtuoso

Re: su - postgres within shell script

  #5  
Oct 30th, 2007
Of course you can always forego maintaining a separate file, and still execute su on a single command as follows:

#!/bin/sh
cat > /tmp/someObscureName.sh <<EOF
#!/bin/sh
echo "Please enter the password for a2billing user and confirm it: "
createuser -W -S -d -r -e a2billinguser
createdb -e -O a2billinguser a2billing
EOF
su - postgres -c "/tmp/someObscureName.sh"
rm -f /tmp/someObscureName.sh
exit 0

I'm just not a big fan of providing alot of semicolon separated commands into a single su -c. It is cleaner and easier to understand/maintain this way, IMHO. (Even if a single line might be more effecient.)
Last edited by masijade : Oct 30th, 2007 at 6:32 am.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Shell Scripting Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Shell Scripting Forum

All times are GMT -4. The time now is 1:52 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC