windows 为什么我的 if Else if 语句在批处理脚本中不起作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7710612/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 08:48:02  来源:igfitidea点击:

Why does my if Else if statement not work in Batch script?

windowsbatch-file

提问by ChrisBD

My Script

我的脚本

echo "Enter your choice (1 or 2 or 3) :"
set /p dbchoice=

IF %dbchoice EQU 1 ( 
    set dbtype="oracle"
) ELSE ( 
    IF %dbchoice EQU 2 ( 
        set dbtype="sqlserver" 
    )
) ELSE ( 
    IF %dbchoice EQU 3 ( 
        set dbtype="db2" 
    )
) ELSE (
    echo "Incorrect choice" 
)

I get the following output:

我得到以下输出:

E:\csmilm>set /p dbchoice=
1
ELSE was unexpected at this time.
E:\csmilm>) ELSE (
E:\csmilm>

What is the problem here?

这里有什么问题?

回答by ChrisBD

Closing brackets in the wrong place. Try:

在错误的地方结束括号。尝试:

IF %dbchoice EQU 1 ( 
    set dbtype="oracle"
) ELSE ( 
    IF %dbchoice EQU 2 ( 
        set dbtype="sqlserver" 
    ) ELSE ( 
        IF %dbchoice EQU 3 ( 
            set dbtype="db2" 
        ) ELSE (
            echo "Incorrect choice" 
        )
    )
)