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.

I want have a script that I can give to friends which will add repositories and download files, install them without having to use command as they are not command line savey and have no desire to learn. I need to know how to get the script to prompt them for the root password to allow them to do some of these functions. How would I go about this? Please don't hate on them.. Getting them to start using Linux was a big step and I am just happy that they are attempting something new... and it was in the open source world..

share|improve this question
    
Thank you both. It's funny at how obvious that was and I overlooked a simple command; gksu. I think that may work for what I am attempting. Thanks to both of you. –  VinceBrowning Oct 12 '12 at 10:02
    
Just to have it mentioned: its exactly the purpose of Ubuntu Software Center to let people who are not comfortable with the command line install applications or add repositories. –  Takkat Oct 12 '12 at 10:35

2 Answers 2

Just use the sudo word in front of the command in the bash script. And when you execute the file, select "Open in terminal", you will be asked for password.

For example, if you are using apt-get commands , you can use like this

#!/bin/sh
sudo apt-get update
sudo apt-get upgrade
.....
.....

If need to make this executable and when you double click there will be a prompt like this

enter image description here

You need to click on Open in terminal button and provide password in the terminal

Update

As Karthick T commented, you can also use the gksu or gksudo word before the command. So, the file could be

gksu apt-get update
gksudo apt-get install <some-package>

Double clicking the script will ask the password and run the command after you provided it.

share|improve this answer
1  
or gksu if they want to run it without terminal ( Run option) –  Karthik T Oct 12 '12 at 9:52
    
@OliveTwist Just for future reference, can you give an example where gksu doesn't work with a bash script? –  hexafraction Oct 12 '12 at 10:26
    
It's working. I was confused about running it in terminal. It won't work there, but in script it is good. Thanks @ObsessiveSSOℲ –  Anwar Shah Oct 12 '12 at 10:42

I've had to do this several times, this is a template of my scripts that need to run as root and prompt the user for the root password.

#!/usr/bin/env bash

cmdname=$(basename $)

function usage() {
    echo
    echo "$cmdname [-l|--location] /location/to/package [-p|--no-provisioning]"
    echo "  -l | --location     Set the location to be used to find the package install"
    echo "  -p | --no-provision If set, will not load the example provisioning"
    exit 1
}

if [ "$(whoami)" != "root" ]; then
    echo "You must be root to do this"
    su -c "$0 $*"
    exit
fi

LOCATION="/tmp/TESTPKG$USER/"
PROVISION=1

OPTIONS=$(getopt -o l:p --long location:,no-provisioning -n "$cmdname" -- $*) || usage
eval set -- $OPTIONS

while true ; do
    case $1 in
        -l|--location)
            LOCATION=$2
            shift 2;;
        -p|--no-provisioning)
            PROVISION=0
            shift 1;;
        --) shift ; break ;;
        *)
            echo "Invalid argument $1"
            exit 1;;
    esac
done

The above is for a specific use case, but I think it highlights two nice features, using getopt and asking for root permission and running command, while passing in the arguments.

So the if block check if the user is currently root, if not, runs the called script ($0) with any arguments passed to the script ($*). Then it exits, as to not attempt to run the script as the non-root user.

The getopt line attempts to then parse the options and if there is an invalid argument it prints the usage and exits.

Best of luck!

share|improve this answer

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.