Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I'm running a remote bash script (using ssh -t 'bash doscript.sh'). In "doscript.sh" I have a sudo su anotheruser. At that point, the script seems to push a shell and I'm left in that "interactive" script and the rest of my script (doscript.sh) doesn't run unless I type "exit". Is there a workaround that gets around this problem?

Thanks in advance.

Here is the contents of my test script "doscript.sh":

sudo su diy
cd
pwd
whoami
exit
whoami
share|improve this question
    
possible cross site duplicate of: stackoverflow.com/questions/1988249/… –  Ciro Santilli May 24 at 6:31

2 Answers 2

up vote 3 down vote accepted

sudo starts a shell unless you instruct it otherwise.

It looks like you actually want to run this script as another user. To do that, try something like this:

#!/bin/bash
if [ `id -nu` != diy ]; then
    sudo -u diy $0 # Re-run this script as user diy
else
    # Everything you want to do goes here
fi

Keep in mind that /etc/sudoers must be set up to allow the original user to run this script as the new user.

share|improve this answer
    
Allthough for bash (not bourne shell) it's more convenient to use if [[ $(id -un) != diy ]]; then.. form of checking. –  val0x00ff Jan 14 '13 at 13:08

su as used in you script only runs the su line itself as the other user.

Use su - otheruser -c "cmds to execute".

share|improve this answer

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.