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

Chris F.A. Johnson chris-E7bvbYbpR6jSUeElwK9/Pw at public.gmane.org
Wed Nov 3 03:36:35 UTC 2010


On Tue, 2 Nov 2010, Walter Dnes wrote:

> 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}

$ a=08
$ b=09
$ echo $(( 10#$a + 10#$b ))
17

    Or, if there can only be a single leading zero (as often happens
    when manipulating dates):

echo $(( ${a#0} + ${b#0} ))

-- 
    Chris F.A. Johnson, <http://cfajohnson.com>
    Author:
    Pro Bash Programming: Scripting the GNU/Linux Shell (2009, Apress)
    Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
--
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