在逐个命令的基础上为bash完成设置不区分大小写

有没有办法指定特定命令具有不区分大小写,而不是全局打开不区分大小写(至少对于那个 shell)?

在我的特定情况下,我有一个小应用程序,它让我命令行访问电子邮件地址的数据库,所以我输入:

db get email john smith

然后它返回John Smith的电子邮件地址.所以我设法在应用程序内部完成启用:设置

COMPREPLY=($(compgen -W "$(db --complete $COMP_CWORD "$COMP_WORDS[@]"}")" -- ${COMP_WORDS[COMP_CWORD]}))

允许我选项卡完成获取和电子邮件.但是,如果我然后键入j< tab>,则拒绝,因为在电子邮件数据库中,它被正确地大写.无论如何,我想要bash来完成这个. (如果我使用大写字母J,它可以工作.)

如果失败了,我可以让我的–complete选项通过匹配输入来改变其回复的情况,我想,但理想情况下,如果可能的话,命令行将与数据库匹配.

请注意,我在使用readline时在app中工作,它只与bash接口似乎是一个问题.

实际上似乎没有办法让compgen对单词列表(-W)进行不区分大小写的匹配.
我看到以下解决方法:

简单的解决方案:首先将单词列表和输入标记翻译为全小写.
注意:如果所有完成都变为全小写,则这只是一个选项.

complete_lower() {

    local token=${COMP_WORDS[$COMP_CWORD]}
    local words=$( db --complete $COMP_CWORD "${COMP_WORDS[@]}" )

    # Translate both the word list and the token to all-lowercase.
    local wordsLower=$( printf %s "$words" | tr [:upper:] [:lower:] )
    local tokenLower=$( printf %s "$token" | tr [:upper:] [:lower:] )

    COMPREPLY=($(compgen -W "$wordsLower" -- "$tokenLower"))   
}

更好,但更精细的解决方案:滚动自己的,不区分大小写的匹配逻辑:

complete_custommatch() {

    local token=${COMP_WORDS[$COMP_CWORD]}
    local words=$( db --complete $COMP_CWORD "${COMP_WORDS[@]}" )

    # Turn case-insensitive matching temporarily on,if necessary.
    local nocasematchWasOff=0
    shopt nocasematch >/dev/null || nocasematchWasOff=1
    (( nocasematchWasOff )) && shopt -s nocasematch

    # Loop over words in list and search for case-insensitive prefix match.
    local w matches=()
    for w in $words; do
        if [[ "$w" == "$token"* ]]; then matches+=("$w"); fi
    done

    # Restore state of 'nocasematch' option,if necessary.
    (( nocasematchWasOff )) && shopt -u nocasematch

    COMPREPLY=("${matches[@]}")
}

相关文章

用的openwrt路由器,家里宽带申请了动态公网ip,为了方便把2...
#!/bin/bashcommand1&command2&wait从Shell脚本并行...
1.先查出MAMP下面集成的PHP版本cd/Applications/MAMP/bin/ph...
1、先输入locale-a,查看一下现在已安装的语言2、若不存在如...
BashPerlTclsyntaxdiff1.进制数表示Languagebinaryoctalhexa...
正常安装了k8s后,使用kubect工具后接的命令不能直接tab补全...