LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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 11-30-2008, 07:06 AM   #1
radiodee1
Member
 
Registered: Oct 2006
Location: New York
Distribution: Debian
Posts: 673
Blog Entries: 11

Rep: Reputation: 36
detecting root user in a bash script


I'm writing a simple bash script and I want the script to be able to detect weather or not the user running the script is the root user or not. Does anybody know how to do this? Also, can it be done in a way that's not dependent on which distribution you're running? Thanks.
 
Old 11-30-2008, 07:12 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Hi,

Code:
if [[ $UID -ne 0 ]]
then
  echo "$0 must be run as root"
  exit 1
fi
UID is a variable that is set by the shell and holds the uid of the user that started the shell/script. 0 (zero) is (always) the root user.

The above code snippet checks to see if the user is root, if not it prints a message and exits.

Hope this helps.
 
Old 11-30-2008, 07:18 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
The LinuxCommand tutorial gives an example of exactly what you're looking for.

Code:
if [ $(id -u) = "0" ]; then
    echo "superuser"
fi
 
  


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 04:06 AM
Granting root permissions to a bash script than can be run by a user? SirTristan Linux - Newbie 4 06-14-2008 11:29 PM
bash script for detecting USB CD-ROM mksc Linux - Newbie 1 05-23-2008 05:12 AM
bash script help - detecting if media is present dv502 Programming 4 05-17-2007 08:12 AM
Getting bash script files to run as root user helptonewbie Programming 5 04-13-2007 10:09 PM


All times are GMT -5. The time now is 06:39 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration