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() ?
Loading…
Ingo Wichmann
save the last command in a variable:
COMMAND=”!!”
13 Feb 2008, 16:42
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.