Using the following you can check if you are root or not when you run bash scripts.
Exmaple 1:
[php]
#!/bin/bash
if [ $(whoami) != 'root' ]; then
echo "Must be root to run $0"
exit 1;
fi
[/php]
Example 2:
[php]
#!/bin/bash
ROOT_UID=0 # Only users with $UID 0 have root privileges.
E_NOTROOT=87 # Non-root exit error.
if [ "$UID" -ne "$ROOT_UID" ]
then
echo "Must be root to run this script!"
exit $E_NOTROOT
fi
echo "You are root!"
[/php]
You can also use this in your bash scripts.

October 21st, 2011 on 02:15
hello
example #2 is usually more portable so it’s a better idea to use this one.
One example where #1 wouldn’t port very well, is on FreeBSD systems where by default you have two root users (with uid 0). One named ‘root’ and one named ‘toor’.