Q: Why do programmers always get Christmas and Halloween mixed up?

Walter Dnes waltdnes-SLHPyeZ9y/tg9hUCZPvPmw at public.gmane.org
Wed Nov 3 00:36:20 UTC 2010


On Mon, Nov 01, 2010 at 12:09:02PM -0400, Lennart Sorensen wrote

> I think your joke is a couple od decades out of date.  Who uses
> octal anymore?

  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}


-- 
Walter Dnes <waltdnes-SLHPyeZ9y/tg9hUCZPvPmw at public.gmane.org>
--
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