Review your favorite Linux distribution.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 10-07-2006, 01:43 PM   #1
Registered: Aug 2004
Location: London
Distribution: Gentoo
Posts: 277

Rep: Reputation: 30
How to check if i'm root in a script?

Hi there i wanted to make it easier for me to install things by making a script of repetitive or long commands. I was just wondering if there is a way of checking if i'm root. If i am logged in as root then to continue to the next commands and if i'm not then to stop and warn me? I was thinking of something like passing the output of "whoami" to a variable but I dont know how to do this. Is there a more elegant way to do this? or is there a command already out there which does this?

Any help would be appreciated.
Old 10-07-2006, 01:46 PM   #2
Registered: Feb 2004
Location: Northamptonshire, UK
Distribution: Windows XP, Arch Linux
Posts: 131

Rep: Reputation: 16
Well, if the file is owned by root then can't you just chmod the file so that only the owner of the file can execute it?

Or am I missing something?
Old 10-07-2006, 02:12 PM   #3
Registered: Oct 2005
Distribution: Debian etch
Posts: 103

Rep: Reputation: 15
Hell, you could, say, do something like:
whoami > /home/username/whoami
cat whoami > (something)
Then again, you'd still need this variable idea from there, so some command could use it as an argument.
Old 10-07-2006, 02:21 PM   #4
Registered: Oct 2005
Location: Rhode Island, USA
Distribution: Slackware, Xubuntu
Posts: 348

Rep: Reputation: 32
[ $(whoami) == "root" ]
will return 1 (yes) if you are logged in as root and 0 (no) if you are not. The spaces after the open bracket and before the close bracket are necessary because it is actually an alternate of the test statement. If you want to learn more about test (and its alternate [ ]) you can `man test`.
if [ $(whoami) == "root" ]; then
     execute root commands
     warn user
It's a rudimentary implementation but that'll get the job done. I would also consider extrasolar's comment that if it's important that only root execute something then it should be owned and executed by root exclusively. In which case you could chown root.root and chmod 744 the file. I have some that are 700 only but I'm anal with that stuff.

A good compromise if you want to have a script for a user with optional root commands, you could use that if statement and instead of warning a user that you aren't root then you could actually make a separate script with the root commands and call it from that script with `su -c "rootscript"` that way it would ask for a password. It seems crude but it'll work. There are certainly better solutions but I'm not an expert.

Good luck!
Old 10-07-2006, 05:20 PM   #5
Registered: Aug 2004
Location: London
Distribution: Gentoo
Posts: 277

Original Poster
Rep: Reputation: 30
Great, just what i was after, thanks for the prompt help guys/gals. Much appreciated!


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 root user? kushalkoolwal Programming 4 09-22-2005 12:15 AM
Script check Echo Kilo Programming 4 12-13-2004 03:55 AM
creating shell script that executes as root regardless of who runs the script? m3kgt Linux - General 13 06-04-2004 10:23 PM
Ip check script, help me? wiggywag Programming 3 01-13-2004 01:58 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:53 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