启动/调用批处理文件时,如何检查是否定义了参数? [英] How can I check if an argument is defined when starting/calling a batch file?
本文介绍了启动/调用批处理文件时,如何检查是否定义了参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图在批处理文件中使用以下验证逻辑,但是即使未向批处理文件提供任何参数,"usage"块也永远不会执行.
I'm trying to use the following validation logic in a batch file but the "usage" block never executes even when no parameter is supplied to the batch file.
if ("%1"=="") goto usage
@echo This should not execute
@echo Done.
goto :eof
:usage
@echo Usage: %0 <EnvironmentName>
exit 1
我在做什么错了?
推荐答案
检查是否已设置命令行参数可以为[%1]==[]
,但可以为指出,"%1"==""
也将起作用.
The check for whether a commandline argument has been set can be [%1]==[]
, but, as Dave Costa points out, "%1"==""
will also work.
我还修复了用法回显中的语法错误,以避开大于和小于符号.另外,exit
需要一个/B
参数,否则CMD.exe
将会退出.
I also fixed a syntax error in the usage echo to escape the greater-than and less-than signs. In addition, the exit
needs a /B
argument otherwise CMD.exe
will quit.
@echo off
if [%1]==[] goto usage
@echo This should not execute
@echo Done.
goto :eof
:usage
@echo Usage: %0 ^<EnvironmentName^>
exit /B 1
这篇关于启动/调用批处理文件时,如何检查是否定义了参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文