简体   繁体   中英

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.

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.

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
1 2
2 was unexpected at this time.

Can anyone tell me whats the issue.

Since your output in java_output is 1 2 (as displayed) then the if statement becomes

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 . leq , gtr , geq

Since you are stringing numerics together with spaces, it's extremely unlikely ever to be anything other than not-equal to 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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