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:

export PROMPT_COMMAND='
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); 
    PACKAGE=${PACKAGE:="-"};
    if [ $PACKAGE != "-" ]; then 
    echo $COMMAND is not installed\; Try \"sudo zypper install $PACKAGE\" to install it.; 
    fi; 
fi'

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

benji@lcars:~>

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() ?


- 2 comments by 1 or more people Not publicly viewable

  1. Ingo Wichmann

    save the last command in a variable:

    COMMAND=”!!”

    13 Feb 2008, 16:42

  2. That was like the first thing I tried, doesn’t seem to work in scripts as far as I can see, unless I’m missing the magic escape sequence. !! is very useful for repeating last command in the shell itself.

    13 Feb 2008, 16:50


Add a comment

You are not allowed to comment on this entry as it has restricted commenting permissions.

February 2008

Mo Tu We Th Fr Sa Su
Jan |  Today  | Mar
            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      

Search this blog

Tags

Galleries

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

Loading…
Not signed in
Sign in

Powered by BlogBuilder
© MMXVII