English 中文(简体)
规定在指挥一级完成对账单的检查
原标题:Setting case-insensitivity for bash completion on a command-by-command basis

是否有办法具体指明,某一指挥机构有过不敏感的情况,而没有在全球(至少就这样说)处理不敏感的问题?

在我的特定情况下,我有一小小 app,使我可以直线访问电子邮件地址数据库,因此我:

db get email john smith

它又回到John Smith的电子邮件地址。 因此,我已设法使完成工作主要能够达到以下目的:

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

允许我填写表格get/code>和>。 然而,如果我打上了<代码>j<tab>,那么它就拒绝,因为在电子邮件数据库中,它有适当的资本。 我愿拿双管齐下,以完成这项工作。 (如果我使用资本代码,则该代码有效。)

如果做不到这一点,我可以有我的<代码>-complete方案,通过匹配投入来改变其答复情况,但理想的是,指挥线将尽可能与数据库匹配。

请注意,在使用阅读线时,我只用表面上似乎是一个问题的仪表相联。

问题回答

Indeed there seems to be no way to have compgen do case-insensitive matching against the word list (-W). I see the following workarounds:

Simple solution: Translate both the word list and the input token to all-lowercase first. Note: This is only an option if it s acceptable to have all completions turn into all-lowercase.

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"))   
}

更好但更详细的解决办法: 3. Roll 你们自己的、对个案敏感的匹配逻辑:

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[@]}")
}

简便使用<条码>grep从事所有工作非常容易;然后,案件在完成时得到保留,而你不必用<条码> /条码”或与之类似的任何东西。 例如:

_example_completions () {
    local choices="john JAMES Jerry Erik eMIly alex Don donald [email protected] RON"
    COMPREPLY=( $( echo "$choices" | tr " " "
" | grep -i "^$2" ) )
}

此处,$choices为您的言词清单,tr用于将词句改为新的条码。 我们可以理解(如果你的话已经是新的线下定义的话,那么-i 备选办法是不敏感的配对,^$2"<>>>> <<<>code> >在行文一开始即与目前的措辞(通过该编码<2<<<<>>>$2<>>/code>)相对应。

$ example dO<tab>
Don     donald     [email protected]




相关问题
Parse players currently in lobby

I m attempting to write a bash script to parse out the following log file and give me a list of CURRENT players in the room (so ignoring players that left, but including players that may have rejoined)...

encoding of file shell script

How can I check the file encoding in a shell script? I need to know if a file is encoded in utf-8 or iso-8859-1. Thanks

Bash usage of vi or emacs

From a programming standpoint, when you set the bash shell to use vi or emacs via set -o vi or set -o emacs What is actually going on here? I ve been reading a book where it claims the bash shell ...

Dynamically building a command in bash

I am construcing a command in bash dynamically. This works fine: COMMAND="java myclass" ${COMMAND} Now I want to dynamically construct a command that redirectes the output: LOG=">> myfile.log ...

Perform OR on two hash outputs of sha1sum

I want perform sha1sum file1 and sha1sum file2 and perform bitwise OR operation with them using bash. Output should be printable i.e 53a23bc2e24d039 ... (160 bit) How can I do this? I know echo $(( ...

Set screen-title from shellscript

Is it possible to set the Screen Title using a shell script? I thought about something like sending the key commands ctrl+A shift-A Name enter I searched for about an hour on how to emulate ...