How do I determine if a shell script is running with root permissions?
bash/sh:
#!/usr/bin/env bash# (Use #!/bin/sh for sh)if [ `id -u` = 0 ] ; then echo "I AM ROOT, HEAR ME ROAR"fi
csh:
#!/bin/cshif ( `id -u` == "0" ) then echo "I AM ROOT, HEAR ME ROAR"endif
You might add something like that at the beginning of your script:
#!/bin/shROOTUID="0"if [ "$(id -u)" -ne "$ROOTUID" ] ; then echo "This script must be executed with root privileges." exit 1fi