当前位置: 首页>>技术教程>>正文


挂接到Ubuntu中的“command not found”处理程序

, ,

问题描述

我想挂接到未找到命令的处理程序中

wim@SDFA100461C:~$ thing
No command 'thing' found, did you mean:
 Command 'tping' from package 'lam-runtime' (universe)
 Command 'thin' from package 'thin' (universe)
thing: command not found

我想用自己的脚本覆盖此行为。

具体来说,我想检查命令是否存在于lsvirtualenv -b的输出中,如果存在,我想激活该virtualenv。

我应该从哪里开始黑客攻击?

最佳方法

一般来说

Linux Journal的文章不错:

从bash的手册页中:

… A full search of the directories in PATH is performed only if the command is not found in the hash table. 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 with the original command and the original command’s arguments as its arguments, and the function’s exit status becomes the exit status of the shell. If that function is not defined, the shell prints an error message and returns an exit status of 127.

A quick grep in /etc discovered where it was happening. The function itself is in /etc/bash_command_not_found and that function gets included (if it exists) in your bash session via /etc/bash.bashrc.

Ubuntu 14.04

经验证据表明,在Ubuntu 14.04安装中,文件/etc /bash_command_not_found不存在,但是,正确的文件是python脚本,位于/usr /lib /command-not-found

参考资料

本文由Ubuntu问答整理, 博文地址: https://ubuntuqa.com/article/11616.html,未经允许,请勿转载。