Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

How do you make an executable file (a shell script not of my own creation), when be accessed, pop up a little window requesting the password (as of now it is set to all executable, owner read and right, group and others only read.)

share|improve this question
1  
Call gksu or kdesudo, which show the dialog and run the program as root. From a script, call those utilities around the part that requires root permission. –  Mechanical snail Jan 18 '13 at 0:32
3  
What kind of executable file? Are you programming now in C++ for example and trying to find a way to integrate with PolicyKit for example? Or do you mean in scripting like gksudo? –  gertvdijk Jan 18 '13 at 0:33
add comment

1 Answer

up vote 3 down vote accepted

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
share|improve this answer
    
I seem to be getting this error (which I no not how tho fix since it has been a bit since I have played with bash.) chris@Chris-Laptop-Ubuntu:~$ ~/bin/Sudoer /usr/bin/minecraft /home/chris/bin/Sudoer: line 31: syntax error: unexpected end of file (Oh yes, and if you are wondering why I am doing this to minecraft, it is because I like it when people have to ask me to go on minecraft. POWER) –  PyRulez Jan 18 '13 at 19:29
    
@PyRulez Seems like you're right :) I was trying too hard to get it more compact, but made a mistake. Now copy the script again. –  gertvdijk Jan 18 '13 at 19:36
    
It seems (and this worked by the way) that I had to rename the original script to something new, and then make another script with the same name which runs ./mysudowrapper.sh /path/to/originalscript_old.sh –  PyRulez Jan 19 '13 at 23:18
    
Oh yes, and when I do that it works like a charm (an evil charm that allows me to control others, mu ha ha.) Thank you. –  PyRulez Jan 19 '13 at 23:19
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.