All 1 entries tagged Command-Not-Found

No other Warwick Blogs use the tag Command-Not-Found on entries | View entries tagged Command-Not-Found at Technorati | There are no images tagged Command-Not-Found on this blog

February 11, 2008

Command–Not–Found on openSUSE

Ubuntu ships with a programme called command-not-found which will suggest a package to install when the user types a command at the terminal which can’t be found. It seems this makes use of a hook called command_not_found_handler() which is not available in SUSE.

Pavol Rusnak is working on porting this programme to openSUSE, so it might be available for 11.0. But is it possible to get this behaviour on a vanilla openSUSE 10.3 without installing any other packages?

It does seem possible to do something hacky to get such behaviour:

if [ $? -eq 127 ]; then 
    history -a; 
    COMMAND=$(tail -n 1 $HISTFILE); 
    SQL="SELECT name from resolvables WHERE id IN (SELECT resolvable_id FROM named_capabilities WHERE name_id IN (SELECT id FROM names WHERE name LIKE \"$COMMAND\")) LIMIT 1;" 
    PACKAGE=$(echo $SQL | sqlite3 /var/cache/zypp/zypp.db); 
    if [ $PACKAGE != "-" ]; then 
    echo $COMMAND is not installed\; Try \"sudo zypper install $PACKAGE\" to install it.; 

Then you get similar behaviour:

benji@lcars:~> kopete
bash: kopete: command not found
kopete is not installed; Try "sudo zypper install kdenetwork3-InstantMessenger" to install it.

benji@lcars:~> amarok
bash: /usr/bin/amarok: No such file or directory
amarok is not installed; Try "sudo zypper install amarok" to install it.

benji@lcars:~> nothingprovidesthis
bash: nothingprovidesthis: command not found


However, the whole adding command to history and then re-reading seems like a really bad idea. Possibly a security hole if someone adds something malicious to .bash_history so it’s probably not a good idea to use this _. If anyone knows how to get the previous command in a better way please let me know. “fc” will return it, but not until after PROMPT_COMMAND has been executed it seems.

It also assumes that anything with exit status 127 is a command not found, which is probably not always true.

Also it doesn’t seem to work in other shells that are not bash.

Any better way to do this without command_not_found_handler() ?

September 2022

Mo Tu We Th Fr Sa Su
Aug |  Today  |
         1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30      

Search this blog



Most recent comments

  • Hey skx, how do you do that ? I run into the fu….. problem, that curl terminate with unspec. error… by David on this entry
  • With some minor changes this will also work for updating from 11.0 to 11.1. by skx on this entry
  • You are the man!!! Great work and good documentation! It worked without any problem for me. Thanks a… by Vany on this entry
  • didn't work for me. In fact killed the system. Have to download 11.0 and burn to dvd to fix it. by maybe windows on this entry
  • Will this method work for 11 => 11.1 ? by Erik Jakobsen on this entry

Blog archive

RSS2.0 Atom
Not signed in
Sign in

Powered by BlogBuilder