Custom filename completion in bash

John Abreau jabr at blu.org
Wed Jan 23 17:59:56 EST 2002


I just learned recently that bash supports custom filename completion,
and after playing with it a bit I got it working correctly this evening.
I thought I'd share my results with the group.

I have a set of perl scripts I put together a while back for indexing
my video recordings. This included a script that, given the name of
a TV series, would display a list of missing episodes (i.e., episodes
that don't appear in the mysql database).

I keep the episode guides, downloaded from epguides.com and munged into
a simple format, in a directory mounted off my nfs fileserver. For 
instance,
the guide for "3rd Rock From the Sun" appears as "/u/epguides/3rd-rock/guid
e"
(and "/u/epguides/3rd-rock/details" for detailed synopses of each episode).
The "missing" script, invoked as

    missing 3rd-rock

would then print out a list of all 3rd Rock episodes I haven't recorded 
yet.

Anyway, it was mildly annoying having to type out the whole series name
every time, so bash's custom completion feature was nice to discover.

Here's how I implemented it:

    # bash function to do the autocompletion
    _epguides() {
       local cur
       COMPREPLY=()
       cur=${COMP_WORDS[COMP_CWORD]}
       COMPREPLY=( $(/bin/ls -l /u/epguides/ | \
            awk "/^d/ && \$9 ~ /^${cur}/ { print \$9 }" ) )
       return 0
    }

    # completion command - uses the above function
    complete -F _epguides -o filenames missing


-- 
John Abreau / Executive Director, Boston Linux & Unix 
ICQ 28611923 / AIM abreauj / JABBER jabr at jabber.org / YAHOO abreauj
Email jabr at blu.org / WWW http://www.abreau.net / PGP-Key-ID 0xD5C7B5D9
PGP-Key-Fingerprint 72 FB 39 4F 3C 3B D6 5B E0 C8 5A 6E F1 2C BE 99


-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 344 bytes
Desc: not available
URL: <http://lists.blu.org/pipermail/discuss/attachments/20020123/8d99e43e/attachment.sig>


More information about the Discuss mailing list