Skip to content

Instantly share code, notes, and snippets.

@James-Firth
Forked from PerpetualBeta/gist:6653157
Last active August 29, 2015 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save James-Firth/a9b620e2293f12be4ab2 to your computer and use it in GitHub Desktop.
Save James-Firth/a9b620e2293f12be4ab2 to your computer and use it in GitHub Desktop.
Bash script header to elevate a script that needs to run as "root" if it is run without sudo. Background here: http://darkblue.sdf.org/weblog/post/got-root
# /bin/sh
if [[ $EUID -ne 0 ]]; then
echo 'This script must be run as "root".'
echo 'Enter password to elevate privileges:'
SCRIPTPATH=$( cd $(dirname $0) ; pwd -P )
SELF=`basename $0`
sudo $SCRIPTPATH'/'$SELF
exit 1
fi
clear #optional may look weird if chained.
echo 'Script running with root privileges.'
# Your code here
@James-Firth
Copy link
Author

Great script by PerpetualBeta, forked to make a small change from using 'whoami' to $EUID

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment