2

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?

1 Answer 1

6

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.

1
  • Very elegant solution, +1. Dec 13, 2011 at 13:13

You must log in to answer this question.

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