bash script issue

Chris F.A. Johnson chris-E7bvbYbpR6jSUeElwK9/Pw at public.gmane.org
Thu Sep 4 00:07:55 UTC 2014


On Wed, 3 Sep 2014, Walter Dnes wrote:
...
> #!/bin/bash
> #
> # bash treats leading zeros as indicating octal, freaks out 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
> }
> ...
> #
> # "Sanitize" numeric variable foobar
> strip_leading_0 ${foobar}
> foobar=${stripped}

$ q=0056789
$ echo "$((10#$q+1))"
56790

   Or, if you want leading zeroes in the output:

printf '%08d\n' "$((10#$q+1))"

-- 
Chris F.A. Johnson, <http://cfajohnson.com>
--
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