|
|
|
|
||||||
| comp.unix.shell Using and programming the Unix shell. |
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
On Sep 7, 9:10 am, SiKing <nos...@noway.invalid> 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. |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
Miles wrote:
> On Sep 7, 9:10 am, SiKing <nos...@noway.invalid> 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. |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
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! |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
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. |
|
![]() |
| Outils de la discussion | |
|
|