We have several different shell scripts that we run on a daily basis. Sometimes one of the computer operators will execute the shell script as the wrong user (Root for example). This screws up the permissions and owenership on all of the files that the script just touched!!AAAARRGGGGGGHHH!
I am looking for a way, when the script fires, to see who is running it.
The solution to this challenge is surprisingly straightforward, and it revolves around using the Unix command id to check the user’s ID, or, even more easily, using whoami (not to be confused with a Jackie Chan movie of the same name!)
When run by itself, whoami looks like this:
That should be sufficient to give you the clue on how to implement the test you seek:
execute the code, we’re the right user
echo “You must be user ‘joe’ to run this script.”
You can modify this to match the user or set of users you want to allow, or you can negate the logic to screen out bad userIDs immediately, like this:
echo “You cannot run this script as root.”
Hope that’s helpful!