LinuxQuestions.org
Help answer threads with 0 replies.
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 01-04-2005, 06:33 PM   #1
tawalker
Member
 
Registered: Jan 2004
Location: UK
Distribution: Red Hat 9
Posts: 52

Rep: Reputation: 15
Root user check in bash script doesn't work


I occasionally put together shell scripts to automate tasks, which would otherwise need me to remember very long commands (e.g. converting digital camera movie clips into DivXs for our Web site). I'm not really a programmer, but can usually find enough advice via Google to get by.

One small script I'm trying to get working, assigns a disk image file (which I use for running FreeDOS under Bochs) to the loopback device and then mounts it, so I can copy files to and from the image.

Because the "mount" command is involved, I want the script to check whether the user is logged in as superuser, and at the very least, exit if not. I found this routine and tried it as a non-superuser:
Code:
if [ $UID -ne $ROOT_UID ]
then
        echo "Must be root to run this script."
        exit $E_NOTROOT
fi
It gave me this message:
Code:
[: 500: unary operator expected
...and didn't exit, so the other two operations (loopback, and mount) failed as I was not the superuser.

Does anyone know what might be wrong with the above code? Alternatively, how could the script be modified so that "sudo" would be called if the user was not root? (I would be the only user who could execute it.)

Many thanks,
Tim.
 
Old 01-04-2005, 07:27 PM   #2
leonscape
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Debian SID / KDE 3.5
Posts: 2,313

Rep: Reputation: 48
What your missing is quotes around the variables try:

Code:
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."
  exit $E_NOTROOT
fi
To run sudo just call sudo from the script and check its output for success. The Advanced Bash-Scripting Guide has the details of how to do this.
 
Old 01-04-2005, 07:43 PM   #3
wapcaplet
LQ Guru
 
Registered: Feb 2003
Location: Colorado Springs, CO
Distribution: Gentoo
Posts: 2,018

Rep: Reputation: 48
You could also test like this:

Code:
if [[ `whoami` != "root" ]]; then
...
I usually use the double-brackets, to avoid that annoying "unary operator expected" error.
 
Old 01-05-2005, 04:51 PM   #4
tawalker
Member
 
Registered: Jan 2004
Location: UK
Distribution: Red Hat 9
Posts: 52

Original Poster
Rep: Reputation: 15
I tried wapcaplet's suggestion, and this halts the script if the user is not root - just what I wanted, so many thanks

leonscape: thanks for the link to the Advanced Bash-Scripting Guide, which I think I will be perusing heartily before long...

Much obliged to you both!

Thanks,
Tim
 
Old 09-15-2011, 11:55 AM   #5
ilane
LQ Newbie
 
Registered: Sep 2011
Location: Massachusetts
Distribution: Fedora, Oracle Linux
Posts: 1

Rep: Reputation: Disabled
Checking for user with root privileges

In the case where you want to check if invoking user has invoked script via sudo (and therefore has root privileges), the 'id' command will give you the effective user and group id.
 
Old 09-15-2011, 05:42 PM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
UID is a preset bash shell variable, but ROOT_UID is not. Unless you've defined it previously in the script, it will be null, breaking the test. Since the root uid is almost always "0", is there any reason not to use that instead?

Also, numeric comparisons like this are better done with ((..)), and string and complex comparisons with [[..]]. The old [ command has some weaknesses, and is really only necessary when writing portable, posix-compliant scripts.
Code:
if (( UID != 0 )); then

#or this evaluates the same:

if (( UID )); then
"0" in an arithmetic test evaluates to false, and all other values will be true. So any user other than root will cause the subcommands to execute.

http://mywiki.wooledge.org/ArithmeticExpression
http://mywiki.wooledge.org/BashFAQ/031

Last edited by David the H.; 09-15-2011 at 05:51 PM. Reason: mistake
 
1 members found this post helpful.
Old 12-18-2011, 03:06 AM   #7
gnuzilla
LQ Newbie
 
Registered: Apr 2006
Location: Chagrin Falls, OH
Distribution: Ubuntu / Debian
Posts: 17

Rep: Reputation: Disabled
Cool check for root user, in POSIX and Bash

I really dislike using 'if's, not that they are wrong, but I find that the exit code is usually enough.
For example, If you are not concerned about using Bashisms

Code:
(( `id -u` )) && echo "Must be root" && exit 1

## Bash's built-in (('s return true for any non zero value.
## If your running as root, `id -u` would be 0
Personally I also like most of my scripts to run with dash, unless I have reason to explicitly call bash.
For example, I prefer this POSIX equivalent.

Code:
[ `id -u` -ne 0 ] && echo "Must be root" && exit 1

## Some call this ugly, I actually find this cleaner to read.
## You could always replace '['s with the verbose version 'test'
 
  


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
Obtain ip address and check for running process via Bash Script? xconspirisist Programming 10 09-12-2008 01:18 PM
Shell Script to check root user? kushalkoolwal Programming 4 09-22-2005 12:15 AM
how do I make sure that the user that is going to run the script is a root user??? nikold01 Linux - General 3 09-10-2004 07:54 AM
Scanner to work as USER and not forced as ROOT Root (Suse 9.1) 1kyle Linux - Hardware 0 07-10-2004 08:51 AM
IntelliMouse thumb buttons work as root, broken as non-root user, wheel works always digital vortex Linux - Hardware 7 03-02-2004 04:14 PM

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

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