Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash Completion:我们可以用它做什么,未来会发生什么_Bash_Tab Completion_Programmable Completion - Fatal编程技术网

Bash Completion:我们可以用它做什么,未来会发生什么

Bash Completion:我们可以用它做什么,未来会发生什么,bash,tab-completion,programmable-completion,Bash,Tab Completion,Programmable Completion,Bash允许您使用TAB键在参数中完成命令名和文件名。 但为什么不同时提供命令的公共选项呢?更好的是,为什么不建立一个完整的系统来告诉你一个选项的功能呢 我听说了可编程完成。。但是我不知道它适合什么 所以我的问题是:有没有办法实现我的要求?与Bash结合使用的其他工具可能。。还是别的什么 这是bash的标准功能:。bash确实支持参数补全:bash补全非常可定制,可以显示常见命令的选项。配置文件只需要指定so。在Debian中,bash completion附带了一组配置文件,用于完成不同命令的

Bash允许您使用TAB键在参数中完成命令名和文件名。 但为什么不同时提供命令的公共选项呢?更好的是,为什么不建立一个完整的系统来告诉你一个选项的功能呢

我听说了可编程完成。。但是我不知道它适合什么


所以我的问题是:有没有办法实现我的要求?与Bash结合使用的其他工具可能。。还是别的什么

这是bash的标准功能:。

bash确实支持参数补全:

bash补全非常可定制,可以显示常见命令的选项。配置文件只需要指定so。在Debian中,bash completion附带了一组配置文件,用于完成不同命令的选项

例如:

alanhaggai@neon:~$ tar
A  c  d  r  t  u  x

下面是bash的官方手册页:

在OS X上,您可以按照说明使用MacPorts轻松安装一组预定义的
bash
完成。查看
/opt/local/etc/bash_completion.d
中的示例可以为您提供一些制作其他示例的想法。

如果您想要更出色的完成,请查看。它非常类似于bash,并从ksh、tcsh等方面获得了一些好的特性。它还修复了一些恼人的bash bug

它可以做一些非常酷的事情,包括为scp完成远程文件名(如果您已经设置了基于密钥的身份验证),以及在您完成选项卡时完成小文档 例如,前几天我在“jar”上做了一个标签,我感到非常惊喜:

[steven@sexy:~]% jar <tab>
0  -- store only without using ZIP compression
M  -- do not create manifest file
etc...
[steven@sexy:~]%jar
0--仅存储而不使用ZIP压缩
M--不创建清单文件
等

我不喜欢这里的任何答案,我想举几个具体的例子

<>我有时有一个有一个共同前缀的目录,希望通过输入目录名的中间部分或目录名的唯一部分来完成对它们的CD。

我能够编写一个完成函数来完成以下任务:

_cd(){
    # Backup old nullglob setting
    local shoptbakup="`shopt -p nullglob`"
    shopt -s nullglob

    local cur opts i opt
    local IFS=$'\n'
    cur="${COMP_WORDS[COMP_CWORD]}"

    # Home directory substitution
    if [[ "${cur:0:1}" == '~' ]]; then
        cur="$HOME/${cur:1}"
    fi

    if [[ $cur == */* ]]; then
        opts=(${cur}*/)
    else
        opts=(*${cur}*/)
    fi

    # Prevent trailing//
    i=0
    for opt in "${opts[@]}"; do
        #opts[$i]=${opt%${opt##*[!/]}}
        opts[$i]=${opt%/}
        i=$((i+1))
    done

    # Restore nullglob setting
    eval "$shoptbakup" 2>/dev/null

    COMPREPLY=("${opts[@]}")
}
complete -o filenames -o bashdefault -o nospace -F _cd cd
现在,在我的提示下,我可以这样做:

$ ls
test 6/  test 7/  test 8/  test1/  test2/  test3/  test4/  test5/
$ cd 7[TAB]
$ listanimals --[TAB][TAB]
--size            --sort-direction  --type
$ listanimals --t[TAB]
$ listanimals --type [TAB][TAB]
birds       fish        insects     mammals     marsupials  misc        reptiles
并自动完成以下操作:

$ cd test\ 7/
您可能希望使用可编程完成的另一个原因是,如果您编写脚本并希望能够为您列出或完成选项。本页有一些非常好的示例:

下面是一个例子:

假设我有一个脚本叫做listanimals。我可以编写一个完成函数来帮助填写选项

# complete sendevent
_listanimals()
{
    local cur=${COMP_WORDS[COMP_CWORD]}
    local prev=${COMP_WORDS[COMP_CWORD-1]}

    if [[ $prev == "--sort-direction" ]]; then
        COMPREPLY=( $( compgen -W "horizontal vertical" -- $cur ) )
        return 0
    elif [[ $prev == "--type" ]];then
        COMPREPLY=( $( compgen -W "mammals reptiles birds fish marsupials insects misc" -- $cur ) )
        return 0
    elif [[ $prev == "--size" ]]; then
        COMPREPLY=( $( compgen -W "tiny small medium large huge" -- $cur ) )
        return 0
    fi


    # completing an option
    if [[ "$cur" == --* ]] || [[ $cur == "" ]]; then
        COMPREPLY=( $( compgen -W "--sort-direction --type --size" -- $cur ) )
    fi
}
complete -F _listanimals listanimals
现在我可以做到:

$ ls
test 6/  test 7/  test 8/  test1/  test2/  test3/  test4/  test5/
$ cd 7[TAB]
$ listanimals --[TAB][TAB]
--size            --sort-direction  --type
$ listanimals --t[TAB]
$ listanimals --type [TAB][TAB]
birds       fish        insects     mammals     marsupials  misc        reptiles
将自动填写以下内容:

$ listanimals --type
然后我可以这样做:

$ ls
test 6/  test 7/  test 8/  test1/  test2/  test3/  test4/  test5/
$ cd 7[TAB]
$ listanimals --[TAB][TAB]
--size            --sort-direction  --type
$ listanimals --t[TAB]
$ listanimals --type [TAB][TAB]
birds       fish        insects     mammals     marsupials  misc        reptiles
我相信你能解决剩下的问题。这是一个有效的示例,因此如果您想试用它,只需将其复制/粘贴到一个文件(例如sample.sh)中,并在bash
source sample.sh
中对其进行源代码转换。实际上,您不需要有一个名为listanimals的脚本


希望有人会发现这很有用,因为我发现这很难理解。

我在mac上使用OSX 10.6,我使用的是GNUBash 3.2.48版