(是意外在这个时候 - 批处理脚本 [英] ( was unexpected at this time - batch script

查看:193
本文介绍了(是意外在这个时候 - 批处理脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用下面的批处理脚本并得到一个错误


  

(是意外的在这个时候。


我知道问题在第一线,但我不明白什么是错的。
任何想法?

脚本:

  IF [%1] == [](
    :LOOP1
    SET / p = ISDEFAULT缺少值,你要使用默认值[1,1,10,本地连接2] [Y / N]
    IF%ISDEFAULT%== Y(
        从= 1套
        SET步= 1
        SET为= 10
        设置LAN =本地连接2
        GOTO:USERLOOP
    )
    IF%ISDEFAULT%==ñGOTO:END
    GOTO:LOOP1


解决方案

其实,这个问题的的第一行。

问题是, CMD 立即做变量替换,当解析如果语句,包括它的身体。因此该行:

  IF%ISDEFAULT%== Y(

由于 ISDEFAULT 当外部如果语句解析未设置,因此它成为是有问题的:

  IF = = Y(

,因此你会得到关于错误(是意想不到的。你可以解决这个问题通过启用命令扩展( SETLOCAL ENABLEDELAYEDEXPANSION )的延迟的环境变量扩展的(见设置/ 了解详情)您也可以重写你的脚本:

  @ECHO OFF
如果不是%1==GOTO:EOF:LOOP1
SET / p = ISDEFAULT缺少值,你要使用默认值[1,1,10,本地连接2] [Y / N]
IF%ISDEFAULT%==Y(
    从= 1套
    SET步= 1
    SET为= 10
    设置LAN =本地连接2
    GOTO:USERLOOP

IF%ISDEFAULT%==NGOTO:EOF
GOTO:LOOP1

(我做了一些其他的变化,比如使用内置的:EOF 标签,而不是:END 。)

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 I don't understand what is wrong. Any ideas ?

script:

IF [%1]==[] (
    :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 :END
    GOTO :LOOP1 
)

解决方案

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.)

这篇关于(是意外在这个时候 - 批处理脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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