我有两个关于我正在处理的批处理脚本的问题。我意识到批处理脚本问题很常见,但还没有找到我确切问题的答案,所以我想我会试着问一下。有问题的区域是菜单上的用户输入部分。
有两个问题:1)输入的不是指定选项之一会导致脚本跳转到随机区域。并且 2) 即使我知道语法和标志的使用通常是正确的(例如,我可以手动运行它们......所以由于某种原因输入是' t 捕获它们)。
第一期示例:
:MenuOne
echo Select one of the following options:
echo 1) x
echo 2) y
echo Q) Quit
set INPUT=
set /P INPUT=[1,2,Q]: %=%
If "%INPUT%"=="1" goto xoption
If "%INPUT%"=="2" goto yoption
If /I "%INPUT%"=="Q" goto Quit
:xoption
@REM Here goes a lot more submenus and/or options that actually run tools via cmd.
:yoption
@REM Again, menus and/or tools being invoked, in a listed menu, designed like above.
:Quit
echo Quitting...
exit
如果用户在选择提示中键入“b”,我希望脚本给出错误并重复菜单。相反,它会在其他菜单周围乱跳。我猜我需要一些 ELSE 语句?谁有我可以用来完成此任务的示例?
一些命令的第二个问题没有正确使用 %input% 并返回一个错误,就好像它从未收到过 %input%。
set /P INPUT=[Testone Input]: %testone%
set /P INPUT=[Testtwo Input]: %testtwo%
commandtorun.exe -f %testone% -h %testtwo%
谢谢!
最佳答案
最好使用 choice
( http://ss64.com/nt/choice.html ),因为它会一直存在,直到您设置正确的输入为止
CHOICE /C XYQ /M "Select of the following options [X,Y,Q]"
if errorlevel 1 goto :x
if errorlevel 2 goto :y
uf errorlevel 3 goto :q
但是仍然可以用 IF 来完成
set INPUT=
set /P INPUT=[1,2,Q]: %=%
If "%INPUT%"=="1" goto xoption
If "%INPUT%"=="2" goto yoption
If /I "%INPUT%"=="Q" goto Quit
rem -- will be executed only if the all the above are not true
goto :eof
对于第二个问题..你没有正确使用SET/P
(变量的名字应该在前面),或者你正在尝试一些我不明白的东西(在哪里使用输入变量):
set /P testone=[Testone Input]:
set /P testtwo=[Testtwo Input]:
commandtorun.exe -f %testone% -h %testtwo%
关于if-statement - 批处理脚本输入问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16575479/