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

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"

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

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

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.)

Hi All, I am writing a shell script where in a condition the script will change the user like su - xxx and then it has to execute some commands/scripts and should stay login to that user/session for user interaction. Once user enter exit it should exit from that session and should return to script.

I have tried using su - xxx -c but it terminates the session after executing the commands. I want to stay on that session after execution of the commands, user should exit manually (type exit) to close that session and return to script.

Any hint will do..

Thanks in advance....for you suggestion..

Have the last command be a call to a shell. You do know, however, that this is rendering your security a moot point, right? Not a good idea IMHO.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.