On Sun, May 4, 2014 at 4:32 PM, ~Stack~ <[email protected]> wrote:

> Greetings,
>
> I know this isn't explicitly a Debian problem, but I have seen many
> really smart Bash programmers on this list so I figured this was a good
> place to ask. :-)
>
> $ bash --version
> GNU bash, version 4.2.37(1)-release (x86_64-pc-linux-gnu
> $ cat /etc/debian_version
> 7.5
>
> I have a Bash script I am working on where I am getting (supposedly) a
> two-digit number from the user. I handle all the logic to ensure I am
> getting two digits and not >2, letters, characters, ect in a different
> function. By the time I get to this code in my script, I am assuming it
> is a one or two digit number. Because I need a two digit number I am
> assuming that my user may have just input a single digit for the first
> nine numbers without a leading zero. I am doing this in my code like:
>
> verifiednum=`printf %02d $uservar`
>
> This works really well when they enter only a single digit or 01-07.
> However, on 08 or 09, this fails.
>
> $ cat test.sh
> #!/bin/bash -
> printf %02d 07 #This Works.
> echo ""
> printf %02d 8  #This Works.
> echo ""
> printf %02d 08 #This doesn't.
> echo ""
> printf %02d 9  #This Works.
> echo ""
> printf %02d 09 #This doesn't.
> echo ""
>
> $ ./test.sh
> 07
> 08
> ./test.sh: line 6: printf: 08: invalid octal number
> 00
> 09
> ./test.sh: line 10: printf: 09: invalid octal number
> 00
>
> I almost want to think that this is a bug, but because it seems to be
> thinking it is an octal number (which technically it is I suppose :). I
> am guessing that it just doesn't like what I am doing with printf but I
> am a bit baffled as to why it only croaks on 08 and 09.
>
> 1) Does anyone know what is wrong here?
>

leading zero is interpreted as octal. You need to prepend the number
with 10# to convert it to decimal first


>
> 2) Is there a better way of solving this issue?
>
>
so try this instead  printf %02d $(( 10#08 ))

Thanks!
> ~Stack~
>
>


-- 
Asif Iqbal
PGP Key: 0xE62693C5 KeyServer: pgp.mit.edu
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?

Reply via email to