How to su properly in a shell script

Reply

Join Date: Nov 2004
Posts: 8
Reputation: stharmon is an unknown quantity at this point 
Solved Threads: 0
stharmon stharmon is offline Offline
Newbie Poster

How to su properly in a shell script

 
0
  #1
Nov 23rd, 2004
Hello,

I have written a script to automate a software install. I am running the script as root, but need to su to another user to configure and complile the program properly. Whenever I do the shell script su's to the user properly but the scripts stops executing until I exit out of the new shell that su put me into. The following is an example of my script.

echo -e "\033[1m \n\t Installing courier-imap... \033[0m"
sleep 2
cd /var/src
tar -xjf tar/courier-imap-2.2.2.20040207.tar.bz2
cd courier-imap-2.2.2.20040207

# build as vpopmail
chown -R vpopmail:vchkpw ../courier-imap-2.2.2.20040207
su - vpopmail

./configure --with-redhat

I have also tried using the following to stay in the same shell and preserve the environment:
su - -m -p vpopmail

Does anyone have any thought as to how I can successfully SU and keep the script running?

Thanks,

Stephen
Reply With Quote Quick reply to this message  
Join Date: Dec 2003
Posts: 2,414
Reputation: alc6379 has a spectacular aura about alc6379 has a spectacular aura about alc6379 has a spectacular aura about 
Solved Threads: 123
Team Colleague
alc6379's Avatar
alc6379 alc6379 is offline Offline
Cookie... That's it

Re: How to su properly in a shell script

 
0
  #2
Dec 2nd, 2004
Ever thought about sudo?

It will allow you to execute single commands as a user without having to drop into the shell of that user. You could even run sudo, and point it to another shell script that contains all of the commands you need it to run, so you could make that script stay somewhat compact.
Alex Cavnar, aka alc6379
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 1
Reputation: akku12345 is an unknown quantity at this point 
Solved Threads: 0
akku12345 akku12345 is offline Offline
Newbie Poster

Re: How to su properly in a shell script

 
0
  #3
Apr 22nd, 2009
Originally Posted by alc6379 View Post
Ever thought about sudo?

It will allow you to execute single commands as a user without having to drop into the shell of that user. You could even run sudo, and point it to another shell script that contains all of the commands you need it to run, so you could make that script stay somewhat compact.



Use -t option in ssh
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 165
Reputation: Fest3er is an unknown quantity at this point 
Solved Threads: 18
Fest3er Fest3er is offline Offline
Junior Poster

Re: How to su properly in a shell script

 
0
  #4
Apr 22nd, 2009
Originally Posted by stharmon View Post
echo -e "\033[1m \n\t Installing courier-imap... \033[0m"
sleep 2
cd /var/src
tar -xjf tar/courier-imap-2.2.2.20040207.tar.bz2
cd courier-imap-2.2.2.20040207

# build as vpopmail
chown -R vpopmail:vchkpw ../courier-imap-2.2.2.20040207
su - vpopmail

./configure --with-redhat
... Does anyone have any thought as to how I can successfully SU and keep the script running?
To actually answer your query, you need to feed the commands you want to execute to su(), as in:
Shell Scripting Syntax (Toggle Plain Text)
  1. su -c "./configure --with-redhat" - vpopmail

You can execute the commands one at a time, or you can put them in another shell script and have su() execute that shell script.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC