Industry guru Dave Taylor offers free tech support on a wide variety of technical and business topics, including HTML, online advertising, Cascading Style Sheets, Web design, management, Unix, Linux, search engine optimization, online dating, Mac OS X, shell script programming and Microsoft Windows.

How can shell scripts check user 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!!AAAARRGGGGGGHHH!

I am looking for a way, when the script fires, to see who is running it.


Dave's Answer:

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:

$ whoami
taylor

That should be sufficient to give you the clue on how to implement the test you seek:

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:

if [ $(whoami) = "root" ]
then
  echo "You cannot run this script as root."
  exit 1
fi

Hope that's helpful!



Help others find this article at Del.icio.us, Digg, Netscape, Reddit, and Stumble Upon    

Subscribe!

Never miss another useful Q&A article again! Subscribe to AskDaveTaylor with Google Reader.

Comments

just like whoami gives u the user name, i want to know a thinkg which shud tell me
whereami i.e where am i on file system

any 1 help

Posted by: Abdul sami at March 14, 2006 3:56 AM

Um, that'd be one of the most basic Unix commands:

pwd

Posted by: Dave Taylor at March 14, 2006 10:46 PM

if you wanted you could also use environment variables to solve both of these problems,

echo $USER - user name
echo $LOGNAME - logname
echo $PWD - print working directory

to view all environment variables type 'printenv' (no quotes)

Posted by: Jeff at June 2, 2006 9:59 AM

how can i check if the user exist or not.
note that I can check it in /etc/passwd
but I wanna make it in script to tell me directly if the user exist or not.
same for the group.

thank you.

Posted by: sultan at April 4, 2007 2:18 AM

how can add a new user account in the text mode or terminal

Posted by: kirankumar at May 3, 2007 5:23 AM

What about using the bash shell's built in $UID variable?

It only prints out a number, but makes for easy checks against root (0).

Posted by: gjjones1 at May 4, 2007 10:23 PM

ggjones1, I don't think that'd be too reliable. What if I ran a script like this:

UID=0 runmyscript.sh

and then had a simple test internally to see if UID was zero?

Posted by: Dave Taylor at May 4, 2007 10:50 PM

in fact UID don't become 0 if user use "sudo" instead of become root

Posted by: cpen at July 26, 2007 3:44 AM

Thanks for the information. Was helpful with my script which needs to be executed as root.

Thanks a bunch!!!

Cheers!

Posted by: Dillon at January 7, 2008 2:40 PM

How to you do this within a tsch shell?

Posted by: ZenBug at February 20, 2008 12:11 PM

to get the "real" user (even if you use "su -"), the who command can be used with either the -m flag OR in the form who ARG1 ARG2 ("who am i" or "who yo daddy").

id or $UID will NOT give you this if you use the "-" after the su command :)

the "id" command can give you JUST the current user id (uid) or JUST the current group id (gid) using the -u or -g flags respectively. This is nice for shell scripts like:

--------------------------------------------
if [ `id -u` -ne 0 ]
then
echo "You must be root to execute `basename $0`"
fi

Posted by: DDreggors at December 9, 2008 1:16 AM

I have a lot to say, but ...
Starbucks coffee cup I have a lot to say, and questions of my own for that matter, but most of all I'd like to say thank you for all your efforts on this Web site by buying you a chai!

I do have a comment, now that you mention it!









Remember personal info?


Please note that I will never send you any unsolicited commercial email. Ever.

While I'm at it, please note that by submitting a question or comment you're agreeing to my terms of service, which are: you relinquish any subsequent rights of ownership to your material by submitting it on this site.









Uniblue: Free Virus Scan

Search
Find just the answers you seek from among our 2000+ free tech support articles by using our Lijit search engine.


Help!





Subscribe to
Ask Dave Taylor!

Add to Google Reader
Add to My Yahoo!
Subscribe in NewsGator Online

RDF   XML

Free Updates!
Sign up and get free weekly updates and special offers on books, seminars, workshops and more.


Recent Entries
Join the List!
Join my author info mailing list, where you'll learn about my upcoming books, speaking gigs, and more!


Book Links
© 2002 - 2009 by Dave Taylor. All Rights Reserved.

Note: This web site is for the purpose of disseminating information for educational purposes, free of charge, for the benefit of all visitors. We take great care to provide quality information. However, we do not guarantee, and accept no legal liability whatsoever arising from or connected to, the accuracy, reliability, currency or completeness of any material contained on this web site or on any linked site.

[whiteboard marker tray]
"Ask Dave Taylor®" is a registered trademark of Intuitive Systems, LLC.