PC Review


Reply
Thread Tools Rate Thread

Bug in CMD.exe? Parentheses confused

 
 
Rich Pasco
Guest
Posts: n/a
 
      9th Feb 2007
Scripts for the command-shell interpreter cmd.exe use parentheses in
two ways: One is for blocks, as in

if condition (
do this
and this
)

The other is for grouping in arithmetic expressions, like this:

set /a result = 2*(3+4)

which assigns the value 14 to the environment variable "result."

However, using such an assignment inside a block leads to confusion, e.g.

if 1==1 (
set /a result = 2*(3+4)
)

which leads to an "Unbalanced parenthesis" error as the closing
parenthesis on the arithmetic expression is mistaken as the closing
parenthesis on the conditional block.

- Rich





 
Reply With Quote
 
 
 
 
Eman
Guest
Posts: n/a
 
      9th Feb 2007
Rich Pasco wrote:

> However, using such an assignment inside a block leads to
> confusion, e.g.
>
> if 1==1 (
> set /a result = 2*(3+4)
> )
>
> which leads to an "Unbalanced parenthesis" error as the
> closing parenthesis on the arithmetic expression is
> mistaken as the closing parenthesis on the conditional
> block.


It's not out of place to escape all suspect
on the right of the "set something=" statement.
E.g., this would work:

~~
@echo off
if 1==1 (
set /a result = 2*^(3+4^)
)
echo result=%result%
~~

And this one too:

~~
@echo off
if 1==1 (
set /a result =^ ^2^*^(^3^+^4^)
)
echo result=%result%
~~

 
Reply With Quote
 
Rich Pasco
Guest
Posts: n/a
 
      9th Feb 2007
Thanks.

Also you the numbers in set statement arithmetic must be interger, not
decimal fractions.

This works:
set /a sum=2+2

This doesn't:
set /a sum=2.1+2.2

- Rich

Eman wrote:

> It's not out of place to escape all suspect
> on the right of the "set something=" statement.
> E.g., this would work:
>
> ~~
> @echo off
> if 1==1 (
> set /a result = 2*^(3+4^)
> )
> echo result=%result%
> ~~
>
> And this one too:
>
> ~~
> @echo off
> if 1==1 (
> set /a result =^ ^2^*^(^3^+^4^)
> )
> echo result=%result%
> ~~
>

 
Reply With Quote
 
Matthias Tacke
Guest
Posts: n/a
 
      9th Feb 2007
Rich Pasco schrieb:
> Scripts for the command-shell interpreter cmd.exe use parentheses in
> two ways: One is for blocks, as in
>
> if condition (
> do this
> and this
> )
>
> The other is for grouping in arithmetic expressions, like this:
>
> set /a result = 2*(3+4)
>
> which assigns the value 14 to the environment variable "result."
>
> However, using such an assignment inside a block leads to confusion, e.g.
>
> if 1==1 (
> set /a result = 2*(3+4)
> )
>


My german help of "set /?" says you've to put the expression in dbl quotes
when using arithmetic operators. Even it isn't necessary in most cases,
you've to use them for this to work.

if 1==1 (
set /a result ="2*(3+4)"
)
 
Reply With Quote
 
Rich Pasco
Guest
Posts: n/a
 
      9th Feb 2007
Danke sehr, Matthais. Now that you mention it, the English help file
also says,

If you use any of the logical or modulus operators, you will need to
enclose the expression string in quotes.

I hadn't noticed that before. While Eman's suggestion of escaping the
special characters works, putting quotes around the entire expression
is simpler and makes it easier to read.

Since that is documented in the help file, I withdraw my "bug" report. :-)

- Rich


Matthias Tacke wrote:

> My german help of "set /?" says you've to put the expression in dbl quotes
> when using arithmetic operators. Even it isn't necessary in most cases,
> you've to use them for this to work.
>
> if 1==1 (
> set /a result ="2*(3+4)"
> )

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
RE: removing parentheses Mike Microsoft Excel Programming 3 2nd Dec 2008 09:18 PM
parentheses Brendy Microsoft Excel Worksheet Functions 5 15th Jan 2006 09:24 PM
Counting Parentheses =?Utf-8?B?dGVkZHliNzc3?= Microsoft Excel Worksheet Functions 2 16th Jun 2004 09:17 AM
Counting Parentheses =?Utf-8?B?dGVkZHliNzc3?= Microsoft Excel Worksheet Functions 2 16th Jun 2004 12:30 AM
Confused, need valid logon for OE, confused.. Rob Windows XP General 1 3rd Nov 2003 02:15 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:45 PM.