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.

Recent Comments