-   Programming (
-   -   Root user check in bash script doesn't work (

tawalker 01-04-2005 07:33 PM

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:

if [ $UID -ne $ROOT_UID ]
        echo "Must be root to run this script."
        exit $E_NOTROOT

It gave me this message:

[: 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,

leonscape 01-04-2005 08:27 PM

What your missing is quotes around the variables try:


ROOT_UID=0    # Only users with $UID 0 have root privileges.
E_NOTROOT=67  # Non-root exit error.

if [ "$UID" -ne "$ROOT_UID" ]
  echo "Must be root to run this script."
  exit $E_NOTROOT

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.

wapcaplet 01-04-2005 08:43 PM

You could also test like this:


if [[ `whoami` != "root" ]]; then

I usually use the double-brackets, to avoid that annoying "unary operator expected" error.

tawalker 01-05-2005 05:51 PM

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!


ilane 09-15-2011 12:55 PM

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.

David the H. 09-15-2011 06:42 PM

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.

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.

gnuzilla 12-18-2011 04:06 AM

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


(( `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.


[ `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'

All times are GMT -5. The time now is 09:10 PM.