944,087 Members | Top Members by Rank

Ad:
Nov 23rd, 2004
0

How to su properly in a shell script

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
stharmon is offline Offline
8 posts
since Nov 2004
Dec 2nd, 2004
0

Re: How to su properly in a shell script

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.
Team Colleague
Reputation Points: 186
Solved Threads: 147
Cookie... That's it
alc6379 is offline Offline
2,519 posts
since Dec 2003
Apr 22nd, 2009
0

Re: How to su properly in a shell script

Click to Expand / Collapse  Quote originally posted by alc6379 ...
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
akku12345 is offline Offline
1 posts
since Apr 2009
Apr 22nd, 2009
0

Re: How to su properly in a shell script

Click to Expand / Collapse  Quote originally posted by stharmon ...
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.
Reputation Points: 51
Solved Threads: 35
Posting Whiz in Training
Fest3er is offline Offline
238 posts
since Aug 2007
Sep 14th, 2010
0
Re: How to su properly in a shell script
Is it possible for me to su to different user from within a script, execute few commands in the new shell and stay in the same shell...?

For Ex:

#!/bin/sh
sudo su - user1
echo #HOME
echo `ls -ltr`

This should print the home directory of user1, print the files there and stay in the same shell.

But the script doesn't execute after su to user1.

sudo su - c "echo $HOME" - user1

works but it exits from new shell immediately after printing $HOME value.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
komalar is offline Offline
1 posts
since Sep 2010
Nov 26th, 2010
0
Re: How to su properly in a shell script
I am executing the following script like "sudo ./script.sh", so that no authorization is needed when temporarily switching to user "test". The HERE tags delimit the execution in the test user context
Shell Scripting Syntax (Toggle Plain Text)
  1. #!/bin/sh
  2. echo ~/
  3. su - test <<HERE
  4. echo ~/
  5. ls -al
  6. HERE
  7. echo ~/

:-)

/Peter Odéus
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Peter Odéus is offline Offline
1 posts
since Nov 2010
Nov 28th, 2010
0
Re: How to su properly in a shell script
hi i need automated routines in bash shell.. i do know how i m start, if you know pls send it to me....
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rehm is offline Offline
2 posts
since Nov 2010
Nov 28th, 2010
0
Re: How to su properly in a shell script
hi i need automated routines in bash shell.. i don't know how i m start, if you know pls send it to me....
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rehm is offline Offline
2 posts
since Nov 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Shell Scripting Forum Timeline: Shell Script - Array Variable - Out of Loop
Next Thread in Shell Scripting Forum Timeline: Quoting





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC