Quantcast
Channel: Linux by Examples » which
Viewing all articles
Browse latest Browse all 7

CLI Magic: command not found, suggest what to apt-get

0
0

I have recently upgrade my Ubuntu Edgy to Ubuntu Feisty and I discover a fantastic CLI magic packaged with Feisty, its call command-not-found. With command-not-found package, when you have type some command that doesn’t exist in your terminal, it will suggest you what to install.

Let say I type trafshow, which It is not installed, the result will be this


The program 'trafshow' is currently not installed.  You can install it by typing:
sudo apt-get install netdiag
Make sure you have the 'universe' component enabled
bash: trafshow: command not found

If somehow it doesn’t work at your end, try to look at /etc/bash.bashrc, make the lines at below is uncomment.


# if the command-not-found package is installed, use it
if [ -x /usr/bin/command-not-found ]; then
        function command_not_found_handle {
                /usr/bin/command-not-found $1
                return $?
        }
fi

command-not-found itself is a python script that will return the suggestion line. Therefore if you type command-not-found <command name> , it will returns the suggestion line even the installed command.

command-not-found gedit
The program 'gedit' is currently not installed.  You can install it by typing:
sudo apt-get install gedit

With that, we can actually check on certain command is from which package, where conventionally I usually do with dpkg -S. Compare to dpkg -S, command-not-found is much much more faster.

Another thing I can make use of command-not-found in my bash script. Look the the simple script here

#!/bin/bash
Cmd=`which trafshow`;
if [ ! -n $Cmd ]
then
    command-not-found trafshow
fi

I try to check for absolute path of trafshow, if trafshow is not installed, the variable Cmd will be null, and therefore I return the suggestion line. The reason I do this is because the command not found features is not functional automatically in bash script. I mean, if you run trafshow straight away in your script and trafshow is not installed, the suggestion line will not appear.

I have no idea whether command-not-found is available on other Linux distribution besides Ubuntu Feisty, but refers to Agile Testing, you can apt-get command-not-found package in Ubuntu Edgy too.

With command-not-found feature enabled, it somehow slow down the command line process because it performs some checking every time you hit the command name that doesn’t exist. Niath is actually time the process of command-not-found, and it is not so slow that we can still tolerate the delay.

[中文翻译]

[tags]ubuntu, cli magic, command not found, suggestion apt-get, feisty, edgy[/tags]


Viewing all articles
Browse latest Browse all 7

Latest Images

Trending Articles





Latest Images