加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-16 01:18:11 所属栏目:安全 来源:网络整理
导读:有没有办法指定特定命令具有不区分大小写,而不是全局打开不区分大小写(至少对于那个 shell)? 在我的特定情况下,我有一个小应用程序,它让我命令行访问电子邮件地址的数据库,所以我输入: db get email john smith 然后它返回John Smith的电子邮件地址.所以我
有没有办法指定特定命令具有不区分大小写,而不是全局打开不区分大小写(至少对于那个 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[@]}")
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读