menu_l Home Search Members Calendar Help Donatemenu_r Current time: 01-18-2022, 06:58 PM menu_l

Hello There, Guest!

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5

Thread Contributor: RazorDOXHow can shell scripts check user's ID

08-03-2010, 04:11 PM,
How can shell scripts check user's ID
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!
This tutorial shows you how to see who is running it.
There are several ways to do that: using the Unix command id to check the user's ID, or, even more easily, using whoami
When run by itself, whoami looks like this:
$ whoami
That should be sufficient to give you the clue on how to implement the test you seek:
if [ $(whoami) = "joe" ]
  execute the code, we're the right user
  echo "You must be user 'joe' to run this script."
  exit 0
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:
if [ $(whoami) = "root" ]
  echo "You cannot run this script as root."
  exit 1
Add Thank You Reply
08-04-2010, 08:33 AM,
How can shell scripts check user's ID
Awesome!! Big Grin
Add Thank You Reply

Forum Jump:

Users browsing this thread: 1 Guest(s)