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.pdf
,myfile.midi
如果它们存在。我想让 bash 知道这个脚本只想读取*.ly
文件(而不是为了方便)。
在您的 中~/.bashrc
,您可以添加类似的内容来为您的 启用自动完成功能script.sh
:
complete -f -X '!*.ly' script.sh
该-f
选项指定文件名,并且-X
是过滤器模式(请注意,过滤器模式会删除与模式匹配的值,因此!
用于否定模式,以便*.ly
删除除之外的所有文件名。