Skip to content

Instantly share code, notes, and snippets.

@subfuzion
Created May 24, 2017 05:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save subfuzion/a5ace9ece752af98f1fda9edc09dc062 to your computer and use it in GitHub Desktop.
Save subfuzion/a5ace9ece752af98f1fda9edc09dc062 to your computer and use it in GitHub Desktop.
Ensure script can run a command as root
#!/bin/sh
# Source: https://get.docker.com/
command_exists() {
command -v "$@" > /dev/null 2>&1
}
sh_c='sh -c'
if [ "$user" != 'root' ]; then
if command_exists sudo; then
sh_c='sudo -E sh -c'
elif command_exists su; then
sh_c='su -c'
else
cat >&2 <<-'EOF'
Error: this installer needs the ability to run commands as root.
We are unable to find either "sudo" or "su" available to make this happen.
EOF
exit 1
fi
fi
# now run some command as root
$sh_c 'some command...'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment