Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 8 Jan 2008 09:56:15 -0800
From:      Christopher Cowart <ccowart@rescomp.berkeley.edu>
To:        questions@freebsd.org
Subject:   Re: is there a /bin/sh method to tell the ending of a file
Message-ID:  <20080108175615.GP96147@hal.rescomp.berkeley.edu>
In-Reply-To: <20080108060118.GF1965@woodstock.nethamilton.net>
References:  <20080108051058.GA19698@thought.org> <20080108053408.GA95218@procacci.kicks-ass.org> <20080108060118.GF1965@woodstock.nethamilton.net>

next in thread | previous in thread | raw e-mail | index | archive | help

--G+DT6X5ssgZ56VG3
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

On Tue, Jan 08, 2008 at 12:01:18AM -0600, Jon Hamilton wrote:
> } On Mon, Jan 07, 2008 at 09:10:58PM -0800, Gary Kline wrote:
> } Paul Procacci <pprocacci@datapipe.com>, said on Mon Jan 07, 2008 [11:34=
:08 PM]:
> } > Hi All,
> } >=20
> } > Is there an easy way of determing whether a string//filename ends in
> } > *.gz? using /bin/sh? =20
>=20
> } Is this what you mean?
> }=20
> } ---------------------
> } #!/bin/sh
> }=20
> } STRING=3D"mystring.gz"
> }=20
> } if [ ".gz" =3D "`echo \"$STRING\" | sed -n 's/.*\(\.gz\)$/\1/p'`" ]; th=
en
> }  echo test;
> } fi
> }=20
> } -----------------------
>=20
> Works (I assume) but perhaps easier to read and more "native" might be:
>=20
> case "$STRING" in
> *\.gz)
>   echo "Found .gz suffix"
>   ;;
> *)
>   echo "Not a .gz suffix"
>   ;;
> esac
>=20
> Sh is a pretty versatile creature; I'm sure there are a thousand more ways
> all of which work, and some of which will cause religious arguments for=
=20
> decades :)

Right. Here's another way using parameter expansion:

| if [ ${STRING##*.} =3D gz ] ; then
|     echo true
| else
|     echo false
| fi

The syntax is terse, but pretty popular in the /etc/rc family of scripts.=
=20

--=20
Chris Cowart
Lead Systems Administrator
Network & Infrastructure Services, RSSP-IT
UC Berkeley

--G+DT6X5ssgZ56VG3
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.4 (FreeBSD)

iQIVAwUBR4O5PyPHEDszU3zYAQJcIhAAv6OwV6m8vdD4aqqcR6mIMU+aseJmcV+q
3UvRR5306Xz0lzR9M/I4Tydkt1VzrkbTz6SkDsW55GJ0nWFhI/TrWbUR6BCPgErw
t703I7Fq6d7uTFSMTKwOrPayoZ7MPTANpnXt1iWK8atlJGWxoZuDJxwg2Kznnd3Y
JDj+N9OqB5ODErepWPq+DEmHbY6JPOgx9CFsh4UBB8IJUOIy99f+KgzYUyRr796J
CQZDD1xcZ1yTo5//1CWeG6dLlwj0a7nrgCBJekjGs1fcrdblLKy61XaSXKyDCPCp
6l+oL5jSxychMkmmL0E3uq2aB41tZZuKZcaLIFBX/dHnPWXQpxldoo0l2WoSPqPZ
aNUvGXsFVHqMWtYPpA+6DZGKUPNQu6Z79EOe7lcA51K6JqrIGix48sfiX+Pit2kz
CI595cnXKS/NUprXTlkGSVQYOIvaLkb5zT6OpRVCuFFYVU/APtPMMLeSmy6Ebo9p
od1fvO13HSsz6MWnBPKcG18IqGB+PXAo0OLexl7WA+GDZHlSwyy4BdezcywR0tXY
u+WkWKJRw0dOZAxHnFvjLHFoZ91+3rT2AmMRLNfFRTjFYYYZjBoiETtUEzKzdAAi
uFAE0Mm4ddDwevmYqPK/t2Svp852OyJmWHimqipMm0KyyudfrcWNXzdV86TpuWp8
jfD9GgDnh+s=
=C1xl
-----END PGP SIGNATURE-----

--G+DT6X5ssgZ56VG3--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20080108175615.GP96147>