简体   繁体   English

批处理脚本-此时出现意外错误

[英]Batch script - unexpected at this time error

Below is my script where I run "test.bat 1" command in command prompt, 1 is taken as input by my java program and it returns incrementing 1 ie 2. This should go on until the value is 10. 以下是我在命令提示符下运行“ test.bat 1”命令的脚本,我的Java程序将1作为输入,它返回增量1,即2。这应该一直进行到值10。

Following is my batch script. 以下是我的批处理脚本。

@echo off

set "java_output="

setlocal enableDelayedExpansion

:top

for /f "delims=" %%J in ('java -jar test.jar %*') do (
      set "java_output=!java_output! %%J" 
)
set java_output=%java_output%
echo %java_output%

if %java_output% NEQ 10 goto top
endlocal

and below is my java code in jar. 下面是我在jar中的Java代码。

public class Test {

    public static void main(String[] args) 
       {
        System.out.println(args[0]);
        int ret = Integer.parseInt(args[0]);
        System.out.println(ret+1);
       }
}

The following is the output that I am getting. 以下是我得到的输出。

C:>test.bat 1 C:> test.bat 1
1 2 1 2
2 was unexpected at this time. 2在这个时候是意外的。

Can anyone tell me whats the issue. 谁能告诉我是什么问题。

Since your output in java_output is 1 2 (as displayed) then the if statement becomes 由于您在java_output的输出为1 2 (如显示),因此if语句变为

if 1 2 neq 10 ...

if expects if string1 op string2 ... and sees 2 as the comparison operator which must be one of == , equ , neq , lss . if期望if string1 op string2 ...并将2视为比较运算符,则必须是==equneqlss leq , gtr , geq leqgtrgeq

Since you are stringing numerics together with spaces, it's extremely unlikely ever to be anything other than not-equal to 10 . 由于您是将数字和空格串在一起,因此除了不等于10之外,几乎不可能是其他任何东西。


Given response: 给出的响应:

  set "java_output=!java_output! %%J" 
)
set java_output=%java_output%

should be 应该

  set /a java_output=%%J"
)

The

set java_output=%java_output%

line does nothing and is redundant. 行什么也不做,是多余的。

set /a assigns a numeric value or arithmetic expression. set /a分配一个数值或算术表达式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM