Skip to content

Instantly share code, notes, and snippets.

@tomdavidson
Created November 30, 2021 16:06
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 tomdavidson/72e539a4fced881a9e6c7b2d9abf5c4a to your computer and use it in GitHub Desktop.
Save tomdavidson/72e539a4fced881a9e6c7b2d9abf5c4a to your computer and use it in GitHub Desktop.
snippit to help with scripts that should be run as root
#!/usr/bin/env bash
if [ $SUDO_USER ]; then
realUser=$SUDO_USER
else
realUser=$(whoami)
fi
realUserHome=$(bash -c "cd ~$(printf %q "$realUser") && pwd")
[ "$(id -u)" == 0 ] || exec sudo -E bash "$0" "$@"
! type sudo > /dev/null 2>&1 && apt install sudo && usermod -aG sudo "$(whoami)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment