Test for root.

Top Page
Attachments:
Message as email
+ (text/plain)
Delete this message
Reply to this message
Author: Matt Alexander
Date:  
Subject: Test for root.
On Thu, 6 Sep 2001, Carl Parrish wrote:

> Okay I'm writting a script that tests for root status. like so
>
> |
>
> test `whoami` = 'root' || echo "You must be root to execute the commands."
>
> it never seems to be able to pass the test. From the command line I can run whoami and get back root but it never works in the script.
> Any idea what I'm doing wrong?



I don't see anything wrong with what you have above, but I want to caution
you about using this as a test for anything important. Consider this:

I create a shell script called "whoami" and place it in $HOME/bin.
My new-and-improved "whoami" script contains the following:

#!/bin/sh
echo root

Then I change my $PATH, like this:
PATH=$HOME/bin:$PATH

Now when I run your script, the test for whoami will return root. Most
likely your script would require root to run whatever following commands
you might have and that you added this line just to notify people that
they can't successfully procede. In any case, be careful when you're
writing shell scripts.
~M