Sunday, January 16, 2011

Sailboat Cast Iron Stoves

BASH: verify possession superuser privileges

Sometimes you need to write a script to automate operations, in whole or in part, must be implemented as a superuser (root or root or administrator ). If we want to simplify that the entire script is executed in superuser mode can be useful to determine, prior to kick off the sequence of operations envisaged, if indeed the user that launched the script meets the root. In other words you want to check if the script was launched by the user root or a non-administrator user via the command sudo (on systems with this feature).
The following script provides an example of how to perform this verification.
 # / bin / bash 
if [`id-u`-eq 0] then echo
"Welcome My Lord" exit 0


else echo "Go away, miserable!" Exit 1 fi


For a more concrete example, although very simple.
 # / bin / bash 
if [`id-u`-eq 0] then rm-rf subdir


exit 0 else echo
"subdir not removed: insufficient priviliges"
exit 1 fi


The operating principle is very simple: the command id-u returns the numeric identifier of the user who launched the script. The user ID with administrative privileges is always 0, and if it's root in the person of a simple user using sudo .

NOTE: The symbol ` is not a simple peak and is obtained by pressing AltGr and ' the Italian keyboard layout (as here).

0 comments:

Post a Comment