问题 bash完成的'have'关键字


have bash中的关键字?或者bash完成脚本使用的语言不是bash?

have gcc &&
_gcc()
{

是很常见。看到: grep "have .* &&" /etc/bash_completion.d/*

我找不到关于我看过的bash完成教程的任何信息,我找不到任何信息 man bash。谷歌“有”也很难。我在哪里可以找到相关文档?

我猜这与确保那里有关 gcc 存在于 PATH

编辑:是的。 /etc/bash_completion 包含:

have()
{
    unset -v have
    # Completions for system administrator commands are installed as well in
    # case completion is attempted via `sudo command ...'.
    PATH=$PATH:/sbin:/usr/sbin:/usr/local/sbin type $1 &>/dev/null &&
    have="yes"
}

7978
2017-10-13 16:59


起源



答案:


have 和 _have 只是在基础中定义的两个函数 bash_completion 文件。在两者之间,它们形成了内置的包装 type 用于确定特定命令/程序是否可用的命令。

# This function checks whether we have a given program on the system.
#
_have()
{
    # Completions for system administrator commands are installed as well in
    # case completion is attempted via `sudo command ...'.
    PATH=$PATH:/usr/sbin:/sbin:/usr/local/sbin type $1 &>/dev/null
}

# Backwards compatibility for compat completions that use have().
# @deprecated should no longer be used; generally not needed with dynamically
#             loaded completions, and _have is suitable for runtime use.
have()
{
    unset -v have
    _have $1 && have=yes
}

16
2017-10-13 17:05



谢谢chepner。现在我明白了。当bash读取完成文件时,如果PATH没有程序,则不需要将完成脚本保留在内存中,因此它只是跳过它。有一个完美的感觉。我把我的 have 在我的问题中供参考。我想我没有 _have。我想知道为什么。 - Xu Wang
我不太了解bash完成项目的内部情况。我从上面提取了上面的代码 庆典完成 git 知识库 就在今天早上;考虑到这一点,它可能是一个新的发展方向 have 被弃用赞成 _have。 - chepner
是的,我打赌你是对的。谢谢。 - Xu Wang


答案:


have 和 _have 只是在基础中定义的两个函数 bash_completion 文件。在两者之间,它们形成了内置的包装 type 用于确定特定命令/程序是否可用的命令。

# This function checks whether we have a given program on the system.
#
_have()
{
    # Completions for system administrator commands are installed as well in
    # case completion is attempted via `sudo command ...'.
    PATH=$PATH:/usr/sbin:/sbin:/usr/local/sbin type $1 &>/dev/null
}

# Backwards compatibility for compat completions that use have().
# @deprecated should no longer be used; generally not needed with dynamically
#             loaded completions, and _have is suitable for runtime use.
have()
{
    unset -v have
    _have $1 && have=yes
}

16
2017-10-13 17:05



谢谢chepner。现在我明白了。当bash读取完成文件时,如果PATH没有程序,则不需要将完成脚本保留在内存中,因此它只是跳过它。有一个完美的感觉。我把我的 have 在我的问题中供参考。我想我没有 _have。我想知道为什么。 - Xu Wang
我不太了解bash完成项目的内部情况。我从上面提取了上面的代码 庆典完成 git 知识库 就在今天早上;考虑到这一点,它可能是一个新的发展方向 have 被弃用赞成 _have。 - chepner
是的,我打赌你是对的。谢谢。 - Xu Wang