How to extract suffix from a filename?

Randall R Schulz
Tue Jun 26 07:44:00 GMT 2001


I know nothing of Mutt nor what interpretation is placed on the contents of 
a ".mailcap" file, but your example looks like shell code to me, so as a 
fond BASH user I'll direct you to its feature allowing string manipulations 
upon the value of variables as they are being expanded.

% FULLNAME="/dir/base.suf1.suf2"
% DIR="${FULLNAME%/*}"
% FILE="${FULLNAME##*/}"
% MAXBASE="${FILE%.*}"
% MINBASE="${FILE%%.*}"
% MAXSUF="${FILE#*.}"
% MINSUF="${FILE##*.}"
/dir base.suf1.suf2 base.suf1 base suf1.suf2 suf2

The patterns are matched using shell "glob" pattern matching (as in file 
name matching: ?, * and [...] patterns), not regular expression matching. 
The MAX/MIN pairs accounts for the possibility of multiple periods in the 
FILE name.

See section 3.10 of the BASH manual (that's an old version of the manual, 
so it may have been displaced to an adjacent section in later editions).

There is also a generalized substitute mode of expansion. It's documented 
in the same place.

Randall Schulz
Mountain View, CA USA

At 02:38 2001-06-26, malcolm.boekhoff wrote:
>Okay, it's a silly question - how can I extract the suffix from a file
>name (I know I can use "basename" and "dirname" to get the file and
>directory components)?
>Read the rest of this mail message if you can be bothered.
>I want to do this because my script executed from my .mailcap file in
>mutt spawns off internet explorer or word or whatever other GUI thing is
>required to read attachments like ".doc" and ".pps", etc. and I can keep
>reading mail in mutt asynchronously.  To make this work I need to copy
>the temporary file created by mutt and sometimes maybe this needs to end
>in ".doc" and I would rather not hard-code the suffix in the script,
>because, maybe somtimes, say for image/* mimetype, the suffix could be
>".jpg" or ".tiff", etc. and maybe the invocation of IE5 requires the
>target to end in ".jpg" or ".tiff" and it probably isn't right for me to
>copy the "source.tiff" to "target.jpg" and start IE5 on the target.
>I would like to use the suffix on the file supplied by mutt here,
>instead of hard coding it.                                   |
>                                                              |
>                                                              |
>                                                              |
>                                             +----------------+
>..                                         |
>             application/       v
>                 TMPFILE=`mktemp $TEMPLATE`.xls
>                 COMMAND="/c/program\ files/microsoft\ office/office\
>/excel \"`cygpath -a -w \"$TMPFILE\"`\"&"
>             ;;
>cp -fp "$SRCFILE" "$TMPFILE"
>eval $COMMAND
>This message has been checked for all known viruses by the
>E-mail Virus Protection service, powered by MessageLabs.
>Unsubscribe info:
>Bug reporting:

Unsubscribe info:
Bug reporting:

More information about the Cygwin mailing list