How do I force the user to become root
I have a standard function I use in bash
for this very purpose:
# Check if we're root and re-execute if we're not.
rootcheck () {
if [ $(id -u) != "0" ]
then
sudo "$0" "$@" # Modified as suggested below.
exit $?
fi
}
There's probably more elegant ways (I wrote this AGES ago!), but this works fine for me.
To use this function consistently in a script, you should pass it the arguments received by the script originally.
Usage: rootcheck "${@}"
(rootcheck
alone will not pass any arguments to the rootcheck function)
i am using this single line version, easy to copy paste on top of scripts
[ `whoami` = root ] || { sudo "$0" "$@"; exit $?; }
sudo chown root yourcode
sudo chmod 500 yourcode
Voila!