( was unexpected at this time - batch script

前端 未结 3 708
别跟我提以往
别跟我提以往 2021-02-08 01:10

I\'m using the batch script below and get an error

( was unexpected at this time.

I know that the problem is in the first line but

3条回答
  •  故里飘歌
    2021-02-08 02:04

    Actually, the problem is not on the first line.

    The problem is that cmd does variable substitution immediately when it parses the IF statement, including its body. Therefore the line:

    IF %isDefault%==y (
    

    is problematic because isDefault isn't set when the outer IF statement is parsed, so it becomes:

    IF ==y (
    

    and hence you get the error about ( being unexpected. You can get around this by enabling the command extension (SETLOCAL ENABLEDELAYEDEXPANSION) for delayed environment variable expansion (see set /? for details). You also can rewrite your script:

    @ECHO OFF
    IF NOT "%1"=="" GOTO :EOF
    
    :LOOP1
    SET /P isDefault=Value Missing, do you want to use default values [1,1,10,Local Area Connection 2]?[y/n]
    IF "%isDefault%"=="y" (
        SET from=1
        SET step=1
        SET to=10
        SET lan="Local Area Connection 2"
        GOTO :USERLOOP
    )
    IF "%isDefault%"=="n" GOTO :EOF
    GOTO :LOOP1
    

    (I made some other changes, such as using the built-in :EOF label instead of :END.)

提交回复
热议问题