简体   繁体   English

为bash设置默认命令

[英]setting a default command for bash

Suppose I create my own script and I want that script to be the default command to be executed if command is not found, how do I go about configuring bash to make this happen 假设我创建自己的脚本,并且我希望该脚本是未找到命令时要执行的默认命令,如何配置bash以实现此目的

Eg. 例如。 the command is called defCMD 该命令称为defCMD

Suppose I type some random string into the command line: 假设我在命令行中输入一些随机字符串:

$ viosjaldfksajflsfa

Normally it would output 'viosjaldfksajflsfa not found' 通常它会输出'viosjaldfksajflsfa not found'

However, instead of that happening I want 'viosjaldfksajflsfa' to be fed as parameter into the defCMD script I wrote and therefore have bash execute defCMD instead. 但是,我希望'viosjaldfksajflsfa'作为参数输入到我编写的defCMD脚本中,而不是发生这种情况,因此请使用bash执行defCMD。

is there a way to make this happen? 有没有办法实现这一目标?

My bash (Fedora) has the following 我的bash(Fedora)有以下内容

$ type command_not_found_handle 
command_not_found_handle is a function
command_not_found_handle () 
{ 
    runcnf=1;
    retval=127;
    [ ! -S /var/run/dbus/system_bus_socket ] && runcnf=0;
    [ ! -x /usr/libexec/packagekitd ] && runcnf=0;
    if [ $runcnf -eq 1 ]; then
        /usr/libexec/pk-command-not-found $@;
        retval=$?;
    else
        echo "bash: $1: command not found";
    fi;
    return $retval
}

You can start from here and add it to your .bashrc file. 您可以从这里开始并将其添加到.bashrc文件中。 Change the else branch. 更改else分支。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM