Bash:オートコンプリートを使用するときに変数名の置換を避けるには?

Bash:オートコンプリートを使用するときに変数名の置換を避けるには?

Bashはパスから変数名の削除を中止し、その値に置き換えたいと思います。パスのオートコンプリートを適用した後、変数を置き換えないままにすることに成功せず、さまざまなbashバージョンで複数の「shopts」設定を試しました。 $ APPSERVERと入力してTabキーを押してから、次の結果が必要です。

$APPSERVER/foo/bar

変える

/terribly-long-path-to-desired-appserver-instance/foo/bar

後者は事実の後にスクリプトを抽出するのを面倒にします。 BASHをオートコンプリートに設定し、変数名を保持する方法を知っている人はいますか?

ベストアンサー1

これがあなたが望むものです最大布材。

はい:

$ at_path $HOME D<tab><tab>
Desktop/    Documents/  Downloads/  Dropbox/ 
$ at_path $HOME Doc<tab>
$ at_path $HOME Documents/<tab><tab>
Documents/projects/   Documents/scripts/  Documents/utils/      
Documents/clients/
$ at_path $HOME Documents/cli<tab>
$ at_path $HOME Documents/clients/<enter>
/home/bill-murray/Documents/clients/

ファイルをコピーしてインポートして機能させます

#
#  The function to provide the "utility": stitch together two paths
#
at_path () {
  printf "${1}/${2}"
}


#
# The completion function
#
_at_path () {

    # no pollution
    local base_path
    local just_path
    local full_path
    local and_path=${COMP_WORDS[2]}

    # global becasue that's how this works
    COMPREPLY=()

    if [[ ${COMP_WORDS[1]} =~ \$* ]]; then
        base_path=`eval printf "${COMP_WORDS[1]}"`
        full_path=${base_path}/${and_path}
        just_path=${full_path%/*}
        COMPREPLY=( $(find ${just_path} -maxdepth 1 -path "${base_path}/${and_path}*" -printf "%Y %p\n" |\
                      sed -e "s!${base_path}/!!" -e '/d /{s#$#/#}' -e 's/^. //' 2> /dev/null) )
    else
        COMPREPLY=()
    fi

}

#
# and tell bash to complete it
#
complete -o nospace -F _at_path at_path

この回答はかなりウサギのように行われ、他の人の解決策を続けてください!頑張ってください!

おすすめ記事