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
Post a Comment