Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a shell script in which most of the commands need to be run under sudo; however, the last few commands need to be run as the current user. Is there a way I can exit sudo and switch to the currently logged in user?

If I was running it all from the command line, I could do something like:

sudo su
[su commands]
exit
[user commands]

However, exit will stop the script at that command.

share|improve this question
2  
Have you considered modifying the script to prefix sudo to the commands needing to be run as root? sudo's inbuilt credential caching mechanism will mean you should only need to answer a password prompt once, unless the shell script runs for a very long time. –  Aaron Miller Sep 30 '13 at 16:19
    
Failing that, you might try adding su $USER after the root commands, and before those to be run as the current user; I'm pretty sure every bash that hasn't been twisted into complete perversity sets $USER, so that should be able to serve as a generic method of producing a shell with the logged-in user's permissions. –  Aaron Miller Sep 30 '13 at 16:21
    
Are you sure everything happens as you write it? sudo su should open a root shell for you and the script should not advance until you leave it. Then [su commands] will be executed (with your user account) and finally exit ends the script. Consider adding whoami at several positions to the script to see who is the active user at different points in the script. –  Tim Sep 30 '13 at 16:23
    
Failing that, what if you just su - instead of sudo su? That should prompt for the root password and then produce an elevated shell, but I'm uncertain how the following exit will behave in a shell script context -- this is why it's often preferable to use Perl or Python for these sorts of tasks; they may seem to present a higher bar to entry, but their much more predictable and less warty behavior more than repays the effort of gaining familiarity. –  Aaron Miller Sep 30 '13 at 16:23
    
@AaronMiller (2nd post) I would not do that. If everything is working as written by TomJ this would mean that after leaving this inner user shell the user (or script) would is given root privileges again. –  Tim Sep 30 '13 at 16:26
show 3 more comments

2 Answers 2

up vote 2 down vote accepted

Have you considered modifying the script to prefix sudo to the commands needing to be run as root? sudo's inbuilt credential caching mechanism will mean you should only need to answer a password prompt once, unless the shell script runs for a very long time.

share|improve this answer
    
Or one can put the sequence of commands to be executed through sudo into a shell script and just do sudo script. –  kju Sep 30 '13 at 20:54
add comment

Dont use "sudo su" or su - , because you open a subshell with this command. This wont work really well.

Pls use

sudo "su commands" for example sudo "ls /home"

if you really want use sudo su, you can use it by typing for example

sudo su -c "ls /tmp"

if you have more then one command, separate it by ";"

sudo su -c "ls /tmp/; whoami"

Regard Chris

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.