使 bash-autocomplete 识别特定脚本的特定文件类型

nat*_*ath 1 bash autocomplete shell-script

如何bash识别特定文件类型的特定文件script

例如,我写了一个小脚本来编译一个lilypond-file,然后自动打开编译后的pdf

#!/bin/bash

run() {
    lilypond "$1"
    name=$(basename "$1" .ly)
    if [ -f "${name}.pdf" ]; then
        xpdf -cont -remote LilyPreview "${name}.pdf" &
    else
        printf "\\n file %s.pdf is not present..!\\n\\n" "$name"
    fi
}

run "$@"
Run Code Online (Sandbox Code Playgroud)

我怎样才能使bash知道,只能建议*.ly从文件夹中的文件,而忽略*.pdf*.midi文件具有相同的基本名称。


更新 2.3:

所以当我想运行时:script.sh myfile.ly,然后 bashs 自动完成也会建议myfile.pdfmyfile.midi如果它们存在。我想让 bash 知道这个脚本只想读取*.ly文件(而不是为了方便)。

use*_*931 5

在您的 中~/.bashrc,您可以添加类似的内容来为您的 启用自动完成功能script.sh

complete -f -X '!*.ly' script.sh

-f选项指定文件名,并且-X是过滤器模式(请注意,过滤器模式会删除与模式匹配的值,因此!用于否定模式,以便*.ly删除除之外的所有文件名。

  • @nath 是的,您可以指定 `-o plusdirs` 到 `complete` 以将目录添加到列表中。 (2认同)