简体   繁体   English

为可执行文件设置bash TAB自动完成参数候选

[英]Set bash TAB autocomplete parameter candidates for an executable

I have a bourne script that takes the name of an executable as a parameter and installs an executable archived by jenkins to the executable path. 我有一个bourne脚本,该脚本以可执行文件的名称作为参数,并将由jenkins存档的可执行文件安装到可执行文件路径。

root@host:~# cat `which jinstall`
#! /bin/sh

if [ "$#" -eq 2 ]; then
    if ! [ -e "/var/lib/jenkins/jobs/$1/builds/$2/archive/$1" ]; then
        echo "$1 build $2 not found"
        exit 1
    fi

    rm /opt/user/bak/$1 2>/dev/null
    mv /opt/user/bin/$1 /opt/user/bak 2>/dev/null
    cp /var/lib/jenkins/jobs/$1/builds/$2/archive/$1 /opt/user/bin
    chown root:user /opt/user/bin/$1
    chmod 650 /opt/user/bin/$1
    ls -al /opt/user/bin/$1
else
    echo "Usage: jinstall <executable> <build_number>"
fi

The question I have is: how do I set the bash autocomplete candidates for the parameters of this script? 我的问题是:如何为该脚本的参数设置bash自动完成候选者? This is for a BASH shell. 这是用于BASH shell。

When I type in a command like "killall" and a few letters then press TAB, bash autocompletes the parameter with an executable name. 当我输入“ killall”之类的命令并输入几个字母然后按TAB时,bash会使用可执行文件名自动完成该参数。 I'd like the parameter to autocomplete using an executable already in /opt/user/bin OR the name of a subdirectory of /var/lib/jenkins/jobs/ 我希望参数使用/ opt / user / bin中已有的可执行文件或/ var / lib / jenkins / jobs /的子目录的名称自动完成

Thanks 谢谢

The following bash script will enable desired autocompletion under the assumption that your filenames don't contain whitespace symbols : 您的文件名不包含空格符号的假设下,以下bash脚本将启用所需的自动完成功能:

jinstall_completion() {
   COMPREPLY=(
       $(
           shopt -s nullglob;
           for x in /var/lib/jenkins/jobs/"$2"* /opt/user/bin/"$2"*
           do
               basename "$x"
           done|sort -u
       )
   );
}

complete -o filenames -F jinstall_completion jinstall

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM