checking uid

How do i go about getting the uid of the user and verify ?

if [ $uid != 'root' ] 
then 
        echo "You are not a superuser, please login as a superuser"
        exit1; 
fi

the above code doesn't work. can some guru please help me.

  1. how to get the uid of the user ? i know by typing id but how to get the script to verify its a su?
  2. how do i exit the whole script if he/she is not a su?
  3. Is the above, able to continue the rest of the process if its a su?

-Thanks & Regards-

use $USERNAME

if [ $USERNAME != 'root' ]
 then
  echo "you are not root"
 else
  echo "you are root"
 fi

hmmm ... i echo $USERNAME and i got nothing

which shell and os are you using ?

else you might try

id | sed 's/).*$//;s/^.*(//'

i'm using sun solaris 9, bash

$ ps -p $$
   PID TTY      TIME CMD
 10387 pts/4    0:00 bash
$ id
uid=0(root) gid=1(other)
$ echo $UID
0
$ [ "$UID" -eq 0 ]||{ echo 'Go away!';exit 1;}
$ su - oracle
$  [ "$UID" -eq 0 ]||{ echo 'Go away!';exit 1;}
Go away!
logout

thanks :slight_smile: got it thank you so much