在 bash shell 中,如果你输入一些系统上未安装的命令,它会抛出一个错误,并给你一个软件包安装建议。例如-
$ iostat
Command 'iostat' not found, but can be installed with:
sudo apt install sysstat
我个人最喜欢的是 tcsh,但是当找不到某些命令时,它不会给出任何软件包安装建议。如何在 tcsh 中获取软件包安装建议(如 bash)?
答案1
魔法之所以有效,是因为command_not_found
包裹command_not_found_handle
为两者提供功能狂欢和嘚。
这适用于狂欢因为这片段在/etc/bash.bashrc
# if the command-not-found package is installed, use it
if [ -x /usr/lib/command-not-found -o -x /usr/share/command-not-found/command-not-found ]; then
function command_not_found_handle {
# check because c-n-f could've been removed in the meantime
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 "%s: command not found\n" "$1" >&2
return 127
fi
}
fi
您还/etc/zsh_command_not_found
可以获取一个文件来获取该行为嘚— 这里,该函数被称为command_not_found_handler
。
从快速搜索来看,似乎并非如此tcsh包括这样的功能来定义command_not_found_handle
。因此,名称可能不同,在这种情况下,您只需将代码片段转换为tcsh并将其包含在您的中~/.tcshrc
,否则您不能!