Q: Why do programmers always get Christmas and Halloween mixed up?
William Park
opengeometry-FFYn/CNdgSA at public.gmane.org
Wed Nov 3 18:17:41 UTC 2010
----- Original Message ----
> From: Lennart Sorensen <lsorense-1wCw9BSqJbv44Nm34jS7GywD8/FfD2ys at public.gmane.org>
> To: tlug-lxSQFCZeNF4 at public.gmane.org
> Sent: Wed, November 3, 2010 10:55:41 AM
> Subject: Re: [TLUG]: Q: Why do programmers always get Christmas and Halloween
>mixed up?
>
> On Tue, Nov 02, 2010 at 08:36:20PM -0400, Walter Dnes wrote:
> > I have one or 2 personal bash scripts that I have to go out of my way
> > to *PREVENT* octal. They parse/manipulate/calculate numbers that get
> > pulled in from a fixed format text file. And leading zeros royally
> > screw things up, because they switch the numbers from base 10 to base 8.
> > Here's a function I use a lot...
> >
> > # bash treats leading zeros as indicating octal, freaks out on
> > # on 08 or 09, and numbers 010 and above are just plain wrong.
> > # So strip leading zeros.
> > strip_leading_0() {
> > stripped="$1"
> > #
> > # Leave plain "0" alone. Only strip zeros if string is longer
> > # than one character.
> > while [[ ${#stripped} -gt 1 ]] && [[ "${stripped:0:1}" == "0" ]]
> > do
> > stripped=${stripped:1}
> > done
> > export stripped
> > }
> >
> > It gets called like so...
> >
> > xday=${dataline:6:2}
> > strip_leading_0 "${xday}"
> > xday=${stripped}
>
> Would it be simpler to just prepend 10# to the number?
>
> For example:
>
> y="0001234"
> let "x = 10#$y"
> echo $x
Or, if you have recent Bash or Ksh,
shopt -s extglob
echo ${x##+(0)}
--
William
--
The Toronto Linux Users Group. Meetings: http://gtalug.org/
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://gtalug.org/wiki/Mailing_lists
More information about the Legacy
mailing list