Forum

 
menu_l Home Search Members Calendar Help Donatemenu_r Current time: 12-06-2021, 01:34 AM menu_l

Hello There, Guest!
(LoginRegister)

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,
#1
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:
Code:
$ whoami
taylor
That should be sufficient to give you the clue on how to implement the test you seek:
Code:
if [ $(whoami) = "joe" ]
then
  execute the code, we're the right user
else
  echo "You must be user 'joe' to run this script."
  exit 0
fi
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:
Code:
if [ $(whoami) = "root" ]
then
  echo "You cannot run this script as root."
  exit 1
fi
Add Thank You Reply
08-04-2010, 08:33 AM,
#2
How can shell scripts check user's ID
Awesome!! Big Grin
Add Thank You Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)