Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question
add comment

3 Answers

up vote 1 down vote accepted

Examine the output of env when you're running from the terminal and when launched by the gui. My guess is that Gnome (or whatever window manager you're using) is adding an environment variable or two.

Another option is to check to see if there's a tty associated with it using if [ -t 1 ] as shown in this answer (note the caveats in the comments).

share|improve this answer
    
[ -t 1 ] seems to work. –  Ryan Thompson Aug 7 '10 at 0:54
    
Just 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 Feb 21 '13 at 19:06
add comment

I believe the more common method is to simply state that the script requires sudo to execute, as in:

sudo ./script_name.sh

In this way, the entire script is run with administrative privileges. If you think about it from the security model standpoint, there's no reason why a script that needs sudo for at least part of it, shouldn't just run with sudo. Otherwise, it's just a nuisance.

However, if you insist on using sudo for only certain parts, you can place sudo in your actual bash script and it will prompt for the password and wait for entry until it continues executing the script. After getting at least one sudo password entry, all subsequent sudos will retain the elevated privileges and not prompt for the password again. It will also leave you entire terminal session with elevated privileges for future sudo not executed within the script.

As for gksudo, I think an option is to tell the individual to use the alt-f2 run dialog, and instead of typing sudo script_name.sh, type gksudo and then click the "Run with file" button and have them navigate to and select the script file they need to run. I know it's cumbersome. Hopefully someone has a more elegant solution.

share|improve this answer
2  
I disagree with the statement on security. The author might be confident to wrap a simple portion with sudo, but if he runs the whole script with sudo, then the entire script needs to be inspected and proven free of security errors on every edit. It's safer to just have sudo for the minimal subset of commands. –  Rich Homolka Aug 5 '10 at 18:08
add comment
if [ "$DISPLAY" ]; then
    gksudo foo
else
    sudo foo
fi

Close enough.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.