Hi there,
Putting a sequence of commands into a script isn't
quite the same as typing the same sequence on the command line.
In this case, if you run your sequence manually, you're running the "whoami" command inside "su". In the script form, the "su" completes (and returns you to the root user) before running the next command.
One easy solution might be to do the su outside of the script, i.e. use the command:
Code:
su - userA /path/to/my_script
in your schedule.
Alternately, if you have to do the "su" from within the script, something like this might help:
Code:
#!/bin/bash
MYUSER="userA"
if [ "$USER" != "$MYUSER" ]
then
/usr/bin/su - $MYUSER $0 $*
exit $?
fi
# Rest of original script goes here...
if ..
then
..
else
..
fi
Note that you'll have to call my_script with a full path, or hard code the path on line 5 above instead of $0 for this to work, as you'll be in a different directory after the "su".
Good luck!