Self-elevating a script to root permissions |
Trick to elevate a script to root permissions |
FeedbackBy Preston Hunt, 05 November 2011 |
I don't know why it took me so long to figure this out, but here is a nice little recipe for elevating a script to root permissions from inside the script.
#!/usr/bin/env bash
if (( UID != 0 )); then
echo Re-running as root
exec sudo -E "$0" ${1+"$@"}
fi
echo Script is running as root
echo Here are my arguments:
for i in "$@"; do echo arg=[$i]; done
id
If you don't want to export the user's environment variables, replace "sudo -E" with "sudo".
blog comments powered by Disqus