Solved How to find out if script is being run as root

Hey guys,

Not to compare FreeBSD to Linux but in Linux one can use the following code in a .sh file to check if it is being run as root, is there a similar solution for FreeBSD i.e. to check if FreeBSD is running the script as root.

Code:
#!/bin/sh
# Init
FILE="/tmp/out.$$"
GREP="/bin/grep"
#....
# Make sure only root can run our script
if [[ $EUID -ne 0 ]]; then
   echo "This script must be run as root" 1>&2
   exit 1
fi

/iSh0w
 
Please pardon my ignorance, I keep getting
Code:
[:missing]
with the following code:

Code:
#!/bin/sh
# Init
# Make sure only root can run our script
idusr=$(id -u);
rootusr=0;
if [ x${idusr} != x${rootusr}]; then
   echo "This script must be run as root"
   exit;
fi

Probably my if-else syntax/statement is flawed

/iSh0w
 
Last edited by a moderator:
You have [ x${idusr} != x${rootusr}]; but you need [ x${idusr} != x${rootusr} ]; (missing a space before ])

You could also write it like this:
Code:
if [ $(id -u) -ne 0 ]; then
   echo "This script must be run as root"
   exit;
fi
id -u should work under Linux too.
 
Ahem, stop with the prefix silliness that is so common in Linux shell scripting. Use proper quoting so that you don't have to use the x prefix:

Code:
if [ "${idusr}" != "${rootusr}" ]; then

The double quotes will guarantee that both sides of the comparison are always defined as empty strings at least.

The reason why you need a space before the ] is that the ] is actually the last argument for test(1) that you're now writing as [ and test(1) is complaining that it can't find the terminating ].
 
If you have similar statements in other places of the script where you need to exit on error, I find an error routine comes in handy:

Code:
[ `id -u` -ne 0 ] && _err "You must be root to run this"

_err(){
    # ... any additional error handling/recording you want ...
    echo "${0}: $1" >&2
    exit 1
}

Also, when trying to fast track learning shell, I found the scripts in /etc/rc.d, written by devs that know what they're doing, highly useful. There's all sorts of clever snippets of code in there.
 
If you have similar statements in other places of the script where you need to exit on error, I find an error routine comes in handy:

Code:
[ `id -u` -ne 0 ] && _err "You must be root to run this"

_err(){
    # ... any additional error handling/recording you want ...
    echo "${0}: $1" >&2
    exit 1
}

Also, when trying to fast track learning shell, I found the scripts in /etc/rc.d, written by devs that know what they're doing, highly useful. There's all sorts of clever snippets of code in there.

Thanks a bunch guys!

/iSh0w
 
Just an idea about an possible "more portable" way....

you can run ps(1) and grep(1) the output to find your script ($0) along with UID.
Below runs on SunOS 5.10 (aka Solaris 10) - I don't have access to a FreeBSD machine now. You need to change the ps(1) command and the column to be displayed (i.e. $3 in the code below) by awk(1) in the FreeBSD version.
Code:
#!/bin/sh
mycmd=`basename $0`
myuid=`ps -l | grep $mycmd | awk '{ print $3}'`
echo $myuid
Instead of backticks, you may want to use $(...) notation, which superseded the `...` form of command substitution. See this link for more details.
 
junovitch@ thank you, I hadn't known about devel/checkbashims. I remember, years ago, doing some bashism on an AIX box, and being annoyed before I realized the problem.

As for $(...) keep in mind it won't work on csh, where backticks will. So if you wanted copy something as root with the date, you'd have to do cp somefile ~scottro/somefile-`date +%F`. Not relevant to this situation, but just something to remember if you're working in csh.
 
Back
Top