Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Batch file 调试Windows命令行语法错误_Batch File_Command Line_Syntax Error - Fatal编程技术网

Batch file 调试Windows命令行语法错误

Batch file 调试Windows命令行语法错误,batch-file,command-line,syntax-error,Batch File,Command Line,Syntax Error,我正在学习Windows命令行课程,我收到了一条关于批处理文件的错误消息,我不理解。我应该创建一个批处理文件,该文件接受参数并打印它们是否是环境变量。这就是我到目前为止所做的: :: Disable echoing of batch file commands to console. @echo off rem ********Begin Header****************** :: Author: Megan :: Date: 04/08/2018 :: Fil

我正在学习Windows命令行课程,我收到了一条关于批处理文件的错误消息,我不理解。我应该创建一个批处理文件,该文件接受参数并打印它们是否是环境变量。这就是我到目前为止所做的:

::  Disable echoing of batch file commands to console.
@echo off

rem ********Begin Header******************
::  Author:     Megan
::  Date:   04/08/2018
::  File:   checkVars.bat
::  Descr:
::  This script determines if a given
::  set of arguments are defined
::  environmental variables.
rem ********End Header********************

::  Check to make sure at least one command
::  line argument has been given. If not,
::  display a usage message and exit the 
::  batch file. 
if "%1" == "" (
   echo Usage: %0 varname1 ...
   echo Determines if variable name is defined
   exit /b 1
)

:: determine if arguments %1 and greater
:: are environmental variables
:again
if not "%1" == "" (
   if defined %1 (
      echo %1 is a defined environment variable.
      echo.
   ) else (
      echo %1 is NOT a defined environment variable.
      echo.
   )
   shift /1
   goto again
) 
当我使用命令checkVars windir mydir prompt myvar运行批处理文件时,我得到以下输出:

windir is a defined environmental variable.

mydir is NOT a defined environmental variable.

prompt is a defined environmental variable.

myvar is NOT a defined environmental variable.

The syntax of the command is incorrect.

看起来我的代码额外运行了一次。有人能帮我指出错误的方向吗?

查看代码后,我注意到您需要用引号括起一些部分。导致语法错误的主要原因是
(如果定义为%1
),其中
%1
需要变成
%1
。希望这能解决你的问题

更正代码:

::  Disable echoing of batch file commands to console.
@echo off

rem ********Begin Header******************
::  Author:     Megan
::  Date:   04/08/2018
::  File:   checkVars.bat
::  Descr:
::  This script determines if a given
::  set of arguments are defined
::  environmental variables.
rem ********End Header********************

::  Check to make sure at least one command
::  line argument has been given. If not,
::  display a usage message and exit the 
::  batch file. 
if "%1" == "" (
   echo Usage: %0 varname1 ...
   echo Determines if variable name is defined
)

:: determine if arguments %1 and greater
:: are environmental variables
:again
if not "%1" == "" (
   if defined "%1" (
      echo %1 is a defined environment variable.
      echo.
   ) else (
      echo %1 is NOT a defined environment variable.
      echo.
   )
   shift /1
   goto again
)