LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 08-16-2005, 03:43 PM   #1
dr_zayus69
Member
 
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 03:45 PM.
 
Old 08-16-2005, 03:46 PM   #2
mai1man
LQ Newbie
 
Registered: Aug 2004
Distribution: suse 9.1 pro, Slack 10.0, Gentoo 2004.2
Posts: 24

Rep: Reputation: 15
$UID

root's UID=0
 
Old 08-16-2005, 03:58 PM   #3
dr_zayus69
Member
 
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, 03:00 AM   #4
heema
Senior Member
 
Registered: Sep 2003
Location: Egypt
Distribution: Arch
Posts: 1,528

Rep: Reputation: 47
Code:
# 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" ]
then
 echo "Must be root to run this script , type su to change to root."
 exit $E_NOTROOT
fi
 
  


Reply



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 12:15 AM
allow sudo for www user to run root shell script cccc *BSD 12 07-23-2005 03:48 PM
Shell script to test my shell alltime Programming 5 04-22-2005 07:24 PM
Req. Shell script: Network performance test altu Solaris / OpenSolaris 6 08-08-2004 11:45 PM
creating shell script that executes as root regardless of who runs the script? m3kgt Linux - General 13 06-04-2004 10:23 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:24 PM.

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