关于Ubuntu command-not-found是如何实现的研究
linux-dash
A beautiful web dashboard for Linux
项目地址:https://gitcode.com/gh_mirrors/li/linux-dash
![](https://devpress.csdnimg.cn/6deffb34f7114cc1a2e1e686a67e0027.png)
·
一直好奇Ubuntu20 原生的bash 每次输入命令行,没有该命令后会提示你如何安装这个软件包,前一段时间一直在找,直到我使用oh-my-zsh添加command-not-found插件后,找到了根源。
cat command-not-found.plugin.zsh
## Platforms with a built-in command-not-found handler init file
for 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
); do
if [[ -r "$file" ]]; then
source "$file"
unset file
return 0
fi
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 ]]; then
command_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 $?
else
printf "zsh: command not found: %s\n" "$1" >&2
return 127
fi
}
fi
# Fedora: https://fedoraproject.org/wiki/Features/PackageKitCommandNotFound
if [[ -x /usr/libexec/pk-command-not-found ]]; then
command_not_found_handler() {
if [[ -S /var/run/dbus/system_bus_socket && -x /usr/libexec/packagekitd ]]; then
/usr/libexec/pk-command-not-found "$@"
return $?
fi
printf "zsh: command not found: %s\n" "$1" >&2
return 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 ]]; then
command_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 ]]; then
command_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 ]]; then
command_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 text
from __future__ import absolute_import, print_function
__version__ = "0.3"
BUG_REPORT_URL = "https://bugs.launchpad.net/command-not-found/+filebug"
try:
import sys
if 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 os
if "COMMAND_NOT_FOUND_FORCE_PYTHON2" not in os.environ:
os.execvp("/usr/bin/python3", [sys.argv[0]] + sys.argv)
import gettext
import locale
from optparse import OptionParser
from CommandNotFound.util import crash_guard
from CommandNotFound import CommandNotFound
except KeyboardInterrupt:
import sys
sys.exit(127)
def enable_i18n():
cnf = gettext.translation("command-not-found", fallback=True)
kwargs = {}
if sys.version < '3':
kwargs["unicode"] = True
cnf.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 be
automatically 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() uses
sys.getdefaultencoding() which is ascii, what it should be using is
locale.getpreferredencoding() which returns value based on LC_CTYPE (most
likely) 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 encoding
def 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)
return
if 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__)
![]( https://profile-avatar.csdnimg.cn/default.jpg)
![](https://devpress.csdnimg.cn/7174e1ca86c447029bb12f9ec0bd281c.png)
![](https://devpress.csdnimg.cn/096f7827187446559bd7b6030eb5db38.png)
![](https://devpress.csdnimg.cn/6deffb34f7114cc1a2e1e686a67e0027.png)
A beautiful web dashboard for Linux
最近提交(Master分支:5 个月前 )
186a802e
added ecosystem file for PM2 4 年前
5def40a3
Add host customization support for the NodeJS version 4 年前
更多推荐
所有评论(0)