Visit Jeremy's Blog.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 08-16-2005, 04:43 PM   #1
Registered: Sep 2004
Location: western massachusetts
Distribution: fedora core 3, Suse 10
Posts: 877

Rep: Reputation: 35
how to test if user is root or not in a shell script?

hi im making a script that will excecute some commands but you need to be root to do so but i don't know how to check to see if the current user is root or not - how would i do so? Thanks for any replies in advance.

edit: i just realized that i could put the output of whoami to a variable and test against that. but is there another way to do so to increase teh portability of the script? Were does whoami get it from?

Last edited by dr_zayus69; 08-16-2005 at 04:45 PM.
Old 08-16-2005, 04:46 PM   #2
LQ Newbie
Registered: Aug 2004
Distribution: suse 9.1 pro, Slack 10.0, Gentoo 2004.2
Posts: 24

Rep: Reputation: 15

root's UID=0
Old 08-16-2005, 04:58 PM   #3
Registered: Sep 2004
Location: western massachusetts
Distribution: fedora core 3, Suse 10
Posts: 877

Original Poster
Rep: Reputation: 35
thanks, that seems more concise then capturing the output of whoami.
Old 08-17-2005, 04:00 AM   #4
Senior Member
Registered: Sep 2003
Location: Egypt
Distribution: Arch
Posts: 1,528

Rep: Reputation: 47
# Run as root

ROOT_UID=0     # Only users with $UID 0 have root privileges.
E_NOTROOT=67   # Non-root exit error.

if [ "$UID" -ne "$ROOT_UID" ]
 echo "Must be root to run this script , type su to change to root."
 exit $E_NOTROOT


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell Script to check root user? kushalkoolwal Programming 4 09-22-2005 01:15 AM
allow sudo for www user to run root shell script cccc *BSD 12 07-23-2005 04:48 PM
Shell script to test my shell alltime Programming 5 04-22-2005 08:24 PM
Req. Shell script: Network performance test altu Solaris / OpenSolaris 6 08-09-2004 12:45 AM
creating shell script that executes as root regardless of who runs the script? m3kgt Linux - General 13 06-04-2004 11:23 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:13 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration