I'd like to write a script that does an administration task, and I would like to be able to run it either from a terminal or not. If it runs in a terminal, even a graphical one, I'd like it to ask for the password using sudo. If it is run in some other way (for example Alt+F2 dialog), it should use gksudo. Any solutions?
Based on the below, this is what I came up with:
#!/bin/sh SUDO="/usr/bin/sudo" if [ -t 1 ]; then "$SUDO" "$@"; else gksudo -- "$SUDO" "$@"; fi
The slightly awkward case for gksudo is to ensure that the options are parsed by sudo and not gksudo, because they have different command-line options.