Convert 08 to decimal 8
am 07.09.2007 16:10:00 von SiKing
Hi all,
I am running this under bash 3.1.
I have two variables that each contain a number, which I need to compare for
equality. Unfortunately, sometimes, the number also contains leading zeros - I
need 8 to be equal to 08, for example.
I tried fixing it with 'let var+=0', but (quoted from the manual):
Constants with a leading 0 are interpreted as octal numbers. Otherwise, numbers
take the form [base#]n, where base is a decimal number between 2 and 64
representing the arithmetic base, and n is a number in that base. If base# is
omitted, then base 10 is used.
I can't seem to able to get this syntax to work. All of the following give me
syntax errors.
a=08
echo $(( $a + 0 ))
echo $(( []$a + 0 ))
echo $(( [10]$a + 0 ))
echo $(( [10#]$a + 0 ))
What is the secret formula?
Thank You.
Re: Convert 08 to decimal 8
am 07.09.2007 16:21:23 von Miles
On Sep 7, 9:10 am, SiKing wrote:
> Hi all,
>
> I am running this under bash 3.1.
>
> I have two variables that each contain a number, which I need to compare for
> equality. Unfortunately, sometimes, the number also contains leading zeros - I
> need 8 to be equal to 08, for example.
> I tried fixing it with 'let var+=0', but (quoted from the manual):
> Constants with a leading 0 are interpreted as octal numbers. Otherwise, numbers
> take the form [base#]n, where base is a decimal number between 2 and 64
> representing the arithmetic base, and n is a number in that base. If base# is
> omitted, then base 10 is used.
>
> I can't seem to able to get this syntax to work. All of the following give me
> syntax errors.
> a=08
> echo $(( $a + 0 ))
> echo $(( []$a + 0 ))
> echo $(( [10]$a + 0 ))
> echo $(( [10#]$a + 0 ))
>
> What is the secret formula?
> Thank You.
Could you not just strip the leading zero? This is really crude, but:
>A=08
>echo $A | sed "s/^0//"
8
>A=8
>echo $A | sed "s/^0//"
8
Just run all the variables through: A=$(echo $A | sed "s/^0//").
Tested on ksh.
Re: Convert 08 to decimal 8
am 07.09.2007 16:35:15 von SiKing
Miles wrote:
> On Sep 7, 9:10 am, SiKing wrote:
>> Hi all,
>>
>> I am running this under bash 3.1.
>>
>> I have two variables that each contain a number, which I need to compare for
>> equality. Unfortunately, sometimes, the number also contains leading zeros - I
>> need 8 to be equal to 08, for example.
>> I tried fixing it with 'let var+=0', but (quoted from the manual):
>> Constants with a leading 0 are interpreted as octal numbers. Otherwise, numbers
>> take the form [base#]n, where base is a decimal number between 2 and 64
>> representing the arithmetic base, and n is a number in that base. If base# is
>> omitted, then base 10 is used.
>>
>> I can't seem to able to get this syntax to work. All of the following give me
>> syntax errors.
>> a=08
>> echo $(( $a + 0 ))
>> echo $(( []$a + 0 ))
>> echo $(( [10]$a + 0 ))
>> echo $(( [10#]$a + 0 ))
>>
>> What is the secret formula?
>> Thank You.
>
> Could you not just strip the leading zero? This is really crude, but:
>> A=08
>> echo $A | sed "s/^0//"
> 8
>> A=8
>> echo $A | sed "s/^0//"
> 8
>
> Just run all the variables through: A=$(echo $A | sed "s/^0//").
> Tested on ksh.
Hmmm, I am going to have to use "s/^0*//", because I have leading zeros
(zeroes?) - plural. But can bash not do this on its own?
Thanx.
Re: Convert 08 to decimal 8
am 07.09.2007 16:51:12 von Stephane CHAZELAS
2007-09-07, 15:35(+01), SiKing:
[...]
> Hmmm, I am going to have to use "s/^0*//", because I have leading zeros
> (zeroes?) - plural.
[...]
That will turn 0 or 000 into the empty string.
> But can bash not do this on its own?
[...]
bash, non standard:
a=$((10#$a))
standard:
case a in (0*[!0]*) a=${a#${a%%[!0]*}};; esac
--
Stéphane
Re: Convert 08 to decimal 8
am 07.09.2007 17:03:21 von SiKing
Stephane CHAZELAS wrote:
> 2007-09-07, 15:35(+01), SiKing:
> [...]
>> Hmmm, I am going to have to use "s/^0*//", because I have leading zeros
>> (zeroes?) - plural.
> [...]
>
> That will turn 0 or 000 into the empty string.
Right, I just figured that out too. :(
>> But can bash not do this on its own?
> [...]
>
> bash, non standard:
What does that mean?
> a=$((10#$a))
So that(!) is the secret syntax...
> standard:
>
> case a in (0*[!0]*) a=${a#${a%%[!0]*}};; esac
Whoa! That took me like 10 minutes to parse in my head.
Thank You!
Re: Convert 08 to decimal 8
am 07.09.2007 17:28:50 von Stephane CHAZELAS
2007-09-07, 16:03(+01), SiKing:
> Stephane CHAZELAS wrote:
>> 2007-09-07, 15:35(+01), SiKing:
>> [...]
>>> Hmmm, I am going to have to use "s/^0*//", because I have leading zeros
>>> (zeroes?) - plural.
>> [...]
>>
>> That will turn 0 or 000 into the empty string.
>
> Right, I just figured that out too. :(
>
>>> But can bash not do this on its own?
>> [...]
>>
>> bash, non standard:
>
> What does that mean?
>
>> a=$((10#$a))
Just that it will work with bash (and ksh and zsh) but is not a
standard sh feature. Generally, you write POSIX sh scripts as
the POSIX sh syntax is generally far enough to write scripts.
This way, you know that your script can be interpreted on any
system because all Unix systems have a POSIX conformant sh,
wether it's bash or any other shell.
> So that(!) is the secret syntax...
>
>> standard:
>>
>> case a in (0*[!0]*) a=${a#${a%%[!0]*}};; esac
>
> Whoa! That took me like 10 minutes to parse in my head.
[...]
Yeah, pretty awful.
a=$(expr "0$a" : '0*\(..*\)')
is more readable but CFAJ will argue that it's not as fast in
most shells :)
Note that expr is meant to work in decimal, so
a=$(expr 0 + "$a")
should work as well.
--
Stéphane
Re: Convert 08 to decimal 8
am 07.09.2007 19:37:14 von Neil Cherry
On Fri, 07 Sep 2007 15:10:00 +0100, SiKing wrote:
> I can't seem to able to get this syntax to work. All of the
> following give me syntax errors.
> a=08
Other have give you good advice but let me add a comment on '08';
bash thinks it's octal (09 will have the same problem). Since the
is no 08 in octal (that would be 010) it ends up being an error
when you perform any math on it. I got around this in this fashion:
M=`date +"%m"`
# Interesting problem, what is 08? According to shell it doesn't exists it
# thinks the number is octal!
case "x${M}" in
"x08")
Mon=8
;;
"x09")
Mon=9
;;
*)
Mon=${M}
;;
esac
I hope that's of some use to you.
--
Linux Home Automation Neil Cherry ncherry@linuxha.com
http://www.linuxha.com/ Main site
http://linuxha.blogspot.com/ My HA Blog
Author of: Linux Smart Homes For Dummies
Re: Convert 08 to decimal 8
am 07.09.2007 19:52:44 von Icarus Sparry
On Fri, 07 Sep 2007 12:37:14 -0500, Neil Cherry wrote:
> On Fri, 07 Sep 2007 15:10:00 +0100, SiKing wrote:
>
>> I can't seem to able to get this syntax to work. All of the following
>> give me syntax errors.
>> a=08
>
> Other have give you good advice but let me add a comment on '08'; bash
> thinks it's octal (09 will have the same problem). Since the is no 08 in
> octal (that would be 010) it ends up being an error when you perform any
> math on it. I got around this in this fashion:
>
> M=`date +"%m"`
>
> # Interesting problem, what is 08? According to shell it doesn't exists
> it # thinks the number is octal!
>
> case "x${M}" in
> "x08")
> Mon=8
> ;;
> "x09")
> Mon=9
> ;;
> *)
> Mon=${M}
> ;;
> esac
>
> I hope that's of some use to you.
Whilst this is good advice, it is a little long winded. To start with you
do not ever need to put a protection character like the "x" here in a
case statement.
Since months can never be numbered zero, you can safely remove any
leading zero, and get
Mon=${M#0}
If you are in a situation where you might have a value that is zero, you
can do several other things.
Mon=$M
case "$Mon" in
0[89]) Mon=${Mon#0} ;; # You could use 0[1-9] as a pattern.
esac
Or
Mon=${M#0} # Strip off leading zero
Mon=${M:-0} # if left with the empty string, make it zero.
Re: Convert 08 to decimal 8
am 08.09.2007 01:12:13 von Neil Cherry
On 07 Sep 2007 17:52:44 GMT, Icarus Sparry wrote:
> On Fri, 07 Sep 2007 12:37:14 -0500, Neil Cherry wrote:
>
>> On Fri, 07 Sep 2007 15:10:00 +0100, SiKing wrote:
>>
>>> I can't seem to able to get this syntax to work. All of the following
>>> give me syntax errors.
>>> a=08
>>
>> Other have give you good advice but let me add a comment on '08'; bash
>> thinks it's octal (09 will have the same problem). Since the is no 08 in
>> octal (that would be 010) it ends up being an error when you perform any
>> math on it. I got around this in this fashion:
>>
>> M=`date +"%m"`
>>
>> # Interesting problem, what is 08? According to shell it doesn't exists
>> it # thinks the number is octal!
>>
>> case "x${M}" in
>> "x08")
>> Mon=8
>> ;;
>> "x09")
>> Mon=9
>> ;;
>> *)
>> Mon=${M}
>> ;;
>> esac
>>
>> I hope that's of some use to you.
>
> Whilst this is good advice, it is a little long winded. To start with you
> do not ever need to put a protection character like the "x" here in a
> case statement.
Sorry the x is there from a long time habit of appending the letter x
in front of a variable (I think it was to avoid an empty
variable). Bad habits never die.
> Since months can never be numbered zero, you can safely remove any
> leading zero, and get
>
> Mon=${M#0}
>
> If you are in a situation where you might have a value that is zero, you
> can do several other things.
>
> Mon=$M
> case "$Mon" in
> 0[89]) Mon=${Mon#0} ;; # You could use 0[1-9] as a pattern.
> esac
>
> Or
> Mon=${M#0} # Strip off leading zero
> Mon=${M:-0} # if left with the empty string, make it zero.
Thanks, I'll save this one for later. :-)
--
Linux Home Automation Neil Cherry ncherry@linuxha.com
http://www.linuxha.com/ Main site
http://linuxha.blogspot.com/ My HA Blog
Author of: Linux Smart Homes For Dummies
Re: Convert 08 to decimal 8
am 08.09.2007 09:14:34 von Cyrus Kriticos
SiKing wrote:
> Miles wrote:
>> On Sep 7, 9:10 am, SiKing wrote:
>>>
>>> I am running this under bash 3.1.
>>>
>>> I have two variables that each contain a number, which I need to
>>> compare for
>>> equality. Unfortunately, sometimes, the number also contains leading
>>> zeros - I
>>> need 8 to be equal to 08, for example.
>
> But can bash not do this on its own?
yes.
$ a=000008
$ echo ${a##*0}
8
--
Best regards | "The only way to really learn scripting is to write
Cyrus | scripts." -- Advanced Bash-Scripting Guide
Re: Convert 08 to decimal 8
am 08.09.2007 10:28:18 von Cyrus Kriticos
SiKing wrote:
> Miles wrote:
>> On Sep 7, 9:10 am, SiKing wrote:
>>>
>>> I am running this under bash 3.1.
>>>
>>> I have two variables that each contain a number, which I need to
>>> compare for
>>> equality. Unfortunately, sometimes, the number also contains leading
>>> zeros - I
>>> need 8 to be equal to 08, for example.
>
> But can bash not do this on its own?
yes.
$ a=000008
$ echo ${a##*0}
8
but
$ a=000008008
$ echo ${a##*0}
8
--
Grüße | stuttmann-karikaturen.de/karikaturen/kari_20070831_Intimshae re.gif
Cyrus |
Re: Convert 08 to decimal 8
am 08.09.2007 10:32:46 von Stephane CHAZELAS
2007-09-08, 10:28(+02), Cyrus Kriticos:
>
> SiKing wrote:
>> Miles wrote:
>>> On Sep 7, 9:10 am, SiKing wrote:
>>>>
>>>> I am running this under bash 3.1.
>>>>
>>>> I have two variables that each contain a number, which I need to
>>>> compare for
>>>> equality. Unfortunately, sometimes, the number also contains leading
>>>> zeros - I
>>>> need 8 to be equal to 08, for example.
>>
>> But can bash not do this on its own?
>
> yes.
>
> $ a=000008
> $ echo ${a##*0}
> 8
>
> but
>
> $ a=000008008
> $ echo ${a##*0}
> 8
hence
a=${a#"${a%%[!0]*}"}
but that turns 0 or 000 into an empty string, hence:
case $a in (0*[!0]*)
a=${a#"${a%%[!0]*}"};;
esac
--
Stéphane
Re: Convert 08 to decimal 8
am 08.09.2007 10:41:13 von Cyrus Kriticos
Stephane CHAZELAS wrote:
>
> a=${a#"${a%%[!0]*}"}
>
> but that turns 0 or 000 into an empty string, hence:
let a=a+0
--
Best regards | "The only way to really learn scripting is to write
Cyrus | scripts." -- Advanced Bash-Scripting Guide
Re: Convert 08 to decimal 8
am 08.09.2007 11:14:43 von Stephane CHAZELAS
2007-09-08, 10:41(+02), Cyrus Kriticos:
> Stephane CHAZELAS wrote:
>>
>> a=${a#"${a%%[!0]*}"}
>>
>> but that turns 0 or 000 into an empty string, hence:
>
> let a=a+0
The reason in the first place for why the OP needs that is
because he can't do shell arithmetics on those numbers because
bash considers 08 as an invalid octal number.
So
let a=a+0
will not work with bash (and is not a standard Unix shell
syntax) or mksh or zsh in sh emulation.
It will work with AT&T ksh because those implementations of ksh
consider 08 as a decimal number.
It will work with PD ksh and zsh (when not in sh emulation)
because those shells don't consider numbers starting with 0 as
octal numbers.
Some PD ksh derivatives like mksh (MirBSD ksh) or posh do
consider 011 as the octal represention of 9. They both consider
08 as an invalid number. posh doesn't have the "let" builtin
because posh aims at being a shell to test POSIX compliance of
scripts, so removed most of the pdksh features that were not
specified by POSIX.
--
Stéphane
Re: Convert 08 to decimal 8
am 08.09.2007 11:53:50 von Cyrus Kriticos
Stephane CHAZELAS wrote:
> 2007-09-08, 10:41(+02), Cyrus Kriticos:
>> Stephane CHAZELAS wrote:
>>> a=${a#"${a%%[!0]*}"}
>>>
>>> but that turns 0 or 000 into an empty string, hence:
>> let a=a+0
>
> The reason in the first place for why the OP needs that is
> because he can't do shell arithmetics on those numbers because
> bash considers 08 as an invalid octal number.
>
> So
>
> let a=a+0
>
> will not work with bash
$ bash --version | head -n 1
GNU bash, version 3.2.13(1)-release (i486-pc-linux-gnu)
$ a=08
$ a=${a#"${a%%[!0]*}"}
$ let a=a+0
$ echo $a
8
$ a=00000
$ a=${a#"${a%%[!0]*}"}
$ let a=a+0
$ echo $a
0
--
Best regards | "The only way to really learn scripting is to write
Cyrus | scripts." -- Advanced Bash-Scripting Guide
Re: Convert 08 to decimal 8
am 08.09.2007 12:20:37 von Stephane CHAZELAS
2007-09-08, 11:53(+02), Cyrus Kriticos:
> Stephane CHAZELAS wrote:
>> 2007-09-08, 10:41(+02), Cyrus Kriticos:
>>> Stephane CHAZELAS wrote:
>>>> a=${a#"${a%%[!0]*}"}
>>>>
>>>> but that turns 0 or 000 into an empty string, hence:
>>> let a=a+0
>>
>> The reason in the first place for why the OP needs that is
>> because he can't do shell arithmetics on those numbers because
>> bash considers 08 as an invalid octal number.
>>
>> So
>>
>> let a=a+0
>>
>> will not work with bash
>
> $ bash --version | head -n 1
> GNU bash, version 3.2.13(1)-release (i486-pc-linux-gnu)
>
> $ a=08
> $ a=${a#"${a%%[!0]*}"}
> $ let a=a+0
> $ echo $a
> 8
>
> $ a=00000
> $ a=${a#"${a%%[!0]*}"}
> $ let a=a+0
> $ echo $a
> 0
Alright. I thought you meant that let a=a+0 was to be the
solution to the overall problem. Sorry about that.
--
Stéphane
Re: Convert 08 to decimal 8
am 10.09.2007 10:39:23 von SiKing
Stephane CHAZELAS wrote:
> 2007-09-07, 16:03(+01), SiKing:
>> Stephane CHAZELAS wrote:
>>> 2007-09-07, 15:35(+01), SiKing:
>>> [...]
>>>> Hmmm, I am going to have to use "s/^0*//", because I have leading zeros
>>>> (zeroes?) - plural.
>>> [...]
>>>
>>> That will turn 0 or 000 into the empty string.
>> Right, I just figured that out too. :(
>>
>>>> But can bash not do this on its own?
>>> [...]
>>>
>>> bash, non standard:
>> What does that mean?
>>
>>> a=$((10#$a))
>
> Just that it will work with bash (and ksh and zsh) but is not a
> standard sh feature. Generally, you write POSIX sh scripts as
> the POSIX sh syntax is generally far enough to write scripts.
> This way, you know that your script can be interpreted on any
> system because all Unix systems have a POSIX conformant sh,
> wether it's bash or any other shell.
OK, thanx for the explanation.
At work, for several other reasons, I am able to hardwire all my scripts for
GNU-bash, GNU-awk, and any other GNU- thing that I may need. The admins are
pretty good and agreeable at installing whatever I ask for in /usr/local on
every machine. So in this case I can stick with the above solution.
Just frustrates me, that this was not obvious from the manual.
> Note that expr is meant to work in decimal, so
>
> a=$(expr 0 + "$a")
>
> should work as well.
Hey, thanx for all the great suggestions.
SK.
Re: Convert 08 to decimal 8
am 11.09.2007 05:45:48 von brian_hiles
SiKing wrote:
> Stephane CHAZELAS wrote:
> > SiKing:
> > > Stephane CHAZELAS wrote:
> > > > SiKing:
> > > > > ...
> > > > a=$((10#$a))
> > a=$(expr 0 + "$a")
Horrors! Using expr(1) or sed(1) just to coerse a numerical type!
That's like using fork(1) to increment an integer!
(Even DGK got into the act (*shudder*): http://www0.us.ioccc.org/1987/korn.c)
The correct solution has been mentioned above. For background
and discussion, see:
https://mailman.research.att.com/pipermail/ast-users/2006q4/ 001471.html
https://mailman.research.att.com/pipermail/ast-users/2006q4/ 001483.html
=Brian
Re: Convert 08 to decimal 8
am 11.09.2007 07:14:23 von Cyrus Kriticos
SiKing wrote:
>
> I am running this under bash 3.1.
>
> What is the secret formula?
$ a=08
$ printf -v a "%d" 0x$a
$ echo $a
8
--
Best regards | "The only way to really learn scripting is to write
Cyrus | scripts." -- Advanced Bash-Scripting Guide
Re: Convert 08 to decimal 8
am 11.09.2007 09:23:23 von Stephane CHAZELAS
2007-09-10, 20:45(-07), bsh:
> SiKing wrote:
>> Stephane CHAZELAS wrote:
>> > SiKing:
>> > > Stephane CHAZELAS wrote:
>> > > > SiKing:
>> > > > > ...
>> > > > a=$((10#$a))
>> > a=$(expr 0 + "$a")
>
> Horrors! Using expr(1) or sed(1) just to coerse a numerical type!
[...]
Hi Brian,
What do you think expr is about then?
> That's like using fork(1) to increment an integer!
What do you think a shell is about if it's not forking processes
to run commands?
--
Stéphane
Re: Convert 08 to decimal 8
am 11.09.2007 21:12:35 von cfajohnson
On 2007-09-11, Stephane CHAZELAS wrote:
> 2007-09-10, 20:45(-07), bsh:
>> SiKing wrote:
>>> Stephane CHAZELAS wrote:
>>> > SiKing:
>>> > > Stephane CHAZELAS wrote:
>>> > > > SiKing:
>>> > > > > ...
>>> > > > a=$((10#$a))
>>> > a=$(expr 0 + "$a")
>>
>> Horrors! Using expr(1) or sed(1) just to coerse a numerical type!
> [...]
>
> Hi Brian,
>
> What do you think expr is about then?
expr is about providing the Bourne shell with capabilities for
arithemtic and string manipulation. It has little or no place in a
POSIX shell script.
>> That's like using fork(1) to increment an integer!
>
> What do you think a shell is about if it's not forking processes
> to run commands?
A POSIX shell is a programming language.
--
Chris F.A. Johnson, author
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
Re: Convert 08 to decimal 8
am 12.09.2007 15:01:24 von Brian Mac
hi SiKing,
i know you specifically mentioned you would like to do this using the
bash shell. i'm not sure how using bash (others have explained much
better than i) but in korn shell you could use typeset:
$ typeset -LZ num=08
$ print $num
8
however, keep in mind you will run into the same issue where the
number 0 is stored as an empty string.
hope this helps,
brian
On Sep 7, 10:10 am, SiKing wrote:
> Hi all,
>
> I am running this under bash 3.1.
>
> I have two variables that each contain a number, which I need to compare for
> equality. Unfortunately, sometimes, the number also contains leading zeros - I
> need 8 to be equal to 08, for example.
> I tried fixing it with 'let var+=0', but (quoted from the manual):
> Constants with a leading 0 are interpreted as octal numbers. Otherwise, numbers
> take the form [base#]n, where base is a decimal number between 2 and 64
> representing the arithmetic base, and n is a number in that base. If base# is
> omitted, then base 10 is used.
>
> I can't seem to able to get this syntax to work. All of the following give me
> syntax errors.
> a=08
> echo $(( $a + 0 ))
> echo $(( []$a + 0 ))
> echo $(( [10]$a + 0 ))
> echo $(( [10#]$a + 0 ))
>
> What is the secret formula?
> Thank You.