Script

Hi, I am trying to login as user1 and run a script which will sudo to a user2 and executes some commands then exit from that user and then execute rest of commands in user1.

Login as user1 and executed this script.

 #!/bin/bash
user=$(echo `who am i`| awk '{print $1}')
echo the user is $user
sudo su - user2
mkdir /app/test1
exit 
ls -ltr

its sudoing to user2 and its not exiting.
Please advise. Thanks for your help.

Consider making two scripts: the main one

# run as user1
sudo user2 ./path/to/script2
# run the code user1 has to execute after this line ...

user1 in /etc/sudoers has to be explicitly declared able to execute that script2 as user2 with the nopasswd option. Do not grant ALL to user1.

I am trying to have all commands in one script. Is it possible.