Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

When I type a command not found in a PATH in ubuntu, I get something like this:

$ rdesktop
The program 'rdesktop' is currently not installed.  You can install it by typing:
sudo apt-get install rdesktop

I believe it's a script that looks up the APT archives and suggest an installation package. I'd like to change it so that it offers me to download it at the press of a 'y'. Two questions:

1) Where is the file located?

2) If I just add the install line in the trivial way, it's going to complain that I'm not a root and will fail (because the rdesktop was ran as a mundane user). How do I make it ask for a password and use it to get root access?

share|improve this question
up vote 5 down vote accepted

When bash encounters a command it cannot find, it looks for a function called command_not_found_handle() and executes it.

Under ubuntu, this is defined in /etc/bash.bashrc

By default it runs a python script in /usr/lib/command-not-found

You could make it do whatever you liked, and this is best done in your own ~/.bashrc file:

    command_not_found_handle() {
        echo -n "Do you want to install $1? [N/y] "
        read -N 1 REPLY
        echo
        if [[ $REPLY == [Yy] ]]; then
            sudo apt-get install -- "$1"
        fi
    }

The sudo part answers the section part of your question, but will obviously need to prompt for a password to escalate to root to do the install.

share|improve this answer
    
Very elegant solution, +1. – Breakthrough Dec 13 '11 at 13:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .