用户输入 .bat 脚本时出现问题
我编写了以下 .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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的问题在于:
在运行该整个块(对于环境变量)之前,任何都会被评估。这意味着
%INPUT_1%
在设置之前先进行评估。您需要做的是将:
放在脚本的顶部(以及退出之前的
endlocal
)并使用延迟扩展符号:您可以使用以下脚本查看此操作:
输出:
因为
%xx%
在读取整个if
语句 时计算,而!xx!
在读取整个if
语句 时计算行被执行。Your problem lies here:
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:
at the top of your script (and
endlocal
before exiting) and use the delayed expansion symbols thus:You can see this in action with the following script:
which outputs:
because
%xx%
is evaluated at the time the entireif
statement is read while!xx!
is evaluated when that line is executed.您使用了 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. :)
您使用的是 /p 而不是 /P。改变它就会起作用。
You're using /p and not /P. Change that and will works.