Tell me more ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

How do I create a keyboard shortcut to a script that needs sudo privileges? I tried to create shortcuts via Ubuntu Keyboard Settings GUI to scripts

"sh script.sh", "sudo sh script.sh"

, but they don't work.

share|improve this question
Possible duplicate: askubuntu.com/questions/116052/… – pl1nk Jun 2 '12 at 19:08
also when calling scripts you need to use bash scriptname, than sh scriptname – pt123 Jun 2 '12 at 22:00
This is incorrect. You only need to run a script with bash if it uses the extended functionality bash provides. When a script has a .sh extension, that indicates (by convention) that it can be run by the system's default sh, even if that is not bash. (Most scripts with no extension can be run by sh too--if a script needs bash, then its documentation should indicate this, or it should have a hashbang line at the top for it, which if you're manually invoking sh or bash you can check first.) – Eliah Kagan Jun 3 '12 at 17:02

2 Answers

sudo requires a terminal to get the password from. Use gksudo instead; it asks for the password via a gui dialog box.

gksudo -- script
share|improve this answer

Put the sudo commands in the script on the lines you need privileges to run. Then run normally.

share|improve this answer
2  
Have you ever tried this? It will work when running from a terminal, but what about running from a shortcut, or from Alt-F2? I don't think it will work. – Marty Fried Jun 2 '12 at 19:29
1  
@MartyFried Indeed, this will not work, since sudo needs to read password input from the command-line. This would only work in the specific situation where the commands being run with sudo are permitted (in the sudoers file) to be run without requiring password authentication. – Eliah Kagan Sep 13 '12 at 19:39

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.