<div dir="ltr"><div><div>echo $((078+1)) -> bash: 078: value too great for base (error token is "078")<br><br></div>Octal has digits 0-7 8 is not a number in octal.<br><br></div>Bill<br><br></div><div class="gmail_extra">
<br><br><div class="gmail_quote">On Wed, Sep 3, 2014 at 6:55 PM, Walter Dnes <span dir="ltr"><<a href="mailto:waltdnes-SLHPyeZ9y/tg9hUCZPvPmw@public.gmane.org" target="_blank">waltdnes-SLHPyeZ9y/tg9hUCZPvPmw@public.gmane.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
On Wed, Sep 03, 2014 at 03:19:49PM +0000, Peter wrote<br>
> D. Hugh Redelmeier <hugh@...> writes:<br>
><br>
> > I don't remember an ordinary shell context where a pathname can be<br>
> > confused with number. Can you give an example?<br>
><br>
> Ordinary not, but:<br>
><br>
> echo $((07+1)) -> 8 (yes... why?)<br>
> echo $((007+1)) -> 8 (hmm, must be Mr. Bond)<br>
> echo $((077+1)) -> 64 (no kidding, 077oct is 64dec)<br>
> echo $((078+1)) -> bash: 078: value too great for base (error token is "078")<br>
><br>
> So if you do any math on any numbers for example numbered files like<br>
> DCIM/IMG0077.jpg you WILL likely be bitten. I think the 1st application<br>
> where I was bitten was many years ago trying to figure out missing and<br>
> duplicate IMGnnnnn.jpg files in a large recovered image directory. I also<br>
> tried to be clever and rename files such that there would be no gaps upon<br>
> finding missing files, in one pass. That did not go well, I had a 10% or so<br>
> file loss when done. There are many other 'opportunities' to get bitten.<br>
<br>
I use bash in many cases where other people would use "R" or whatever.<br>
Leading zeros in numbers come up so often, I've written my own function<br>
to handle the situation. You can include it at the top of a program<br>
like so...<br>
<br>
#!/bin/bash<br>
#<br>
# bash treats leading zeros as indicating octal, freaks out on 08 or 09,<br>
# and numbers 010 and above are just plain wrong. So strip leading zeros.<br>
strip_leading_0() {<br>
stripped="${1}"<br>
#<br>
# Leave plain "0" alone. Only strip zeros if string is longer<br>
# than one character.<br>
while [ ${#stripped} -gt 1 ] && [ "${stripped:0:1}" == "0" ]<br>
do<br>
stripped=${stripped:1}<br>
done<br>
export stripped<br>
}<br>
...<br>
#<br>
# "Sanitize" numeric variable foobar<br>
strip_leading_0 ${foobar}<br>
foobar=${stripped}<br>
<br>
...or you can set up and load a common function library as shown at<br>
<a href="http://bash.cyberciti.biz/guide/Shell_functions_library" target="_blank">http://bash.cyberciti.biz/guide/Shell_functions_library</a><br>
<span class="HOEnZb"><font color="#888888"><br>
--<br>
Walter Dnes <<a href="mailto:waltdnes-SLHPyeZ9y/tg9hUCZPvPmw@public.gmane.org">waltdnes@waltdnes.org</a>><br>
I don't run "desktop environments"; I run useful applications<br>
--<br>
The Toronto Linux Users Group. Meetings: <a href="http://gtalug.org/" target="_blank">http://gtalug.org/</a><br>
TLUG requests: Linux topics, No HTML, wrap text below 80 columns<br>
How to UNSUBSCRIBE: <a href="http://gtalug.org/wiki/Mailing_lists" target="_blank">http://gtalug.org/wiki/Mailing_lists</a><br>
</font></span></blockquote></div><br></div>