Multiple su - in Korn Shell script

i am trying to do something like this :


#!/bin/ksh
# Change to the userid user1
su - user1

#Issue the command to change directory and list files
cd /home/user1/
ls -lrt
exit				#Come out of the user1 to root again
 
#change to user 2

su - user2
cd /home/user2/
ls -lrt
exit				#back to root again


#come out of the script
done				# Is this needed?

Trying to execute this as root ( with appropriate permissions) however only the first line executes. Where am i going wrong?

Thanks

The su command spawns a subshell with the new id. This subshell won't have access to the commands that the parent shell is running. Instead the su command just fires up a new shell which reads input from the terminal. Your script is waiting for you you to exit from that shell. Then it will put up after the su command.

You may be able to use something like:
su user -c " cd /somewhere ; ls -l"

Thanks Perderabo. That works.