PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Serveur - Sécurité et techniques > comp.unix.shell > Convert 08 to decimal 8
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

Convert 08 to decimal 8

Réponse
 
LinkBack Outils de la discussion
Vieux 07/09/2007, 15h10   #1
SiKing
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Convert 08 to decimal 8

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.
  Réponse avec citation
Vieux 07/09/2007, 15h21   #2
Miles
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Convert 08 to decimal 8

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.

  Réponse avec citation
Vieux 07/09/2007, 15h35   #3
SiKing
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Convert 08 to decimal 8

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.
  Réponse avec citation
Vieux 07/09/2007, 15h51   #4
Stephane CHAZELAS
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Convert 08 to decimal 8

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
  Réponse avec citation
Vieux 07/09/2007, 16h03   #5
SiKing
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Convert 08 to decimal 8

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!
  Réponse avec citation
Vieux 07/09/2007, 16h28   #6
Stephane CHAZELAS
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Convert 08 to decimal 8

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
  Réponse avec citation
Vieux 07/09/2007, 18h37   #7
Neil Cherry
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Convert 08 to decimal 8

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
  Réponse avec citation
Vieux 07/09/2007, 18h52   #8
Icarus Sparry
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Convert 08 to decimal 8

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.
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 07h25.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,14761 seconds with 16 queries