用户输入 .bat 脚本时出现问题

发布于 2024-10-08 03:14:07 字数 1345 浏览 0 评论 0原文

我编写了以下 .bat 脚本

@echo off
@rem set variables
set CURRENT_DIR=%cd%
set SCRIPT_DIR=%~dp0%
set Program Files=%Program Files:Program Files=Progra~1%

:input
set INPUT=
echo Following are the installation choices you have:
echo        1. Choice1
echo        2. Choice2
echo        3. Choice3

set /P INPUT=Please select any of the above choice (1to 3) : %=%
echo input is %INPUT%
if "%INPUT%"=="" goto input

if "%INPUT%"=="1" (
    echo Choice 1 selected
    goto QUITNOW
)

if "%INPUT%"=="2" (
echo Choice 2 selected
set /p INPUT_1=Please enter input 1: %=%
echo input_1 is  %INPUT_1%
goto QUITNOW
)

if "%INPUT%"=="3" (
echo Choice 3 selected
set /p input_3= Please enter input_3: %=%
echo %input_3%
)

:QUITNOW
cd %CURRENT_DIR%
@echo on

首先,当它要求我选择选项并且我输入 2 时,它将 INPUT 变量设置为 2 并进入第二个 if 块。在 if 块中,它再次要求输入(INPUT_1),我确实输入了输入单词,但看起来这次没有设置 INPUT_1 变量。有人知道我哪里做错了吗?使用我的输入跟踪脚本的输出

C:\Documents and Settings\Administrator\Desktop>scriptA.bat
Following are the installation choices you have:
        1. Choice1
        2. Choice2
        3. Choice3
Please select any of the above choice (1to 3) : 2
input is 2
Choice 2 selected
Please enter input 1: failingHere
input_1 is

C:\Documents and Settings\Administrator\Desktop>

因此,如果您观察到它没有打印“failingHere”输入。如果您需要有关此内容的更多信息,请帮助并告诉我。

I wrote the following .bat script

@echo off
@rem set variables
set CURRENT_DIR=%cd%
set SCRIPT_DIR=%~dp0%
set Program Files=%Program Files:Program Files=Progra~1%

:input
set INPUT=
echo Following are the installation choices you have:
echo        1. Choice1
echo        2. Choice2
echo        3. Choice3

set /P INPUT=Please select any of the above choice (1to 3) : %=%
echo input is %INPUT%
if "%INPUT%"=="" goto input

if "%INPUT%"=="1" (
    echo Choice 1 selected
    goto QUITNOW
)

if "%INPUT%"=="2" (
echo Choice 2 selected
set /p INPUT_1=Please enter input 1: %=%
echo input_1 is  %INPUT_1%
goto QUITNOW
)

if "%INPUT%"=="3" (
echo Choice 3 selected
set /p input_3= Please enter input_3: %=%
echo %input_3%
)

:QUITNOW
cd %CURRENT_DIR%
@echo on

At first when it ask me to select the choice and i entered 2,it set the INPUT variable to 2 and it goes in second if block. In if block again it ask for the input (INPUT_1),i do enter the input word but looks like this time its not setting the INPUT_1 variable.Do anyone have any idea where am i doing wrong. Following the the output of the script with my inputs

C:\Documents and Settings\Administrator\Desktop>scriptA.bat
Following are the installation choices you have:
        1. Choice1
        2. Choice2
        3. Choice3
Please select any of the above choice (1to 3) : 2
input is 2
Choice 2 selected
Please enter input 1: failingHere
input_1 is

C:\Documents and Settings\Administrator\Desktop>

So if you observe its not printing the "failingHere" input. Please help and let me know if you need any further information regarding this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

东风软 2024-10-15 03:14:07

您的问题在于:

if "%INPUT%"=="2" (
    echo Choice 2 selected
    set /p INPUT_1=Please enter input 1: %=%
    echo input_1 is  %INPUT_1%
    goto QUITNOW
)

在运行该整个块(对于环境变量)之前,任何都会被评估。这意味着 %INPUT_1% 在设置之前先进行评估。

您需要做的是将:

setlocal enableextensions enabledelayedexpansion

放在脚本的顶部(以及退出之前的endlocal)并使用延迟扩展符号:

if "%INPUT%"=="2" (
    echo Choice 2 selected
    set /p INPUT_1=Please enter input 1: %=%
    echo input_1 is  !INPUT_1!
    goto QUITNOW
)

您可以使用以下脚本查看此操作:

@setlocal enableextensions enabledelayedexpansion
@echo off
set xx=1
if %xx%==1 (
    set xx=2
    echo %xx%
    echo !xx!
)
endlocal

输出:

1
2

因为%xx% 在读取整个 if 语句 时计算,而 !xx! 在读取整个 if 语句 时计算被执行。

Your problem lies here:

if "%INPUT%"=="2" (
    echo Choice 2 selected
    set /p INPUT_1=Please enter input 1: %=%
    echo input_1 is  %INPUT_1%
    goto QUITNOW
)

This entire block is evaluated (for environment variables) before any of it is run. That means %INPUT_1% is evaluated before being set.

What you need to do is put:

setlocal enableextensions enabledelayedexpansion

at the top of your script (and endlocal before exiting) and use the delayed expansion symbols thus:

if "%INPUT%"=="2" (
    echo Choice 2 selected
    set /p INPUT_1=Please enter input 1: %=%
    echo input_1 is  !INPUT_1!
    goto QUITNOW
)

You can see this in action with the following script:

@setlocal enableextensions enabledelayedexpansion
@echo off
set xx=1
if %xx%==1 (
    set xx=2
    echo %xx%
    echo !xx!
)
endlocal

which outputs:

1
2

because %xx% is evaluated at the time the entire if statement is read while !xx! is evaluated when that line is executed.

童话 2024-10-15 03:14:07

您使用了 ms-dos/dos-batch 标签,但我不认为 set /p 在 MS-DOS 中已经可用。我以为它被添加到Windows命令提示符(cmd)中。
如果没有,如果我当时就知道的话,我的基于批处理的文本冒险就会容易得多。 :)

You used the ms-dos/dos-batch tags, but I don't think set /p is already available in MS-DOS. I thought it to be added to the Windows command prompt (cmd).
If not, it would have made my batch-based text adventure a lot easier if I had known it back then. :)

心安伴我暖 2024-10-15 03:14:07

您使用的是 /p 而不是 /P。改变它就会起作用。

if "%INPUT%"=="2" (
echo Choice 2 selected
set /P INPUT_1=Please enter input 1: %=%
echo input_1 is  %INPUT_1%
goto QUITNOW
)

You're using /p and not /P. Change that and will works.

if "%INPUT%"=="2" (
echo Choice 2 selected
set /P INPUT_1=Please enter input 1: %=%
echo input_1 is  %INPUT_1%
goto QUITNOW
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文