From owner-freebsd-questions@FreeBSD.ORG Tue Jul 13 00:59:58 2010 Return-Path: Delivered-To: questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 761581065674 for ; Tue, 13 Jul 2010 00:59:58 +0000 (UTC) (envelope-from aiza21@comclark.com) Received: from avmxsmtp3.comclark.com (avmxsmtp3.comclark.com [202.69.191.117]) by mx1.freebsd.org (Postfix) with ESMTP id 109F98FC1E for ; Tue, 13 Jul 2010 00:59:57 +0000 (UTC) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AksXAD1XO0zKRa3YOWdsb2JhbAAHh2iYVQEBAQE0AcAChScEg3mHDQ X-IronPort-AV: E=Sophos;i="4.55,191,1278259200"; d="scan'208";a="11002575" Received: from unknown (HELO [10.0.10.3]) ([202.69.173.216]) by avmxsmtp3.comclark.com with ESMTP; 13 Jul 2010 08:59:56 +0800 Message-ID: <4C3BBA8A.3020305@comclark.com> Date: Tue, 13 Jul 2010 08:59:54 +0800 From: Aiza User-Agent: Thunderbird 2.0.0.17 (Windows/20080914) MIME-Version: 1.0 To: Anonymous References: <4C3B26B4.1000208@comclark.com> <867hl0ems5.fsf@gmail.com> In-Reply-To: <867hl0ems5.fsf@gmail.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Cc: "questions@freebsd.org" Subject: Re: .sh check for sufix g or m on size field X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 13 Jul 2010 00:59:58 -0000 Anonymous wrote: > Aiza writes: > >> Have a .sh script that accepts an -s sparse file size. >> Only 2 suffix's are valid m and g. >> >> Been trying to get this line of code to strip out just the single >> letter. But it strips the letter and every thing to the right of it. >> >> Timagesize=`echo-n "${imagesize}" | sed 's/g.*$//'` > > You didn't state what's your input. I guess smth like following will do > > strip() { > local size= > if printf >&- 2>&- %g ${size:=${1%[gm]}}; then > echo "it's a \`$size' without suffix" > else > echo "$1 has invalid suffix" > fi > } > > $ strip 17m > it's a `17' without suffix > $ strip 33g > it's a `33' without suffix > $ strip 25gm > 25gm has invalid suffix > This is real close but it allows a numeric value through as valid which is not a valid condition. The $size value has to be suffixed with g or m to be valid. A numeric value only or a numeric value suffixed with anything else than m or g is invalid.