0

I'm testing if i can run the script as user1 then sudo as user2 to execute a section of the script as user, then revert back to user1 afterwards.

Apparently my script gets stuck after the first sudo and does not execute the succeeding lines. How can this be resolved?

Apologies as I'm really not experienced in shell scripting

#!/bin/bash
whoami
ls -lrt /myfolder
sudo -i -u user2
whoami

sftp ${othersvr} <<EOF
cd /tgtpath
lcd /myfolder
get -p *.txt
exit
EOF

sudo -i -u user1
whoami
ls -lrt /myfolder
2
  • you have to exit from each invocation of sudo (I'm pretty sure). Good luck. Commented Aug 24, 2017 at 4:09
  • still getting stuck with user2 after the first sudo. the command after the sudo to user2 doesnt get executed Commented Aug 24, 2017 at 5:08

1 Answer 1

1

You should find a better way of doing what you want, like using ACLs or something.

However, if you want to do it this way, take into account that sudo can receive a command

sudo -i -u user2 'whoami; ls -lrt /myfolder'
sudo -i -u user1 'whoami; ls -lrt /myfolder'
Sign up to request clarification or add additional context in comments.

2 Comments

how can i properly do that if the command i want to be executed is an sftp. I have updated my example below for your reference
Just create a script doing all the steps you want to do (and to simplify escaping) and invoke it from sudo -i -u user script

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.