为何我的 IF ELSE 不起作用?

为何我的 IF ELSE 不起作用?

这是我写的一个简单的脚本,没什么特别的

@echo off

echo 1 = stream1 source
echo 2 = stream1 high
echo 3 = stream1 medium
echo 4 = stream1 low
echo 5 = stream2 source
echo 6 = stream2 high
echo 7 = stream2 medium
echo 8 = stream2 low

set /p id="Enter ID: "

IF %id% == 1 (
    set link=twitch.tv/stream1 source
) ELSE IF %id% == 2 (
    set link=twitch.tv/stream1 high
) ELSE IF %id% == 3 (
    set link=twitch.tv/stream1 medium
) ELSE IF %id% == 4 (
    set link=twitch.tv/stream1 low
) ELSE IF %id% == 5 (
    set link=twitch.tv/stream2 source
) ELSE IF %id% == 6 (
    set link=twitch.tv/stream2 high
) ELSE IF %id% == 7 (
    set link=twitch.tv/stream2 medium
) ELSE IF %id% == 8 (
    set link=twitch.tv/stream1 high
) ELSE (
echo ..............
echo 1 = source
echo 2 = high
echo 3 = medium
echo 4 = low

set /p quality="Enter ID: "

    IF %quality% == 1 (
        set hq="source"
    ) ELSE IF %quality% == 2 (
        set hq="high"
    ) ELSE IF %quality% == 3 (
        set hq="medium"
    ) ELSE IF %quality% == 4 (
        set hq="low"
    ) ELSE (
        set hq="source"
    )
    set link=twitch.tv/%id% %hq%
)

Start C:\livestreamer\livestreamer.exe %link%

echo %link%

pause

它的作用是要求输入一个 1-8 的数字,如果是 1-8,它将根据用户输入的内容使用命令行 stream1 源、stream2 源等运行程序 livestream.exe。

如果不是 1-8,则程序会要求质量,因此是源/高/中/低。

例如,如果他们输入 1,那么最终运行的命令是

Start C:\livestreamer\livestreamer.exe stream1 source

如果 2,

Start C:\livestreamer\livestreamer.exe stream1 high

ETC

如果他们输入例如 asdfasdf

然后放入高,那么最后将是

Start C:\livestreamer\livestreamer.exe asdfasdf high

如果我删除最后的 else 条件中的内容,它就会起作用,否则它就不会起作用:(

我得到的错误是

( was unexpected at this time.

答案1

看来您需要将 %id% == 1 类型语句更改为“%id%”=="1" 的形式。

当宏变量(例如 %id%)设置为空格时,它将扩展为:IF space== 1(这不是有效的语法。但是如果你用一些字符集(我通常使用双引号)将宏变量括起来,语句将扩展为 IF“ space”==1(这是有效的语法,但不符合你的意图。为了使其工作,你需要用相同的字符(在本例中为双引号)将比较值括起来。

IF "%id%"=="1"

IF "%id%"=="1" (
    set link=twitch.tv/stream1 source
) ELSE IF "%id%"=="2" (
    set link=twitch.tv/stream1 high
) ELSE IF "%id%"=="3" (
    set link=twitch.tv/stream1 medium
...

    IF "%quality%"=="1" (
        set hq="source"
    ) ELSE IF "%quality%"=="2" (
        set hq="high"
    ...

答案2

发生故障的线路是IF %quality% == 1

虽然批处理文件在逻辑上只执行一个分支中的嵌套If,但整个If语句将作为一个命令进行处理,因此整个命令在所有分支中都必须有效。您的 If 语句(作为一个整体)在处理时无效。

如果启用 Echo,您将看到线路故障,如下所示

( was unexpected at this time.
C:\Temp>    IF  == 1 (

请注意失败的部分:IF == 1 (

这是无效的,因为比较没有左侧。比较没有左侧,因为在处理 If 之前没有执行set给变量赋值的操作(因为它嵌套在 If 中)。quality

修复:

用引号将所有比较的两边括起来,这样就会出现一个空值,""而不仅仅是一个空格。

例如:

IF "%id%" == "1" (
    set link=twitch.tv/stream1 source
) ELSE IF "%id%" == "2" (
    set link=twitch.tv/stream1 high
) ELSE IF "%id%" == "3" (
    set link=twitch.tv/stream1 medium
) ELSE IF "%id%" == "4" (
    set link=twitch.tv/stream1 low
) ELSE IF "%id%" == "5" (
    set link=twitch.tv/stream2 source
) ELSE IF "%id%" == "6" (
    set link=twitch.tv/stream2 high
) ELSE IF "%id%" == "7" (
    set link=twitch.tv/stream2 medium
) ELSE IF "%id%" == "8" (
    set link=twitch.tv/stream1 high
) ELSE (...

另一个解决方法:

将嵌套的 If 移至其自己的标记部分,并使用Goto语句来引导流程。

@echo off

echo 1 = stream1 source
echo 2 = stream1 high
echo 3 = stream1 medium
echo 4 = stream1 low
echo 5 = stream2 source
echo 6 = stream2 high
echo 7 = stream2 medium
echo 8 = stream2 low

set /p id="Enter ID: "

IF %id% == 1 (
    set link=twitch.tv/stream1 source
) ELSE IF %id% == 2 (
    set link=twitch.tv/stream1 high
) ELSE IF %id% == 3 (
    set link=twitch.tv/stream1 medium
) ELSE IF %id% == 4 (
    set link=twitch.tv/stream1 low
) ELSE IF %id% == 5 (
    set link=twitch.tv/stream2 source
) ELSE IF %id% == 6 (
    set link=twitch.tv/stream2 high
) ELSE IF %id% == 7 (
    set link=twitch.tv/stream2 medium
) ELSE IF %id% == 8 (
    set link=twitch.tv/stream1 high
) ELSE (
    goto ChooseQuality
)

goto StartProgram


:ChooseQuality

echo ..............
echo 1 = source
echo 2 = high
echo 3 = medium
echo 4 = low

set /p quality="Enter ID: "

IF %quality% == 1 (
    set hq="source"
) ELSE IF %quality% == 2 (
    set hq="high"
) ELSE IF %quality% == 3 (
    set hq="medium"
) ELSE IF %quality% == 4 (
    set hq="low"
) ELSE (
    set hq="source"
)
set link=twitch.tv/%id% %hq%

goto StartProgram


:StartProgram

Start C:\livestreamer\livestreamer.exe %link%

echo %link%

pause

相关内容