[Bug 1479805] Re: /etc/bash_command_not_found is utterly broken

Launchpad Bug Tracker 1479805 at bugs.launchpad.net
Mon Oct 26 10:16:47 UTC 2015


** Branch linked: lp:~dholbach/command-not-found/1479805

-- 
You received this bug notification because you are a member of Ubuntu
Sponsors Team, which is subscribed to the bug report.
https://bugs.launchpad.net/bugs/1479805

Title:
  /etc/bash_command_not_found is utterly broken

Status in command-not-found package in Ubuntu:
  In Progress

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 Ubuntu-sponsors mailing list