LinuxQuestions.org
Review your favorite Linux distribution.
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 09-21-2005, 01:13 PM   #1
kushalkoolwal
Senior Member
 
Registered: Feb 2004
Location: Middle of nowhere
Distribution: Debian Squeeze
Posts: 1,249

Rep: Reputation: 49
Shell Script to check root user?


Hi,

I am writing a shell script which has various admininstration commands and therefore the script is to be run as a 'root' user. How can I check in the beginning my script whether the user is logged on as a 'root' user or not?
 
Old 09-21-2005, 01:30 PM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

One way is to check the UID (which is set by the shell) of the current user (should be 0 for root).

I.e.:
Code:
if [[ $UID == "0" ]]
then
  <root commands>
fi
Hope this helps.
 
Old 09-21-2005, 02:32 PM   #3
sirclif
Member
 
Registered: Sep 2004
Location: south texas
Distribution: fedora core 3,4; gentoo
Posts: 192

Rep: Reputation: 30
well, if you only want the root user to be able to run the script, you could just let root own the script and set the permissions so that only root can run it.

If all users are allowed to run the script, but only root users are allowed to access the full functionalitiy through some command line options or something, then you would probably want check inside your script.
 
Old 09-21-2005, 02:51 PM   #4
kushalkoolwal
Senior Member
 
Registered: Feb 2004
Location: Middle of nowhere
Distribution: Debian Squeeze
Posts: 1,249

Original Poster
Rep: Reputation: 49
Thanks it worked!!!
 
Old 09-22-2005, 12:15 AM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,344

Rep: Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746
Or look into sudo ...
 
  


Reply


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
Root user check in bash script doesn't work tawalker Programming 6 12-18-2011 03:06 AM
shell script to check ftp communication yuva_mca Linux - General 2 12-01-2005 07:15 AM
how to test if user is root or not in a shell script? dr_zayus69 Programming 3 08-17-2005 03:00 AM
allow sudo for www user to run root shell script cccc *BSD 12 07-23-2005 03:48 PM
Shell Script to Check apache installed or not. apt Programming 7 12-06-2004 11:07 PM

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

All times are GMT -5. The time now is 06:17 AM.

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