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.

exactly i want to make a script and add this to shortcut launcher...
when i double click the icon, it will popup a terminal window (done)

in terminal, it will ask root password at first..
and then do some stuff with root privilege (i make it as a function) ..
after that, in the end of script it will close the window
like "press anykey to close window" (i can do it)

#!/bin/sh
touchpad(){
    whoami
}
sudo -S touchpad # but it return error "command not found"

how to run a function as root user?
or there is another way to do this?

EDIT :
i'v try this #!/bin/bash

sudo ./main2.sh #this not work when i make it as a launcher
exit 

this is my current code (after googling):

#!/bin/bash

gksudo -m "Input Password" clear
sudo whoami
#sudo bla bla bla
read -n 1 -p "Press any key to close window"

this is what i want, but got error if i type wrong password for 3 times..
how to force quit main window if gksudo fail??

share|improve this question
add comment

3 Answers 3

up vote 2 down vote accepted

Test the return value of gksudo:

if ! gksudo -m "Input Password" clear ; then
    echo Bad password
    exit 1
fi
share|improve this answer
add comment

You can't. To do it, you need to put the function in another script, and run that script with sudo.

Sudo only runs commands, it cannot access functions inside your script, as it runs in a separate process. If you insist on running a function in your script, you could run the script again with sudo, but pass it an argument with $1 that runs the function directly.

share|improve this answer
    
i have try to put my function in another script, it works when i acces it via terminal. but when make it as a launcher(application interminal) in panel it didn't works... see my edit.. –  Warung Nasi 49 May 11 '11 at 7:16
2  
Please try it using the full pathname: sudo FULL_PATH_TO/main2.sh . When called from the launcher the initial working directory is not the script directory, using ./main2.sh is not expected to work. –  João Pinto May 11 '11 at 11:42
add comment

what about using gksudo instead? It will prompt user for entering password. Then it should work with just sudo.

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.