Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I determine if a shell script is running with root permissions?

I've got a script I want to require be run with su privileges, but the interesting scripted command that will fail comes very late in the script, so I'd like a clean test up front to determine if the scrip will fail without SU capabilities.

What is a good way to do this for bash, sh, and/or csh?

like image 942
F. Randall Farmer Avatar asked Jun 15 '11 18:06

F. Randall Farmer


1 Answers

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/csh
if ( `id -u` == "0" ) then
        echo "I AM ROOT, HEAR ME ROAR"
endif
like image 138
Nick ODell Avatar answered Sep 28 '22 04:09

Nick ODell