0

登录到 shell 时,我希望将当前 .bashrc 配置文件中执行的所有命令“重定向”到同一命令。

例子:

ls /home/bin/
foo

home$ foo
# will call /home/bin/foo given the right $PATH

home$ bar
# will give "command not found" how can i make it so it call foo?
4

2 回答 2

1

bash版本 4 及更高版本中,如果您定义了一个名为 的函数command_not_found_handlebash它将在找不到命令时调用它。您可以使用它来运行备用命令。

command_not_found_handle () {
    missing_command=$1
    shift
    arguments=$@
    echo "$missing_command not found, running 'foo' instead"
    foo
}

把它放在你的.bashrc.

于 2013-05-23T12:08:01.847 回答
0

实现这一点的最简单方法是创建包装器外壳提示符,即交互式外壳脚本

MY_PROMPT='$ '
while :
do
  echo -n "$MY_PROMPT"
  read line
  foo "$line"
  done

exit 0

这将接受用户输入并将其传递给 foo。但我仍然想知道你需要什么?我不知道任何 bash 选项

于 2013-05-23T06:56:45.410 回答