mtryingtocode
mtryingtocode

Reputation: 969

UNIX bash switch users back and forth

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

Upvotes: 0

Views: 88

Answers (1)

Diego Torres Milano
Diego Torres Milano

Reputation: 69218

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'

Upvotes: 1

Related Questions