Reputation: 359
I want to execute a pre-set command automatically after I run some command witch can not be found. Is there any solution?
Let me clear my question. Set a command, and this command will be executed automatically when I type some command in the shell and get "command not found" result.
Upvotes: 2
Views: 572
Reputation: 1675
With zsh, use the function command_not_found_handler:
command_not_found_handler () {
echo Oups
}
With bash, use the function command_not_found_handle:
command_not_found_handle () {
echo Oups
}
Using this mechanism, there is a tool for Archlinux that gives you the package that contains your missing command. This tool is called command_not_found and is available on AUR and on Github. Similar applications exist for other distributions.
Upvotes: 5
Reputation: 4043
Try the command to achieve your goal,
cmd1 2>&1 | grep "command not found" > /dev/null && cmd2
Brief explanation,
cmd1
: is the command may no be found in your systemcmd2
: if error message for cmd1
is "command not found", execute cmd2
Upvotes: 0
Reputation: 25789
The same way you can chain commands to execute only if previous was successful using the logical and
operator:
command1 && command2
you can use the logical or
operator to have command2
run only if command1
failed:
command1 || command2
This will, however, capture all error states from command1
, not just the not found
condition.
Upvotes: 1