help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] The best way to trim trailing .tiff or .tif?


From: Eric Blake
Subject: Re: [Help-bash] The best way to trim trailing .tiff or .tif?
Date: Fri, 05 Oct 2012 08:26:17 -0600
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:15.0) Gecko/20120911 Thunderbird/15.0.1

On 10/05/2012 04:01 AM, Davide Brini wrote:
> On Thu, 4 Oct 2012 21:02:39 -0500, Peng Yu <address@hidden> wrote:
> 
>> Hi,
>>
>> There can be many ways to trim .tiff or .tif from a filename. I'm
>> using the following code (fairly verbose). I'm sure there should be a
>> more concise (meaning less typing) way of doing it. Could anybody let
>> me know what is the most concise way? Thanks!
>>

>> function cmd {
>> local f="$1"
>> if [[ "$f" =~ \.*.tiff ]]
>> then
>>   echo "${f%.tiff}"
>> elif [[ "$f" =~ \.*.tif ]]
>> then
>>   echo "${f%.tif}"
>> else
>>   echo "$f"
>> fi
>> }
>>
>> cmd tmp.tif
>> cmd tmp.tiff
>> cmd tmp_xxx
> 
> With extended globbing one could do
> 
> printf '%s\n' "${f%@(.tiff|.tif)}"

Your use of 'function', 'local', and '[[' agree with your choice of
#!/bin/bash, in that your script is definitely non-POSIX; extended
globbing is also a bash extension, and therefore right in line with your
intent of being bash-only.  However, if you want to scrub things so that
you can be portable to POSIX sh, I think the most concise you can get is:

cmd () {
  case $1 in
    *.tif | *.tiff) echo "${1%%.tif*}" ;;
    *) echo "$1" ;;
  esac
}

-- 
Eric Blake   address@hidden    +1-919-301-3266
Libvirt virtualization library http://libvirt.org

Attachment: signature.asc
Description: OpenPGP digital signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]