.sh check for sufix g or m on size field

Aiza aiza21 at comclark.com
Tue Jul 13 00:59:58 UTC 2010

Anonymous wrote:
> Aiza <aiza21 at comclark.com> 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.

More information about the freebsd-questions mailing list