2012-10-13 34 views
11

have在bash中的關鍵字?或者bash完成腳本使用的是不是bash的語言?'有'關鍵字爲bash完成

have gcc && 
_gcc() 
{ 

這很常見。請參閱:grep "have .* &&" /etc/bash_completion.d/*

我找不到關於bash完成教程的任何信息,我見過並且我在man bash中找不到任何信息。谷歌「擁有」也很困難。我在哪裏可以找到相關文件?

我猜它必須確保PATH中存在gcc

編輯:是的。 /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" 
} 

回答

15

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 
} 
+0

謝謝chepner。現在我明白了。當bash讀入完成文件時,如果PATH沒有程序,則不需要將完成腳本保存在內存中,因此它只是跳過它。非常有意義。我在我的問題中加入了我的「有」,以供參考。我想我沒有'_have'。我想知道爲什麼。 –

+0

我不太瞭解bash完成項目的內部結構。我今天早上從[bash-completion'git' repository](http://bash-completion.alioth.debian.org)上拉了上面的代碼;它可能是一個新的發展方向,因爲有關'have'的提示已被棄用而傾向於_have。 – chepner

+0

是的,我敢打賭,你是對的。謝謝。 –