批量比较两个字符串 [英] Comparing two strings in batch
问题描述
当前,我正在编写一个批处理文件以接受参数,并根据参数值跳转到文件的特定部分.我的代码如下:
Currently I am writing a batch file to accept a parameter and jump to a specific part of the file based on the parameter value. The code I have is as follows:
SET param = %~n1
IF "%param%"=="q" GOTO :QUICK
IF "%param%"=="t" GOTO :THOROUGH
IF "%param%"=="b" GOTO :BOOT
IF "%param%"=="f" GOTO :FIX
IF "%param%"=="" GOTO :INVALID
:QUICK
echo "Quick"
pause
exit
:INVALID
echo "Invalid"
pause
exit
:BOOT
echo "Boot"
pause
exit
:FIX
echo "Fix"
pause
exit
:THOROUGH
echo "Thorough"
pause
exit
脚本唯一可以接受的参数是/q,/t,/b或/f
(或不带/
),并且当提供了错误的参数时,它应该跳到 INVALID
.当我使用无效的参数运行它时,它可以工作,但是,如果我提供了正确的参数(例如, file.bat t
),结果将无法正常运行,并最终直接转到 INVALID
.我尝试使用%param%" EQ"q"
,并在进行比较之前将 q
设置为变量,但是我没有运气得出结果这样可行.
The only parameters the script should accept are /q, /t, /b or /f
(or without /
), and when an incorrect parameter is supplied, it should jump to INVALID
. When I run it with an invalid parameter, it works, however, if I supply a parameter that is correct (for example, file.bat t
) the result fails to work as I hope, and ends up going directly to INVALID
. I have tried using "%param%" EQ "q"
, and setting q
to a variable before doing the comparison, but I have had no luck coming up with a result that works.
推荐答案
您没有参数%param%
.但是您有一个名为%param< space>%
的参数,它的值不是 q
,而是< space> q
you don't have a parameter %param%
. But you have a parameter named %param<space>%
And it's value isn't q
, but <space>q
编写您的 set
命令,不要带空格:
Write your set
command without spaces:
set param=%~n1
甚至更好(以避免在末尾出现意想不到的空格):
or even better (to avoid unintended spaces at the end):
set "param=%~n1"
注意:如果/i%param%" =="q"
Note: you can make your parameters case independent with if /i "%param%"=="q"
这篇关于批量比较两个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!