Industry guru Dave Taylor answers free tech support questions about a wide variety of business and technical topics, including blogging, Google AdSense, MySpace, Sony PSP, Apple iPod, Mp3 players, management, Linux, SEO, Mac OS X, Facebook, Twitter, LinkedIn 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 Simpy.

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

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.









Search
Find just the answers you seek from among our 1700+ 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 - 2008 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]