gpt4 book ai didi

bash-completion - 第一次调用命令时定义的完成函数

转载 作者:行者123 更新时间:2023-11-29 09:09:12 25 4
gpt4 key购买 nike

我添加了一个新的别名 scp_using_rsync,它使用 rsync 通过 SSH 通过特定选项复制文件。我想将 scp 的 bash 完成链接到这个别名。

当我添加这一行时它起作用了:

complete -o bashdefault -o default -o nospace -F _scp scp_using_rsync 2>/dev/null || complete -o default -o nospace -F _scp scp_using_rsync

唯一的问题是我注意到,_scp 在我的 bash 环境中被定义,只有在我在该 shell 中至少尝试使用 ssh/scp 进行制表符补全一次之后。因此,如果我在新的 shell 中直接运行 scp_using_rsync,我会得到 _scp not found 错误。

typeset -F 在新 shell 中尝试 ssh 或 scp 命令的制表符完成前后的输出清楚地表明以下函数在第一次尝试制表符完成后得到定义:

$ diff ~/.scratch/file1 ~/.scratch/file2
224a225,227
> declare -f _scp
> declare -f _scp_local_files
> declare -f _scp_remote_files
226a230
> declare -f _sftp
230a235,240
> declare -f _ssh
> declare -f _ssh_ciphers
> declare -f _ssh_macs
> declare -f _ssh_options
> declare -f _ssh_suboption
> declare -f _ssh_suboption_check

在我的系统中,这些函数似乎是在 /usr/share/bash-completion/completions/ssh 中定义的。

这些是我的 2 个相互关联的问题:

  • bash 如何找出自动获取定义的位置并在第一次尝试完成时定义它们?
  • 我应该如何以类似的方式将 scp_using_rsync 的 bash 完成链接到 scp 的 bash 完成?

最佳答案

Bash 4.1添加了一个新的 -D complete 的选项, compgencompopt :

New complete/compgen/compopt -D option to define a `default' completion: a completion to be invoked on command for which no completion has been defined. If this function returns 124, programmable completion is attempted again, allowing a user to dynamically build a set of completions as completion is attempted by having the default completion function install individual completion functions each time it is invoked.

有一个例子来自 bash 的 manual :

_completion_loader()
{
. "/etc/bash_completion.d/$1.sh" >/dev/null 2>&1 && return 124
}
complete -D -F _completion_loader

关于bash-completion - 第一次调用命令时定义的完成函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15858271/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com