1

How can I make bash recognize a certain filetype for a specific script?

for example, I wrote a tiny script to compile a lilypond-file and then automatically open the compiled 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 "$@"

how can I make bash know, to only suggest *.ly files from the folder and ignore *.pdf and *.midi files with the same basename.


Update 2.3:

so when I want to run: script.sh myfile.ly, then bashs autocomplete also suggests myfile.pdf and myfile.midi if they are present. I want bash to know that this script only wants to read *.ly files (rather as a matter of convenience).

2
  • 1
    What exactly do you mean by autocomplete? How do you want to call the script?
    – WerKater
    Dec 15, 2018 at 23:31
  • @nath If you run script.sh myfile.ly, why should the other filenames matter?
    – Kusalananda
    Dec 15, 2018 at 23:42

1 Answer 1

5

In your ~/.bashrc, you could add something like this to enable autocompletion for your script.sh:

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

The -f option specifies filenames, and -X is the filter pattern (note that the filter pattern removes values matching the pattern, so ! is used to negate the pattern such that all filenames except *.ly are removed.

2
  • nice, thanks. Would there be an option to allow folders to be displayed?
    – nath
    Dec 16, 2018 at 0:08
  • 2
    @nath yes, you can specify -o plusdirs to complete to add directories to the list.
    – user234931
    Dec 16, 2018 at 0:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .