•
•
•
•
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
![]() |
•
•
Join Date: Oct 2007
Posts: 2
Reputation:
Rep Power: 0
Solved Threads: 0
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
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
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
----------------------------------------------
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
•
•
Join Date: Oct 2007
Posts: 306
Reputation:
Rep Power: 2
Solved Threads: 29
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
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!
------------------------------------------------------------------------
Having trouble passing cert exams? Check out How To Pass Any Computer Certification Test!
Of course you can always forego maintaining a separate file, and still execute su on a single command as follows:
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.)
#!/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
----------------------------------------------
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
![]() |
•
•
•
•
•
•
•
•
DaniWeb Shell Scripting Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- Sending email from a shell script (Shell Scripting)
- Problem with variables in Windows shell script (Windows NT / 2000 / XP / 2003)
- Using find in a bash shell script (Shell Scripting)
- Shell Script for Gnome 2.6 Print Manager. (Shell Scripting)
- How to su properly in a shell script (Shell Scripting)
Other Threads in the Shell Scripting Forum
- Previous Thread: newbie question regarding checking date of file
- Next Thread: dont know how to do it



Linear Mode