
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. 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
Categorized:
Shell Script Programming
(Article 4306)
Tagged: Previous: How do I delete all but one directory in Linux? Next: Difference between software copyright and software patents? Subscribe!
Never miss another useful Q&A article again! Subscribe to AskDaveTaylor with Google Reader. just like whoami gives u the user name, i want to know a thinkg which shud tell me any 1 help Posted by: Abdul sami at March 14, 2006 3:56 AMUm, that'd be one of the most basic Unix commands: pwd Posted by: Dave Taylor at March 14, 2006 10:46 PMif you wanted you could also use environment variables to solve both of these problems, echo $USER - user name to view all environment variables type 'printenv' (no quotes) how can i check if the user exist or not. thank you. Posted by: sultan at April 4, 2007 2:18 AMhow can add a new user account in the text mode or terminal Posted by: kirankumar at May 3, 2007 5:23 AMWhat 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 PMggjones1, 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 PMin fact UID don't become 0 if user use "sudo" instead of become root Posted by: cpen at July 26, 2007 3:44 AMThanks 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 PMHow to you do this within a tsch shell? Posted by: ZenBug at February 20, 2008 12:11 PMto 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: -------------------------------------------- I have a lot to say, but ...
I do have a comment, now that you mention it!
|
![]()
Search
Find just the answers you seek from among our 2300+ free tech support articles by using our Lijit search engine.
Help!
Subscribe to
Ask Dave Taylor!
Free Updates!
Sign up and get free weekly updates and special offers on books, seminars, workshops and more.
Articles and Reviews
Auctions and Online Shopping Blogs and RSS Feeds Building Web site traffic Business and Management Cell Phones and Mobile Phones CGI Scripts and Web Site Programming Computer and Internet Basics d) None of the Above HTML and CSS Industry News and Trade Shows Mac OS X Help MySpace, Facebook, Twitter and Social Network Help Pay Per Click (PPC) Search Engine Optimization Shell Script Programming Sony PSP, MP3 Players, Etc. The Writing Business Unix and Linux Help Video Game Tips and Help Windows Help
Recent Entries
Book Links
|