using @ with set /a and >nul, gives "missing operand". Why?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
taripo
Posts: 228
Joined: 01 Aug 2011 13:48

using @ with set /a and >nul, gives "missing operand". Why?

#1 Post by taripo » 13 Apr 2013 11:27

Code: Select all

C:\>@set a=4>nul

C:\>@set /a a=4>nul
Missing operand.


Another example

C:\Users\gary\AppData\Local\Temp>for /L %f in (1,1,2) do @set /a ggg=4>nul
Missing operand.
Missing operand.

C:\>echo %ggg%
%ggg%

C:\>

I notice if I do this I get no error..

Code: Select all

C:\>for /L %f in (1,1,2) do @(set /a ggg=4)>nul

C:\>


But that doesn't work here

Code: Select all

C:\>for /f %f in (%tempfile3%) do @(set /a total+=%f)>nul
Missing operand.


It totals the numbers but it still says "Missing operand."

How can I get rid of that message or still run the command but prevent that message from appearing in the first place?

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: using @ with set /a and >nul, gives "missing operand". W

#2 Post by dbenham » 13 Apr 2013 11:54

The @ has nothing to do with it. This also fails

Code: Select all

set /a a=4>nul

Either of the following work

Code: Select all

@set /a a=4 >nul
@set /a "a=4">nul


I suspect your last example using FOR /F fails because of the value contained in %f

If you substitute a number for constant %f, then the code works.


Dave Benham

taripo
Posts: 228
Joined: 01 Aug 2011 13:48

Re: using @ with set /a and >nul, gives "missing operand". W

#3 Post by taripo » 13 Apr 2013 12:36

thanks that works, and you were right about the value contained in %f being a problem, it was non-numeric, from the file I was reading.

Regarding set /a dfds=4>nul failing but it working when there's a space between the expression and the >

It looks to my less skilled mind, like the cmd interpreter is choosing not to interpret > as a special, redirection symbol, and instead, treating it as literal and passing it to the set /a. I notice set f=4>nul fails though gives no error.

But dir>nul or dir>myfile works.

With echo, one famously knows that echo abc >myfile is going to echo a and b and c and space(and sadly the hard to avoid new line), to the file. One typically wouldn't want the space, and would do echo abc>myfile.

I always figured the space is superfluous, and I suppose it often is..

Why with set must there be a space before the >?

What is it about SET that causes cmd to not see it as a redirection unless there's a space before it?

Endoro
Posts: 244
Joined: 27 Mar 2013 01:29
Location: Bozen

Re: using @ with set /a and >nul, gives "missing operand". W

#4 Post by Endoro » 13 Apr 2013 14:52

Interesting:

Code: Select all

C:\TEST>for /L %f in (1,1,2) do set /a ggg=04>nul

C:\TEST>set /a ggg=04 1>nul

C:\TEST>set /a ggg=04 1>nul

C:\TEST>echo %ggg%
4


Code: Select all

C:\TEST>for /L %f in (1,1,2) do set /a ggg=%f>nul

C:\TEST>set /a ggg=1 1>nul

C:\TEST>set /a ggg=2 1>nul

C:\TEST>echo %ggg%
2

:)

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: using @ with set /a and >nul, gives "missing operand". W

#5 Post by dbenham » 13 Apr 2013 15:19

taripo wrote:Why with set must there be a space before the >?
What is it about SET that causes cmd to not see it as a redirection unless there's a space before it?

Actually, CMD is interpreting the > as redirection. The SET command is not seeing the 4 because it is being interpreted as a file handle for the redirection. So "of course" the SET command reports that it is missing an operand :wink:

CMD defines handle 0 for stdin, 1 for stdout, and 2 for stderr. Handles 3-9 are undefined and available for use.

The following code first directs handle 4 to a file, and then redirects stdout to 4, so the ECHO output appears in "test.txt"

Code: Select all

4>test.txt (
  echo Hello world >&4
)

When no handle is specified before >, then the interpreter assumes 1 (stdout).


Dave Benham

jeb
Expert
Posts: 1062
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: using @ with set /a and >nul, gives "missing operand". W

#6 Post by jeb » 15 Apr 2013 02:45

But the discovery of Endoro is remarkable :!:
Endoro wrote:set /a ggg=04>nul

It assigns 04, not 0 as expected.

Probably the rule is: A digit in front of a redirect symbol is only used as redircet stream number if it stands alone

Btw. There exists problems with the @ character :)

Code: Select all

@echo Works < nul
<nul @echo Fails
@ <nul echo Works again


jeb

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: using @ with set /a and >nul, gives "missing operand". W

#7 Post by Squashman » 15 Apr 2013 07:05

But you still can't use 08.

Post Reply