如何替换“bash:$ COMMAND:command not found”消息?

时间:2022-07-11 15:43:33

Any help would be appreciated.

任何帮助,将不胜感激。

Basically, I want to replace:

基本上,我想要替换:

~]$ obvfake
bash: obvfake: command not found

With:

~]$ obvfake
[*] command not found

Thanks.

4 个解决方案

#1


8  

bash version 4 introduces a hook for handling missing commands; add the following to your .bashrc file.

bash版本4引入了一个用于处理缺失命令的钩子;将以下内容添加到.bashrc文件中。

command_not_found_handle () {
    printf "[*] command not found\n"
    return 127
}

#2


3  

You can write this to your .bashrc:

你可以写这个.bashrc:

function error_handler {
  if [ $? -eq 127 ]; then
      echo "[*] command not found"
  fi  
}

trap 'error_handler' ERR 

This will still show the bash: obvfake: command not found though. You can suppress this by doing:

这仍将显示bash:obvfake:命令未找到。你可以通过这样做来抑制这个:

obvfake 2> /dev/null

#3


0  

I'd simply redirect errors to /dev/null. If obvfake returns an exit code greater than 0 then it will echo your custom error message.

我只是将错误重定向到/ dev / null。如果obvfake返回大于0的退出代码,那么它将回显您的自定义错误消息。

obvfake 2>/dev/null || echo "[*] command not found"

This might be a little be too general since it will not distinguish between error codes. So we could check for a specific exit code.

这可能有点过于笼统,因为它不会区分错误代码。所以我们可以检查一个特定的退出代码。

obvfake 2>/dev/null || {
    if (( $? == 127 )); then
        echo "[*] command not found"
    fi
}

If I'd want to check a lot of error codes I'd replace the if expression with a case statement. For the ease of use you could integrate that functionality inside your script and maybe wrap it up into a function to reuse it at various points of failure.

如果我想检查很多错误代码,我会用if语句替换if表达式。为了便于使用,您可以将该功能集成到脚本中,并将其包装到一个函数中,以便在各种故障点重用它。

You pretty much want to know more about redirection in bash. :)

您非常希望了解有关bash中重定向的更多信息。 :)

EDIT: I guess I misinterpreted the original question. I thought obvfake is a custom script complaining about commands being called but not found on the system.

编辑:我想我误解了原来的问题。我认为obvfake是一个自定义脚本,抱怨调用命令但在系统上找不到。

#4


-1  

As suggested by chepner... you can customize the default message by replacing the bash function (handles Signal 127 or command-not-found functionality) with the one designed by you and include that function in .bashrc script.

正如chepner所建议的......您可以通过将bash函数(处理Signal 127或命令未找到的功能)替换为您设计的函数来自定义默认消息,并将该函数包含在.bashrc脚本中。

# function that handles command-not-found message.
command_not_found_handle() {

 echo -e "My Friend, '$1' is a typo. Please correct it and re-enter the command."  
  return 127

}

You also can check this at: http://bitdiffferent.com/command-not-found/

您也可以访问:http://bitdiffferent.com/command-not-found/

#1


8  

bash version 4 introduces a hook for handling missing commands; add the following to your .bashrc file.

bash版本4引入了一个用于处理缺失命令的钩子;将以下内容添加到.bashrc文件中。

command_not_found_handle () {
    printf "[*] command not found\n"
    return 127
}

#2


3  

You can write this to your .bashrc:

你可以写这个.bashrc:

function error_handler {
  if [ $? -eq 127 ]; then
      echo "[*] command not found"
  fi  
}

trap 'error_handler' ERR 

This will still show the bash: obvfake: command not found though. You can suppress this by doing:

这仍将显示bash:obvfake:命令未找到。你可以通过这样做来抑制这个:

obvfake 2> /dev/null

#3


0  

I'd simply redirect errors to /dev/null. If obvfake returns an exit code greater than 0 then it will echo your custom error message.

我只是将错误重定向到/ dev / null。如果obvfake返回大于0的退出代码,那么它将回显您的自定义错误消息。

obvfake 2>/dev/null || echo "[*] command not found"

This might be a little be too general since it will not distinguish between error codes. So we could check for a specific exit code.

这可能有点过于笼统,因为它不会区分错误代码。所以我们可以检查一个特定的退出代码。

obvfake 2>/dev/null || {
    if (( $? == 127 )); then
        echo "[*] command not found"
    fi
}

If I'd want to check a lot of error codes I'd replace the if expression with a case statement. For the ease of use you could integrate that functionality inside your script and maybe wrap it up into a function to reuse it at various points of failure.

如果我想检查很多错误代码,我会用if语句替换if表达式。为了便于使用,您可以将该功能集成到脚本中,并将其包装到一个函数中,以便在各种故障点重用它。

You pretty much want to know more about redirection in bash. :)

您非常希望了解有关bash中重定向的更多信息。 :)

EDIT: I guess I misinterpreted the original question. I thought obvfake is a custom script complaining about commands being called but not found on the system.

编辑:我想我误解了原来的问题。我认为obvfake是一个自定义脚本,抱怨调用命令但在系统上找不到。

#4


-1  

As suggested by chepner... you can customize the default message by replacing the bash function (handles Signal 127 or command-not-found functionality) with the one designed by you and include that function in .bashrc script.

正如chepner所建议的......您可以通过将bash函数(处理Signal 127或命令未找到的功能)替换为您设计的函数来自定义默认消息,并将该函数包含在.bashrc脚本中。

# function that handles command-not-found message.
command_not_found_handle() {

 echo -e "My Friend, '$1' is a typo. Please correct it and re-enter the command."  
  return 127

}

You also can check this at: http://bitdiffferent.com/command-not-found/

您也可以访问:http://bitdiffferent.com/command-not-found/