0
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?
Answer
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.
[ -t 1 ]
seems to work. – Ryan C. Thompson – 2010-08-07T00:54:33.953Just a note that you might be redirecting output, using tee or similar, and
[ -t 1 ]
would return true, which is not what you want.[ -t 0 ]
seems more appropriate, since it checks stdin. – domen – 2013-02-21T19:06:48.973