在批处理中使用SET/a时为什么会缺少运算符? [英] Why do I get a missing operator when using SET /a in Batch?

查看:113
本文介绍了在批处理中使用SET/a时为什么会缺少运算符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码行是:

SET /A 327761=%RANDOM% * 1000 / 32768 + 1  

我正遇到一个缺少的操作员错误.我不知道这怎么可能,这里的其他问题也无济于事.

And I'm just getting a missing operator error. I don't see how this could be and other questions on here don't help.

推荐答案

虽然以数字开头的变量是一个不好的主意,但通常的问题是在尝试检索值而不是设置值时出现的.也就是说,这样做没有任何问题

While it is a bad idea to name a variable starting with a number, the usual problem araises when trying to retrieve the value, not when you set it. That is, there is not any problem in doing

set "1234=some value"

您稍后会在检索该值时遇到问题,因为%1234%被解析为%1 (批处理文件的第一个参数),后跟字符串 234%

You will have later problems retrieving the value because %1234% is parsed as %1 (first argument to batch file) followed by the string 234%

但是,正如您所说,您可以设置该值.那么,为什么像 set/a"1234 = 1 + 1" 这样的东西会引发错误?

But as said, you can set the value. So, why something like set /a "1234=1+1" raises an error?

原因是首先解析 cmd 中的命令以确定要执行的命令,但是在某些情况下,一旦识别出命令,其余的工作就会委托给命令特定的解析器. for 命令就是这种行为的一个例子,但是 set/a 也有自己的解析器.

The reason is that the commands in cmd are first parsed to determine the command to execute, but in some cases, once the command has been identified, the rest of the work is delegated to a command specific parser. for command is one example of this behaviour, but set /a also has its own parser.

set/a 解析器不仅处理 var = value 语法.您也可以做类似的事情

This set /a parser does not only handle the var=value syntax. You can also do things like

 set /a 123+123

检索未将计算结果存储在任何地方的结果.

to retrieve the result of the calc not storing it anywhere.

set/a 解析器将其参数作为数字/算术/逻辑/位/赋值操作数/运算符的序列进行处理. = 只是一个赋值运算符,该运算符需要其左侧的操作数为变量名.

The set /a parser handles its parameters as a sequence of numeric/arithmetic/logic/bit/assignment operands/operators. The = is just an assignment operator, and this operator needs the operand on its left side to be a variable name.

问题是 set/a 解析器将数字操作数放在变量名之前:如果它以数字开头,则它是数字操作数.在您的情况下, set/a 327761 = .... 327761 文字作为数字操作数而不是变量名称处理,并且赋值运算符不能将一个值分配给另一个值.

The problem is that the set /a parser gives precedence to the numeric operands over the variable names: if it starts with a number, it is a number operand. In your case, set /a 327761=...., the 327761 literal is handled as a numeric operand, not as a variable name, and the assignment operator can not assign a value to another value.

这篇关于在批处理中使用SET/a时为什么会缺少运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆