Q: Why do programmers always get Christmas and Halloween mixed up?
Lennart Sorensen
lsorense-1wCw9BSqJbv44Nm34jS7GywD8/FfD2ys at public.gmane.org
Wed Nov 3 14:55:41 UTC 2010
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
--
Len Sorensen
--
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