Re: : Re: : Re: : bash script issue
Bill Thanis <[email protected]> Wed, 3 Sep 2014 19:18:25 -0400
| Newsgroups | gmane.org.user-groups.linux.tolug |
|---|---|
| Message-ID | <CAA3RLT6Ja3VKWnizeWWpwsrH0EvNLpe9zNnQM-6EhicdSsv+dw@mail.gmail.com> |
--20cf30363911f3a6a605023173fc Content-Type: text/plain; charset=UTF-8 echo $((078+1)) -> bash: 078: value too great for base (error token is "078") Octal has digits 0-7 8 is not a number in octal. Bill On Wed, Sep 3, 2014 at 6:55 PM, Walter Dnes <waltdnes-SLHPyeZ9y/[email protected]> wrote: > On Wed, Sep 03, 2014 at 03:19:49PM +0000, Peter wrote > > D. Hugh Redelmeier <hugh@...> writes: > > > > > I don't remember an ordinary shell context where a pathname can be > > > confused with number. Can you give an example? > > > > Ordinary not, but: > > > > echo $((07+1)) -> 8 (yes... why?) > > echo $((007+1)) -> 8 (hmm, must be Mr. Bond) > > echo $((077+1)) -> 64 (no kidding, 077oct is 64dec) > > echo $((078+1)) -> bash: 078: value too great for base (error token is > "078") > > > > So if you do any math on any numbers for example numbered files like > > DCIM/IMG0077.jpg you WILL likely be bitten. I think the 1st application > > where I was bitten was many years ago trying to figure out missing and > > duplicate IMGnnnnn.jpg files in a large recovered image directory. I also > > tried to be clever and rename files such that there would be no gaps upon > > finding missing files, in one pass. That did not go well, I had a 10% or > so > > file loss when done. There are many other 'opportunities' to get bitten. > > I use bash in many cases where other people would use "R" or whatever. > Leading zeros in numbers come up so often, I've written my own function > to handle the situation. You can include it at the top of a program > like so... > > #!/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} > > ...or you can set up and load a common function library as shown at > http://bash.cyberciti.biz/guide/Shell_functions_library > > -- > Walter Dnes <waltdnes-SLHPyeZ9y/[email protected]> > I don't run "desktop environments"; I run useful applications > -- > 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 > --20cf30363911f3a6a605023173fc Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable <div dir=3D"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=3D"= gmail_extra"> <br><br><div class=3D"gmail_quote">On Wed, Sep 3, 2014 at 6:55 PM, Walter D= nes <span dir=3D"ltr"><<a href=3D"mailto:waltdnes-SLHPyeZ9y/[email protected]" target= =3D"_blank">waltdnes-SLHPyeZ9y/[email protected]</a>></span> wrote:<br><blockquote clas= s=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;pad= ding-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 c= an be<br> > > confused with number.=C2=A0 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<b= r> > DCIM/IMG0077.jpg you WILL likely be bitten. I think the 1st applicatio= n<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 a= lso<br> > tried to be clever and rename files such that there would be no gaps u= pon<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 g= et bitten.<br> <br> =C2=A0 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.=C2=A0 You can include it at the top of a program<b= r> 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.=C2=A0 So strip leading ze= ros.<br> strip_leading_0() {<br> =C2=A0 =C2=A0stripped=3D"${1}"<br> #<br> # Leave plain "0" alone.=C2=A0 Only strip zeros if string is long= er<br> # than one character.<br> =C2=A0 =C2=A0while [ ${#stripped} -gt 1 ] && [ "${stripped:0:1= }" =3D=3D "0" ]<br> =C2=A0 =C2=A0do<br> =C2=A0 =C2=A0 =C2=A0 stripped=3D${stripped:1}<br> =C2=A0 =C2=A0done<br> =C2=A0 =C2=A0export stripped<br> }<br> ...<br> #<br> # "Sanitize" numeric variable foobar<br> strip_leading_0 ${foobar}<br> foobar=3D${stripped}<br> <br> ...or you can set up and load a common function library as shown at<br> <a href=3D"http://bash.cyberciti.biz/guide/Shell_functions_library" target= =3D"_blank">http://bash.cyberciti.biz/guide/Shell_functions_library</a><br> <span class=3D"HOEnZb"><font color=3D"#888888"><br> --<br> Walter Dnes <<a href=3D"mailto:waltdnes-SLHPyeZ9y/[email protected]">waltdnes@waltdnes.= org</a>><br> I don't run "desktop environments"; I run useful applications= <br> --<br> The Toronto Linux Users Group.=C2=A0 =C2=A0 =C2=A0 Meetings: <a href=3D"htt= p://gtalug.org/" target=3D"_blank">http://gtalug.org/</a><br> TLUG requests: Linux topics, No HTML, wrap text below 80 columns<br> How to UNSUBSCRIBE: <a href=3D"http://gtalug.org/wiki/Mailing_lists" target= =3D"_blank">http://gtalug.org/wiki/Mailing_lists</a><br> </font></span></blockquote></div><br></div> --20cf30363911f3a6a605023173fc-- -- 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