LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Root user check in bash script doesn't work (http://www.linuxquestions.org/questions/programming-9/root-user-check-in-bash-script-doesnt-work-273911/)

tawalker 01-04-2005 06: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:
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.

leonscape 01-04-2005 07:27 PM

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.

wapcaplet 01-04-2005 07:43 PM

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.

tawalker 01-05-2005 04: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!

Thanks,
Tim

ilane 09-15-2011 11:55 AM

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 05: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.
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

gnuzilla 12-18-2011 03: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

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'



All times are GMT -5. The time now is 06:38 PM.