• Non-installed programs and installation hints (from shell?)

    From Janis Papanagnou@janis_papanagnou@hotmail.com to comp.unix.shell,alt.os.linux.ubuntu on Fri May 7 13:14:12 2021
    From Newsgroup: alt.os.linux.ubuntu

    If on Ubuntu I issue a command name of a program that is not installed
    I get an installation hint from a *bash* command shell like

    $ stellarium

    Command 'stellarium' not found, but can be installed with:

    sudo apt install stellarium


    In a *ksh93* context I don't get that hint

    $ stellarium
    ksh93: stellarium: not found [No such file or directory]


    Does anyone know the underlying mechanism (in bash or generally Ubuntu)
    how that is achieved. (In the bash dotfiles I didn't see anything but
    may also just have missed it.)

    Janis
    --- Synchronet 3.18a-Linux NewsLink 1.113
  • From Anssi Saari@as@sci.fi to comp.unix.shell,alt.os.linux.ubuntu on Fri May 7 14:30:03 2021
    From Newsgroup: alt.os.linux.ubuntu

    Janis Papanagnou <janis_papanagnou@hotmail.com> writes:

    Does anyone know the underlying mechanism (in bash or generally Ubuntu)
    how that is achieved. (In the bash dotfiles I didn't see anything but
    may also just have missed it.)

    The command-not-found command from the package with the same name
    provides the information and the default /etc/bash.bashrc uses that
    package if it's installed. The way it works in bash and zsh is that the
    shell executes a specific function if a command is not found, if that
    functions is defined. Presumably ksh93 doesn't have that feature.
    --- Synchronet 3.18a-Linux NewsLink 1.113
  • From Janis Papanagnou@janis_papanagnou@hotmail.com to comp.unix.shell,alt.os.linux.ubuntu on Fri May 7 14:44:18 2021
    From Newsgroup: alt.os.linux.ubuntu

    On 07.05.21 13:30, Anssi Saari wrote:
    Janis Papanagnou <janis_papanagnou@hotmail.com> writes:

    Does anyone know the underlying mechanism (in bash or generally Ubuntu)
    how that is achieved. (In the bash dotfiles I didn't see anything but
    may also just have missed it.)

    The command-not-found command from the package with the same name
    provides the information and the default /etc/bash.bashrc uses that
    package if it's installed. The way it works in bash and zsh is that the
    shell executes a specific function if a command is not found, if that functions is defined. Presumably ksh93 doesn't have that feature.

    Thanks. So file '/etc/bash.bashrc' is the key to find the code.

    And in ksh it would in principle be possible to also capture the
    error, e.g. by

    trap '[ $? -eq 127 ] && command_not_found_handle "${...}"' ERR

    if only we had (where I wrote "${...}") the name of the command
    available that produced the error.

    Janis
    --- Synchronet 3.18a-Linux NewsLink 1.113
  • From David W. Hodgins@dwhodgins@nomail.afraid.org to comp.unix.shell,alt.os.linux.ubuntu on Fri May 7 09:51:00 2021
    From Newsgroup: alt.os.linux.ubuntu

    On Fri, 07 May 2021 08:44:18 -0400, Janis Papanagnou <janis_papanagnou@hotmail.com> wrote:
    if only we had (where I wrote "${...}") the name of the command
    available that produced the error.

    In Mageia it's the package packagekit-command-not-found
    which contains /etc/profile.d/PackageKit.sh and /usr/libexec/pk-command-not-found

    Regards, Dave Hodgins

    --
    Change dwhodgins@nomail.afraid.org to davidwhodgins@teksavvy.com for
    email replies.
    --- Synchronet 3.18a-Linux NewsLink 1.113
  • From Christian Weisgerber@naddy@mips.inka.de to comp.unix.shell,alt.os.linux.ubuntu on Fri May 7 14:02:30 2021
    From Newsgroup: alt.os.linux.ubuntu

    On 2021-05-07, Janis Papanagnou <janis_papanagnou@hotmail.com> wrote:

    $ stellarium

    Command 'stellarium' not found, but can be installed with:

    sudo apt install stellarium

    Does anyone know the underlying mechanism (in bash or generally Ubuntu)
    how that is achieved.

    Bash has a hook that allows you to intercept "command not found"
    errors. I once played around with this to catch all commands of
    the form <number>x<number>, e.g. 100x40, and resize xterm accordingly.

    From the man page, section "Command Execution":
    If the search is unsuccessful, the shell searches for a defined
    shell function named command_not_found_handle. If that function
    exists, it is invoked in a separate execution environment with the
    original command and the original command's arguments as its
    arguments, and the function's exit status becomes the exit status
    of that subshell.

    --
    Christian "naddy" Weisgerber naddy@mips.inka.de
    --- Synchronet 3.18a-Linux NewsLink 1.113
  • From Christian Weisgerber@naddy@mips.inka.de to comp.unix.shell,alt.os.linux.ubuntu on Fri May 7 14:51:29 2021
    From Newsgroup: alt.os.linux.ubuntu

    On 2021-05-07, Janis Papanagnou <janis_papanagnou@hotmail.com> wrote:

    And in ksh it would in principle be possible to also capture the
    error, e.g. by

    trap '[ $? -eq 127 ] && command_not_found_handle "${...}"' ERR

    if only we had (where I wrote "${...}") the name of the command
    available that produced the error.

    I guess you could extract it by way of hist -l $((HISTCMD - 1)).

    --
    Christian "naddy" Weisgerber naddy@mips.inka.de
    --- Synchronet 3.18a-Linux NewsLink 1.113
  • From Janis Papanagnou@janis_papanagnou@hotmail.com to comp.unix.shell,alt.os.linux.ubuntu on Fri May 7 18:06:59 2021
    From Newsgroup: alt.os.linux.ubuntu

    On 07.05.21 16:51, Christian Weisgerber wrote:
    On 2021-05-07, Janis Papanagnou <janis_papanagnou@hotmail.com> wrote:

    And in ksh it would in principle be possible to also capture the
    error, e.g. by

    trap '[ $? -eq 127 ] && command_not_found_handle "${...}"' ERR

    if only we had (where I wrote "${...}") the name of the command
    available that produced the error.

    I guess you could extract it by way of hist -l $((HISTCMD - 1)).

    Ah, yes, of course. (Need some more coffee, I guess.) - Thanks!

    With your suggestion I tested this variant in my ksh environment

    trap '[ $? -eq 127 ] && command_not_found_handle $(fc -ln -0)' ERR

    and (with the respective function definition) it basically works.
    (Need only to get rid of the duplicate "not found" information.)

    Janis
    --- Synchronet 3.18a-Linux NewsLink 1.113