一直好奇Ubuntu20 原生的bash 每次输入命令行,没有该命令后会提示你如何安装这个软件包,前一段时间一直在找,直到我使用oh-my-zsh添加command-not-found插件后,找到了根源。

cat command-not-found.plugin.zsh
## Platforms with a built-in command-not-found handler init filefor file (# Arch Linux. Must have pkgfile installed: https://wiki.archlinux.org/index.php/Pkgfile#Command_not_found/usr/share/doc/pkgfile/command-not-found.zsh# macOS (M1 and classic Homebrew): https://github.com/Homebrew/homebrew-command-not-found/opt/homebrew/Library/Taps/homebrew/homebrew-command-not-found/handler.sh/usr/local/Homebrew/Library/Taps/homebrew/homebrew-command-not-found/handler.sh
); doif [[ -r "$file" ]]; thensource "$file"unset filereturn 0fi
done
unset file## Platforms with manual command_not_found_handler() setup# Debian and derivatives: https://launchpad.net/ubuntu/+source/command-not-found
if [[ -x /usr/lib/command-not-found || -x /usr/share/command-not-found/command-not-found ]]; thencommand_not_found_handler() {if [[ -x /usr/lib/command-not-found ]]; then/usr/lib/command-not-found -- "$1"return $?elif [[ -x /usr/share/command-not-found/command-not-found ]]; then/usr/share/command-not-found/command-not-found -- "$1"return $?elseprintf "zsh: command not found: %s\n" "$1" >&2return 127fi}
fi# Fedora: https://fedoraproject.org/wiki/Features/PackageKitCommandNotFound
if [[ -x /usr/libexec/pk-command-not-found ]]; thencommand_not_found_handler() {if [[ -S /var/run/dbus/system_bus_socket && -x /usr/libexec/packagekitd ]]; then/usr/libexec/pk-command-not-found "$@"return $?fiprintf "zsh: command not found: %s\n" "$1" >&2return 127}
fi# NixOS: https://github.com/NixOS/nixpkgs/tree/master/nixos/modules/programs/command-not-found
if [[ -x /run/current-system/sw/bin/command-not-found ]]; thencommand_not_found_handler() {/run/current-system/sw/bin/command-not-found "$@"}
fi# Termux: https://github.com/termux/command-not-found
if [[ -x /data/data/com.termux/files/usr/libexec/termux/command-not-found ]]; thencommand_not_found_handler() {/data/data/com.termux/files/usr/libexec/termux/command-not-found "$1"}
fi# SUSE and derivates: https://www.unix.com/man-page/suse/1/command-not-found/
if [[ -x /usr/bin/command-not-found ]]; thencommand_not_found_handler() {/usr/bin/command-not-found "$1"}
fi

发现debian衍生系列的发现版会用/usr/lib/command-not-found脚本,如果懂python,又要兴趣的朋友可以深入研究一下。提升python能力。

cat /usr/lib/command-not-found
#!/usr/bin/python3
# (c) Zygmunt Krynicki 2005, 2006, 2007, 2008
# Licensed under GPL, see COPYING for the whole textfrom __future__ import absolute_import, print_function__version__ = "0.3"
BUG_REPORT_URL = "https://bugs.launchpad.net/command-not-found/+filebug"try:import sysif sys.path and sys.path[0] == '/usr/lib':# Avoid ImportError noise due to odd installation location.sys.path.pop(0)if sys.version < '3':# We might end up being executed with Python 2 due to an old# /etc/bash.bashrc.import osif "COMMAND_NOT_FOUND_FORCE_PYTHON2" not in os.environ:os.execvp("/usr/bin/python3", [sys.argv[0]] + sys.argv)import gettextimport localefrom optparse import OptionParserfrom CommandNotFound.util import crash_guardfrom CommandNotFound import CommandNotFound
except KeyboardInterrupt:import syssys.exit(127)def enable_i18n():cnf = gettext.translation("command-not-found", fallback=True)kwargs = {}if sys.version < '3':kwargs["unicode"] = Truecnf.install(**kwargs)try:locale.setlocale(locale.LC_ALL, '')except locale.Error:locale.setlocale(locale.LC_ALL, 'C')def fix_sys_argv(encoding=None):"""Fix sys.argv to have only unicode strings, not binary strings.This is required by various places where such argument might beautomatically coerced to unicode string for formatting"""if encoding is None:encoding = locale.getpreferredencoding()sys.argv = [arg.decode(encoding) for arg in sys.argv]class LocaleOptionParser(OptionParser):"""OptionParser is broken as its implementation of _get_encoding() usessys.getdefaultencoding() which is ascii, what it should be using islocale.getpreferredencoding() which returns value based on LC_CTYPE (mostlikely) and allows for UTF-8 encoding to be used."""def _get_encoding(self, file):encoding = getattr(file, "encoding", None)if not encoding:encoding = locale.getpreferredencoding()return encodingdef main():enable_i18n()if sys.version < '3':fix_sys_argv()parser = LocaleOptionParser(version=__version__,usage=_("%prog [options] <command-name>"))parser.add_option('-d', '--data-dir', action='store',default="/usr/share/command-not-found",help=_("use this path to locate data fields"))parser.add_option('--ignore-installed', '--ignore-installed',action='store_true',  default=False,help=_("ignore local binaries and display the available packages"))parser.add_option('--no-failure-msg',action='store_true', default=False,help=_("don't print '<command-name>: command not found'"))(options, args) = parser.parse_args()if len(args) == 1:try:cnf = CommandNotFound.CommandNotFound(options.data_dir)except FileNotFoundError:print(_("Could not find command-not-found database. Run 'sudo apt update' to populate it."), file=sys.stderr)print(_("%s: command not found") % args[0], file=sys.stderr)returnif not cnf.advise(args[0], options.ignore_installed) and not options.no_failure_msg:print(_("%s: command not found") % args[0], file=sys.stderr)if __name__ == "__main__":crash_guard(main, BUG_REPORT_URL, __version__)

关于Ubuntu command-not-found是如何实现的研究相关推荐

  1. Ubuntu - command checklist

    只是简单记录我使用的命令,没有什么参考价值,我只是用于整理 chown chmod permissions filename Where, permissions can be read, write ...

  2. Ubuntu command 记录

    1.修改主机名 查看: root# : hostname guest#  : sudo /etc/hostname vi /etc/hostname     #只有一行,直接改就成 vi /etc/h ...

  3. ubuntu command

    uninstall software: sudo apt-get purge openjdk* 转载于:https://www.cnblogs.com/bioinfo/p/3659683.html

  4. ubuntu 14.04安装postgresql最新版本

    官网: https://www.postgresql.org/download/linux/ubuntu/ ---------------------------------------------- ...

  5. Ubuntu 14.04 LTS 的安装和配置以及各种问题的解决

    折腾了几天的时间终于把Ubuntu 14.04 LTS 环境配置好了,为防止遗忘,特作此记录,也方便遇到同样问题的童鞋们查看 步骤一,系统安装 可以拷贝一个系统盘,我用的是 ubuntu-14.04- ...

  6. Ubuntu 8.10 安装后的详细设置

    xmarklet网络书签,收集自己的网络书签,创建自己的网站导航. [url]http://www.kmimagine.com/xmarklet/index.aspx[/url] [b]Ubuntu ...

  7. 如何在ubuntu 14.04系统下开启nfs网络文件系统

    之前安装过ubuntu 11.04和ubuntu 12.04的系统时,分别研究过如何开启NFS网络文件系统.今天将系统更新到了ubuntu 14.04,发现之前的办法都不行了,有些步骤得更新一下,于是 ...

  8. linux 编译 suitesparse,Ubuntu下编译SuiteSparse-4.4.1和METIS-4.0.3

    ubuntu下编译caffe Ubuntu下编译caffe 纯粹是个人编译的记录.不用CUDA(笔记本是amd卡,万恶的nvidia):不手动编译依赖包(apt-get是用来干啥的?用来直接装二进制包 ...

  9. IDEA启动main函数时,Command line is too long Shorten command line for XXX or also问题解决

    巧了,最近起了一个挺长的类名,启动main函数的时候,idea返回了一个Command line is too long Shorten command line的报错,请教研究之后,解决方法如下 方 ...

  10. Ubuntu系统如何连ZJU内网

    2018.11.6 亲测有效 原来都是用Windows上的客户端输入学号加密码的VPN账号来无线上网,可是Ubuntu系统似乎没有对应的客户端,而工科生又很需要装Windows和Ubuntu双系统,因 ...

最新文章

  1. RESTful之限流Throttling
  2. PHP项目学习——控件
  3. SwiftUI区分浅色和深色
  4. 02 面向对象之:类空间问题以及类之间的关系
  5. 判断两条线段是否相交
  6. Actuator对于JMX支持
  7. json_decode
  8. qdialog 返回值_c – QDialog exec()并获取结果值
  9. 7-290 鸡兔同笼 (10 分)
  10. 深入浅出mysql gtid_Mysql 5.7 Gtid内部学习(九) 实际案例(一)
  11. SpringBoot POM 继承(spring-boot-starter-parent)
  12. 吴恩达机器学习之单变量线性回归实现部分
  13. Qt加载osgearth时报错:0x0000xxx处未经处理的异常:0xC00005:写入位置***发生冲突
  14. nginx php重定向,nginx如何重定向信息
  15. 聊聊DevOps制品管理-不止是存储制品这么简单
  16. android销毁指定activity,Android - 销毁指定Activity
  17. stm32语音识别文字显示_『开源项目』基于STM32的智能垃圾桶之语音识别
  18. ICCV 2021 |首届 SoMoF 人体序列预测比赛冠军方案分享
  19. 大数据学习之Flink环境搭建+提交任务方式
  20. “内卷”和“躺平” 英语怎么说?

热门文章

  1. 一文秒懂串口、COM口、TTL、RS-232、RS-485区别
  2. mpvue入坑之数据修改不变化
  3. 实现电话簿管理程序, Mytel类描述单个电话号码,TelManager类负责管理电话号码。把增、删、改、查功能封装到该类中。要求电话号码能从磁盘读写。
  4. JQuery.Ajax()的data参数类型
  5. java文件读取德语乱码
  6. HTML5 canvas 平铺的几种方法
  7. java调整字体_用java修改字体样式
  8. 解决 The run destination 设备 is not valid for Testing the Xcode doesn’t support iPhone4s’s iOS 15.1
  9. 告别弱网,一直追剧一直爽
  10. 什么是ZigBee,他的特点是哪些?