有没有一种方法来捕获用户命令bash哪些没有找到,并用它做什么?
问题描述:
假设我在bash型saklfjsklj,我得到:有没有一种方法来捕获用户命令bash哪些没有找到,并用它做什么?
bash: saklfjsklj: command not found
有没有一种方法,以拦截和执行脚本?
答
bash有一个command_not_found_handle
回调:http://www.gnu.org/software/bash/manual/bashref.html#Command-Search-and-Execution
有在this answer on askubuntu.com信息关于使用默认搞乱。
答
定义一个名为command_not_found_handle
的函数。命令和参数将作为参数传递给函数。
$ command_not_found_handle() { echo "[email protected]"'!'; }
$ zounds
zounds!
答
从bash的手册页,
*如果搜索不成功,名为command_not_found_handle定义的shell函数shell搜索。如果该函数存在,则使用原始命令和原始命令的参数作为其参数来调用该函数,并且函数的退出状态将成为shell的退出状态。如果没有定义的功能,壳打印错误消息,并返回127 *
我还发现这篇文章关于这个问题的退出状态,