如何检查一个参数定义调用批处理文件时? [英] How do I check that a parameter is defined when calling a batch file?
问题描述
我尝试使用下面的验证逻辑在一个批处理文件,但即使在没有参数提供给批处理文件中的用法块从不执行。
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
我在做什么错了?
What am I doing wrong?
推荐答案
对于一个命令行参数是否已经设置可的 [%1] == [] 的,但正如戴维哥斯达黎加<支票一个href=\"http://stackoverflow.com/questions/830565/how-do-i-check-that-a-parameter-is-defined-when-calling-a-batch-file/830593#830593\">points出,的%1==的也可以工作。
The check for whether a commandline argument has been set can be [%1]==[] but, as Dave Costa points out, "%1"=="" will also work.
我也固定在使用回声语法错误逃离大于和小于的迹象。此外,退出需要一个/ 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屋!