Title: Implement a «Command not found» handler in OpenBSD Author: Solène Date: 09 March 2021 Tags: openbsd Description: # Introduction On many Linux systems, there is a special program run by the shell (configured by default) that will tell you which package provide a command you tried to run but is not available in $PATH. Let's do the same for OpenBSD! # Prerequisites We will need to install the package pkglocate to find binaries. ```shell command as root # pkg_add pkglocate ``` We will also need a file /usr/local/bin/command-not-found executable with this content: ```shell script #!/bin/sh CMD="$1" RESULT=$(pkglocate */bin/${CMD} */sbin/${CMD} | cut -d ':' -f 1) if [ -n "$RESULT" ] then echo "The following package(s) contain program ${CMD}" for result in $RESULT do echo " - $result" done else echo "pkglocate didn't find a package providing program ${CMD}" fi ``` # Configuration Now, we need to configure the shell to run this command when it detects an error corresponding to an unknown command. This is possible with bash, zsh or fish at least. ## Bash configuration Let's go with bash, add this to your bash configuration file ```bash script command_not_found_handle() { /usr/local/bin/command-not-found "$1" } ``` ## Fish configuration ```fish shell script function fish_command_not_found /usr/local/bin/command-not-found $argv[1] end ``` ## ZSH configuration ```zsh shell script function command_not_found_handler() { /usr/local/bin/command-not-found "$1" } ``` # Trying it Now that you configured your shell correctly, if you run a command in your shell that isn't available in your PATH, you may have either a success with a list of packages giving the command or that the command can't be found in any package (unlucky). This is a successful output that found the program we were trying to run. ```Success output $ pup The following package(s) contain program pup - pup-0.4.0p0 ``` This is a result showing that no package found a program named "steam". ```Unsuccessful output $ steam pkglocate didn't find a package providing program steam ```