How to check if running as root in a bash script -


i'm writing script requires root level permissions, , want make if script not run root, echoes "please run root." , exits.

here's pseudocode i'm looking for:

if (whoami != root)   echo "please run root"    else (do stuff) fi  exit 

how best (cleanly , securely) accomplish this? thanks!

ah, clarify: (do stuff) part involve running commands in-and-of require root. running normal user come error. meant cleanly run script requires root commands, without using sudo inside script, i'm looking syntactic sugar.

the $euid environment variable holds current user's uid. root's uid 0. use in script:

if [ "$euid" -ne 0 ]   echo "please run root"   exit fi 

Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -

java - Digest auth with Spring Security using javaconfig -