Rather than touching the current shell script, you can make a wrapper to call it with more permissions.
Save the script from below, make it executable (e.g. chmod +x mysudowrapper.sh), and use it like this:
./mysudowrapper.sh /path/to/originalscript.sh
Then make the originalscript.sh only runnable by root:
chown root:root /path/to/originalscript.sh
chmod go-x /path/to/originalscript.sh
Test it:
/path/to/originalscript.sh
bash: /path/to/originalscript.sh: Permission denied
Via wrapper script as above should work. From here you should be all set.
#!/bin/bash
# Wrapper to run commands with sudo. Uses gksudo in a GUI environment, falls back on
# plain sudo in non-GUI environment.
# Info: http://askubuntu.com/a/244690/88802
# Author(s): Gert van Dijk
# Disclaimer: No warranties whatsoever. I'm not responsible for any damage here.
# Purpose of this script is to *demonstrate* a wrapper to run other commands.
GKSUDO=/usr/bin/gksudo
SUDO=/usr/bin/sudo
gui_sudo () { # Run command with a GUI-capable sudo-wrapper
$GKSUDO -- $SUDO "$@"
}
plain_sudo () { # Run command with the plain sudo wrapper
$SUDO "$@"
}
has_gui () { # Checks for whether GUI is available via the $DISPLAY environment
if [ "$DISPLAY" != "" ]; then return 0; else return 1; fi
}
has_args () { # Checks for valid amount of arguments
if [ "$1" != "" ]; then return 0; else return 1; fi
}
print_usage () { # Prints usage
echo "Usage: $0 <command> [args]"
}
if has_args $@; then
if has_gui; then gui_sudo $@; else plain_sudo $@; fi
else
print_usage; exit 1
fi
gksudo? – gertvdijk Jan 18 at 0:33