Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 12 Jul 2010 19:35:06 +0400
From:      Anonymous <swell.k@gmail.com>
To:        Aiza <aiza21@comclark.com>
Cc:        "questions@freebsd.org" <questions@freebsd.org>
Subject:   Re: .sh  check for sufix g or m on size field
Message-ID:  <867hl0ems5.fsf@gmail.com>
In-Reply-To: <4C3B26B4.1000208@comclark.com> (Aiza's message of "Mon, 12 Jul 2010 22:29:08 %2B0800")
References:  <4C3B26B4.1000208@comclark.com>

next in thread | previous in thread | raw e-mail | index | archive | help
Aiza <aiza21@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

>
> I plan to strip just the m or g if its there and the result should be
> numeric. If not numeric know invalid suffix.
>
> Need help with the sed syntax. Or if there is better way I want to
> learn it.



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