接受用户输入后,脚本无法运行第二个功能

时间:2019-07-08 22:25:39

标签: batch-file

我的目标是首先提示,回显该行,发出下一个提示,然后显示press any key to exit...

我不确定为什么我的没有发出第二个提示。

@echo off

::deploying to test
set /p tdeploy="Deploy to test: [y/n]"

IF /I "%tdeploy%"=="y"(
    call :deploy_test
    if /I "%ERRORLEVEL%" NEQ "0"(
        echo Deploy test failed
    )
)

::deploying to argos
set /p adeploy="Deploy to argos: [y/n]"

IF /I "%adeploy%"=="y"(
    call :deploy_argos
    if /I "%ERRORLEVEL%" NEQ "0"(
        echo Deploy argos failed
    )
)
set /p DUMMY=Press any key to exit...


:deploy_test
ECHO deploying test!
goto :eof


:deploy_argos
ECHO deploying argos!
goto :eof

:eof
set /p DUMMY=Press any key to exit.222..

2 个答案:

答案 0 :(得分:0)

我通常更喜欢将其构造为嵌套的IF ELSE,以避免使用一堆GOTO命令,并且必须弄清楚错误级别:

@echo off

REM deploying to test
set /p tdeploy="Deploy to test: [y/n]"

if /i "%tdeploy%" == "y" (
    echo           deploying test!
    ) else (
    if /i "%tdeploy%" == "n" (
        echo           deploy test cancelled
        ) else (
        echo           seriously, there were only two options...
        )
    )
)

REM deploying to argos
set /p adeploy="Deploy to test: [y/n]"

if /i "%adeploy%" == "y" (
    echo           deploying test!
    ) else (
    if /i "%adeploy%" == "n" (
        echo           deploy test cancelled
        ) else (
        echo           seriously, there were only two options...
        )
    )
)

pause

pause将成为您的“按任意键继续...”-其余间距只是为了使其更加可见。

答案 1 :(得分:0)

我建议您在choice.exeY问题中使用N

@Echo Off

Rem Deploying to test
Choice /M "Deploy to test"
If "%ERRORLEVEL%"=="1" (Call :deploy_test
    If ErrorLevel 1 Echo Deploy test failed)

Rem Deploying to argos
Choice /M "Deploy to argos"
If "%ERRORLEVEL%"=="1" (Call :deploy_argos
    If ErrorLevel 1 Echo Deploy test failed)

Echo Press any key to exit...
Timeout /T -1 >NUL
GoTo :EOF

:deploy_test
Echo Deploying test!
Timeout /T 3 /NoBreak >NUL
Exit /B 0

:deploy_argos
Echo Deploying argos!
Timeout /T 3 /NoBreak >NUL
Exit /B 1

在上面的示例中,我使用了两种不同的退出代码,以模拟两个部署选项中每一个的返回错误级别。