【发布时间】:2017-02-01 08:36:33
【问题描述】:
我在编写批处理脚本时总是遇到这个问题。每当我让脚本提示用户设置变量时,如果输入了分号(这只是一个示例),则脚本将关闭。有没有办法阻止它这样做?
例子:
@echo off
:1
cls
echo Please enter your ID:
set /P id=
if /i %id%==119 goto Tom
if /i %id%==204 goto Brittany
if /i %id%==12 goto Shannon
if /i %id%==64 goto Jack
goto 1
:Tom
cls
echo Tom, you have to mow the lawn.
pause>nul
exit
:Brittany
cls
echo Brittany, you have to fold the laundry.
pause>nul
exit
:Shannon
cls
echo Shannon, you have to feed the dog.
pause>nul
exit
:Jack
cls
echo Jack, you have to replace the radio's capacitors.
pause>nul
exit
我会看到运行该脚本的内容:
C:\>myscript.bat
Please enter your ID:
asjfash;dfjlas;ldf
asjfash;dfjlas;ldf==i was unexpected at this time.
脚本关闭。
谢谢!
【问题讨论】:
-
请edit您的问题并分享您的失败代码,按照如何创建minimal reproducible example。
-
抱歉,JosefZ,我还是新手。
-
显示 result 是正确且必要的,但还不够;也显示你失败的代码。阅读Syntax : Escape Characters, Delimiters and Quotes。当然,您需要转义分号,例如就像在这个例子中:
if /i "%varname%"=="literal;string" call :labelE -
最诚挚的歉意,我已经添加了一些示例代码。
标签: windows batch-file variables scripting