Getting User ID in Bash

I want to just check to see the user id of the user running the bash script. The script needs to be run as root, so I want to make sure root is executing the script.
dignifiedAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MysidiaCommented:
User identification is provided by one of the standard bash variables...

http://www.gnu.org/software/bash/manual/bashref.html#SEC62
0
trigger-happyCommented:
the env variable is $USER
you can use that for finding out who the user is.

--trigger-happy
0
jlevieCommented:
You can use the id command for this, which works for any shell. For example:

wilowisp> id
uid=1716(jlevie) gid=11(noc) groups=11(noc)
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

MysidiaCommented:
USER is not a shell built in, it may be set, but it is not built in...
(no guarantee it would be set)

besides which it can easily be set by the person trying to run the script via
export USER=root

More importantly... it won't be set to root necessarily when a user su's to
root via "su"..  USER typically reflects the user at login time..  in this
sense "USER" is similar to the "LOGNAME" variable

What would be more appropriate to use would be a variable
like EUID or UID
0
trigger-happyCommented:
oh ok, thnx for correcting me

--trigger-happy
0
wesly_chenCommented:
Hi,

#!/bin/sh

for USERS in `ps -o "%U : %a" | grep -v grep | grep bash | awk -F: '{print $1}'`
do
  id $USERS
done

For more information about "id", do
man id

Wesly
0
MysidiaCommented:
Who cares what other users are running bash?

If you want to use ps, then do

ps -o %U $$ |tail -1 |xargs id
0
MysidiaCommented:
Err, oops.. that's just weird, don't use programs like PS to get the current ID,
A more practical approach was mentioned earlier
0
wesly_chenCommented:
Thanks Mysdia for pointing out "ps -o %U" issue.

#!/bin/sh

for USERS in `ps -elf | grep -v grep | grep bash | awk  '{print $3}'| sort -u`
do
  id $USERS
done

Wesly
0
mike_mianCommented:
`you could  just set the permission fo the script so that no-none but root can run it.
ie
chmod 700 <scriptname>

This sets teh permission on hte script to readwrite and execute  root only.

I am assuming that the script is owned by root and you have access to the root if not so can change the ownership (eg chown root <scriptname>)
0
bryanlloydharrisCommented:
Using the info from the first post:

$ echo $EUID
500

Bryan
0
MysidiaCommented:
Right, specifically:

if [ $EUID -ne 0 ] ; then
  echo "Sorry, this script must be run as root"
  exit 0
fi
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.