| 1 | #!/bin/bash |
| 2 | # am-i-root.sh: Am I root or not? |
| 3 | |
| 4 | ROOT_UID=0 # Root has $UID 0. |
| 5 | |
| 6 | if [ "$UID" -eq "$ROOT_UID" ] # Will the real "root" please stand up? |
| 7 | then |
| 8 | echo "You are root." |
| 9 | else |
| 10 | echo "You are just an ordinary user (but mom loves you just the same)." |
| 11 | fi |
| 12 | |
| 13 | exit 0 |
| 14 | |
| 15 | |
| 16 | # ============================================================= # |
| 17 | # Code below will not execute, because the script already exited. |
| 18 | |
| 19 | # An alternate method of getting to the root of matters: |
| 20 | |
| 21 | ROOTUSER_NAME=root |
| 22 | |
| 23 | username=`id -nu` # Or... username=`whoami` |
| 24 | if [ "$username" = "$ROOTUSER_NAME" ] |
| 25 | then |
| 26 | echo "Rooty, toot, toot. You are root." |
| 27 | else |
| 28 | echo "You are just a regular fella." |
| 29 | fi |