为什么我的IF ELSE没有工作?

时间:2015-08-01 01:49:58

标签: shell batch-file command-line batch-processing

这是我写的一个简单的剧本,根本没什么好看的

@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 source,stream2 source等运行程序livestream.exe,具体取决于用户的用户身份输入

如果它不是1-8,那么程序会询问质量,所以来源/高/中/低。

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

Start C:\livestreamer\livestreamer.exe stream1 source

如果是2,

Start C:\livestreamer\livestreamer.exe stream1 high

如果他们输入例如asdfasdf

然后投入高位,然后决赛将是

Start C:\livestreamer\livestreamer.exe asdfasdf high

如果我删除最终其他条件中的内容,它会起作用,但否则它不会:(

我得到的错误是

( was unexpected at this time.

4 个答案:

答案 0 :(得分:1)

首先,batch不是一种强大的语言。
我遇到了和你一样的问题,所以我意识到括号需要采用那种确切的格式,否则它根本不起作用。即使在语句的if部分放置括号也会阻止脚本按预期工作
您可以使用choice语句,这是一个确定用户输入的简单语句。

@echo off


:point_one
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 /a id = 0

choice /c:123456789 /m "Enter ID: " /n

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

goto point_three


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

choice /c:12345678 /m "Enter ID: " /n

IF %ERRORLEVEL% == 1 (
    set hq="source"
)
IF %ERRORLEVEL% == 2 (
    set hq="high"
)
IF %ERRORLEVEL% == 3 (
    set hq="medium"
)
IF %ERRORLEVEL% == 4 (
    set hq="low"
) 
ELSE set hq="source"

set link=twitch.tv/%id% %hq%

goto point three


:point_three

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

echo %link%

pause >nul

我仍然不明白为什么在最后添加else语句。我的意思是为什么不为用户添加额外的选择以选择是否要转到:point_two

答案 1 :(得分:1)

基本上,你属于delayed expansion trap,但你可以通过简化你的代码轻松避免它(它是不必要的复杂)。类似的东西:

@echo off
set hq=

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 link=none
set /p id=Enter ID:
IF "%id%"=="1" set link=twitch.tv/stream1 source
IF "%id%"=="2" set link=twitch.tv/stream1 high
IF "%id%"=="3" set link=twitch.tv/stream1 medium
IF "%id%"=="4" set link=twitch.tv/stream1 low
IF "%id%"=="5" set link=twitch.tv/stream2 source
IF "%id%"=="6" set link=twitch.tv/stream2 high
IF "%id%"=="7" set link=twitch.tv/stream2 medium
IF "%id%"=="8" set link=twitch.tv/stream1 high

if "%link%" neq "none" goto :skip
cls
echo ..............
echo 1 = source
echo 2 = high
echo 3 = medium
echo 4 = low
echo ..............
set /p quality=Enter ID:
IF "%quality%"=="1" set hq="source"
IF "%quality%"=="2" set hq="high"
IF "%quality%"=="3" set hq="medium"
IF "%quality%"=="4" set hq="low"
REM are you sure, you want the quotes in %hq% ?

:skip
set param=twitch.tv/%link% %hq%
Start C:\livestreamer\livestreamer.exe %param%
echo %param%
pause

你在最后几行中对你的参数有一个逻辑怪癖。

答案 2 :(得分:0)

试试这个:

@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:
::point one
IF "%id%"=="1" (
    set link=twitch.tv/stream1 source
) 
IF "%id%"=="2" (
    set link=twitch.tv/stream1 high
) 
IF "%id%"=="3" (
    set link=twitch.tv/stream1 medium
) 
IF "%id%"=="4" (
    set link=twitch.tv/stream1 low
) 
IF "%id%"=="5" (
    set link=twitch.tv/stream2 source
) 
IF "%id%"=="6" (
    set link=twitch.tv/stream2 high
) 
IF "%id%"=="7" (
    set link=twitch.tv/stream2 medium
)
IF "%id%"=="8" (
    set link=twitch.tv/stream1 high
)
::point one end
cls
echo ..............
echo 1 = source
echo 2 = high
echo 3 = medium
echo 4 = low
echo ..............
set /p quality=Enter ID:
::point two begin
    IF "%quality%"=="1" (
        set hq="source"
    ) 
    IF "%quality%"=="2" (
        set hq="high"
    ) 
    IF "%quality%"=="3" (
        set hq="medium"
    ) 
    IF "%quality%"=="4" (
        set hq="low"
    )
    set link=twitch.tv/%id% %hq%
    ::point two end

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

echo %link%

pause

你的IF-else语法是不正确的(从我标记的第一点开始指向一端)同样适用于我标记为第二点的点开始指向两端。该脚本现在应该可以正常工作。

希望有所帮助!

答案 3 :(得分:0)

代码中的错误是由延迟扩展陷阱产生的,正如Stephan在他的回答中指出的那样:quality变量在代码块中读取,因此{{1}扩展得到变量在块之前具有的相同值。您需要按%quality%更改该扩展,并在开头添加!quality!以修复错误。

但是,您应该知道array concept是一种编程语言用于以相同方式处理一系列元素的方法。例如:

setlocal EnableDelayedExpansion

有关批处理文件中阵列管理的进一步说明,请参阅this post