[Bug 1479805] Re: /etc/bash_command_not_found is utterly broken
CarstenHey
carsten at debian.org
Thu Jul 30 18:25:38 UTC 2015
I also added a check to both shell snippets (i.e., for zsh and for bash)
that prevents defining the handler function if the shell is run
interactively. Ideally, all users would set up their shell rc files in a
way that does not source the snippets for interactive shells, but users
are not perfect …. I can imagine rare cases where command-not-found is
used for debugging, and where a user might want to enable it even for
non-interactive shells. To ease this, $PS1 is checked, and not $-. The
outer if condition now is for both shells: [[ -n "${PS1-}" && -x
/usr/lib/command-not-found ]]
I also replaced all tabs with four spaces each in the zsh snippet in
order to let the file fit on an 80 characters wide terminal w/o line
wrapping.
There is still one issue that could be fixed in these files: LP:#559060.
Don't expect a patch from me for this one, although I might send one, if
I stumble over a clean solution.
http://stateful.de/~carsten/tmp/150730jFFWrFL4qo4/bash_command_not_found
http://stateful.de/~carsten/tmp/150730jFFWrFL4qo4/zsh_command_not_found
* bash_command_not_found:
- Print an error message if a command is not found, and the package
has been removed but not purged. (LP: #1479805)
- Do not define the handler function if the package has been removed.
- Do not define the handler function if the shell is interactive.
* zsh_command_not_found:
- Do not define the handler function if the shell is not interactive.
- Reindent file in order to fit on an 80 characters wide terminal without
line wrapping.
--
You received this bug notification because you are a member of Ubuntu
Foundations Bugs, which is subscribed to command-not-found in Ubuntu.
https://bugs.launchpad.net/bugs/1479805
Title:
/etc/bash_command_not_found is utterly broken
Status in command-not-found package in Ubuntu:
New
Bug description:
In a nutshell, if the package command-not-found is removed, but not
purged, sourcing this file leads to silent failures if a command is
not found.
I don't use Ubuntu, but I assume that the file bash_command_not_found
in the bzr repository is installed to /etc
./x is your bash_command_not_found with /usr/ replaced by /ur/ to
simulate that the packages is removed but not purged.
I use a self written printexitvalue (see tcsh and zsh), which prints
the line "bash: exit $?" via $PROMPT_COMMAND - this is unrelated to
the bug, but shown in the output below.
$ unset -f command_not_found_handle
$ cat x
command_not_found_handle() {
if [ -x /ur/lib/command-not-found ]; then
/ur/lib/command-not-found -- "$1"
return $?
else
return 127
fi
}
$ foo
bash: foo: command not found
bash: exit 127
$ . ./x
$ foo
bash: exit 127
$
As you can see, trying to run the non-available command foo results in
no output (except of the one I do in $PROMPT_COMMAND) and correctly
fails with exit code 127. It does not print "bash: foo: command not
found", as it should.
command_not_found_handle() is not command_not_found_handler() (note
the trailing 'r' in the word handler, that's not in the word handle).
man bash (for the former function) and man zshall (for the latter
function) contain the glory details how both functions work and how
they differ. If /usr/lib/command-not-found is available, your
command_not_found_handle() implementation behaves as it should, but if
/usr/lib/command-not-found is not available it behaves as zsh's
command_not_found_handler() should, but not as a
command_not_found_handle() should.
An untested, but presumably correct implementation, except of the
unconditional overwriting of the function (I don't know how to prevent
this in bash properly), and returning 127 when it should return 126
(see man p exec), based on /etc/zsh_command_not_found, is shown below.
Please replace two leading spaces with a tab, four leading spaces with
two tabs and so on; and don't replace "|| return $?" with "\nreturn
$?", in case someone uses set -e interactively. I quoted the words
return and builtin to guard against weird alias definitions (I did not
do this in the zsh snipppet because, in general, I expect zsh users to
have at least basic shell knowledge, and sometimes zsh users do ugly
things on purpose.
# (c) Zygmunt Krynicki 2007,
# Licensed under GPL, see COPYING for the whole text
#
# This script will look-up command in the database and suggest
# installation of packages available from the repository
if [[ -x /usr/lib/command-not-found ]] ; then
command_not_found_handle() {
[[ -x /usr/lib/command-not-found ]] || {
\builtin printf >&2 'bash: %scommand not found\n' ${1+"$1: "}
\return 127
}
/usr/lib/command-not-found -- ${1+"$1"} || \return $?
}
fi
To manage notifications about this bug go to:
https://bugs.launchpad.net/ubuntu/+source/command-not-found/+bug/1479805/+subscriptions
More information about the foundations-bugs
mailing list