Skip to content

Instantly share code, notes, and snippets.

@vilhalmer
Created February 4, 2017 23:29
Show Gist options
  • Save vilhalmer/5b58d123fccc3baf98bf68543738a873 to your computer and use it in GitHub Desktop.
Save vilhalmer/5b58d123fccc3baf98bf68543738a873 to your computer and use it in GitHub Desktop.
Boilerplate for escalating shell script privileges using the standard macOS password dialog
#!/usr/bin/env bash
if [[ $EUID -ne 0 ]]; then
# Find this script in the filesystem so we can re-run it. (There are probably better ways of doing this.)
self="$(cd $(dirname $(pwd -P)/${BASH_SOURCE[0]}) && pwd -P)/$(basename ${BASH_SOURCE[0]})"
admin_prompt="Hax requires root plz"
exec 10>&1 11>&2 # Copy stdout and stderr so we can bypass osascript's control of the terminal.
osascript -e "do shell script \"$self >&10 2>&11\" \
with administrator privileges \
with prompt \"$admin_prompt\"" &> /dev/null # We don't care about osascript's error output.
exit # Don't continue with the rest of the script in this outer instance.
fi
# The rest of the script as usual
echo "Executing hax" || ( echo "Hax failed!" && exit 1 )
@ChristopherA
Copy link

This code used to work in older macOS, but is failing on Big Sur. Any ideas?

@vilhalmer
Copy link
Author

Sorry, no idea. I haven't used macOS in many years now.

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