让 Unix 告诉我“x 个相似的”

让 Unix 告诉我“x 个相似的”

通常,如果我在终端中执行某些操作,我会输错一些内容并输入一些无意的内容。

如果我输入int一个简单的例子,它会告诉我Command 'int' not found, but there are 18 similar ones.

不是说我需要想要知道这18个相似的命令,但是有没有办法找出这“18个相似的”到底是什么?无论是在终端还是其他地方。

答案1

如果这种情况发生在 Ubuntu 中,askubuntu.com是说bash使用/usr/lib/command-not-found它使用Python的CommandNotFound模块。

/usr/lib/python3/dist-packages/CommandNotFound/CommandNotFound.py您可以在负责“但有 X 个相似的”的确切行中看到,它们从 CommandNotFound.py 文件中的第 178 行开始:

if len(mispell_packages)+len(mispell_snaps) > max_alt:
    print("", file=self.output_fd)
    print(_("Command '%s' not found, but there are %s similar ones.") % (word, len(mispell_packages)), file=self.output_fd)
    print("", file=self.output_fd)
    self.output_fd.flush()
    return

因为没有开关、标志或任何选项来使 CommandNotFound.py 也返回那些类似命令的列表,如果你真的想知道这些包是什么,你可以编辑 python 文件的这一部分并添加两行来打印内容具有类似命令的数组,而不仅仅是该数组中的元素数量。添加的行是这部分代码中的第 4 行和第 5 行:

if len(mispell_packages)+len(mispell_snaps) > max_alt:
    print("", file=self.output_fd)
    print(_("Command '%s' not found, but there are %s similar ones.") % (word, len(mispell_packages)), file=self.output_fd)
    for x in range(len(mispell_packaged)):
        print(mispell_packages[x])
    print("", file=self.output_fd)
    self.output_fd.flush()
    return

现在,当您保存/usr/lib/python3/dist-packages/CommandNotFound/CommandNotFound.py(您需要以根用户身份编辑它)并输入 时int,您会得到:

Command 'int' not found, but there are 18 similar ones.
('itd, 'ncl-ncarg', 'universe', '')
('ant, 'ant', 'universe', '')
('inl, 'ioport', 'universe', '')
('inw, 'ioport', 'universe', '')
('tint, 'tint', 'universe', '')
('inc, 'mailutils-mh', 'universe', '')
('inc, 'mmh', 'universe', '')
('inc, 'nmh', 'universe', '')
('nit, 'python-nevow', 'universe', '')
('init, 'systemd-sysv', 'main', '')
('itv, 'python-invoke', 'universe', '')
('itv, 'python3-invoke', 'universe', '')
('cnt, 'open-infrastructure-container-tools', 'universe', '')
('inb, 'ioport', 'universe', '')
('ent, 'ent', 'universe', '')
('ink, 'ink', 'universe', '')
('iyt, 'python3-yt', 'universe', '')
('iat, 'iat', 'universe', '')

该列表包含类似的命令名称(itd、and、inl、inw、tint)以及提供此命令的包(ncl-ncarg、ant、ioport)及其来自的存储库(宇宙,主要)。

希望它能满足您的好奇心:)说实话,读完您的帖子后我也很好奇。

相关内容