如何在启动/调用批处理文件时检查是否定义了参数? [英] How can I check if an argument is defined when starting/calling a batch file?

查看:20
本文介绍了如何在启动/调用批处理文件时检查是否定义了参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在批处理文件中使用以下验证逻辑,但即使没有向批处理文件提供参数,使用"块也永远不会执行.

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.

我还修复了使用 echo 中的语法错误以转义大于号和小于号.此外,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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆