批处理脚本冒险

发布于 2024-11-28 05:00:52 字数 724 浏览 2 评论 0原文

这是我的批处理脚本文件。有两种情况


  • 情况 1

@echo off
set name=
set /P TypeName=Name: %=%

if %TypeName% == "abcd" goto correctName
else goto wrongName

:correctName
echo Correct Name
:end

:wrongName
echo Wrong Name
:end

当我输入 abcd 作为输入时,我得到 'else' 未被识别为内部或外部命令、可操作程序或批处理文件

名称错误


  • 场景 2

@echo off
set name=
set /P TypeName=Name: %=%

if %TypeName% EQA "abcd" goto correctName
if %TypeName% NEQ "abcd" goto wrongName

:correctName
echo Correct Name
:end

:wrongName
echo Wrong Name
:end

当我输入 abcd 作为输入时,我得到EQA is意外此时。

我的脚本中有什么问题吗?我在这里遗漏了什么吗

Here is my batch script file. There are 2 scenarios


  • Scenario 1

@echo off
set name=
set /P TypeName=Name: %=%

if %TypeName% == "abcd" goto correctName
else goto wrongName

:correctName
echo Correct Name
:end

:wrongName
echo Wrong Name
:end

When i type abcd as the input, i get 'else' is not recognized as an internal or external command,operable program or batch file

Wrong Name


  • Scenario 2

@echo off
set name=
set /P TypeName=Name: %=%

if %TypeName% EQA "abcd" goto correctName
if %TypeName% NEQ "abcd" goto wrongName

:correctName
echo Correct Name
:end

:wrongName
echo Wrong Name
:end

When i type abcd as the input, i get EQA was unexpected at this time.

Is there something wrong in my script?Am I missing something here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

薔薇婲 2024-12-05 05:00:52
  1. ELSE 应与 IF 关键字位于同一行,或与属于 IF 的右括号位于同一行。

    像这样:

    IF %TypeName% == "abcd" 转到正确名称 ELSE 转到错误名称
    

    或者像这样:

    IF %TypeName% == "abcd" (
      回声 正确。
      转到正确的名称
    ) ELSE GOTO 错误名称
    
  2. Equal 运算符的正确关键字是 EQU

    IF %TypeName% EQU "abcd" GOTO 正确名称
    
  1. ELSE should be on the same line with the IF keyword or on the same line with the closing bracket that pertains to the IF.

    Like this:

    IF %TypeName% == "abcd" GOTO correctName ELSE GOTO wrongName
    

    Or like this:

    IF %TypeName% == "abcd" (
      ECHO Correct.
      GOTO correctName
    ) ELSE GOTO wrongName
    
  2. The correct keyword for the Equal operator is EQU:

    IF %TypeName% EQU "abcd" GOTO correctName
    
红玫瑰 2024-12-05 05:00:52

第一个示例几乎是正确的,只是批处理文件中 IF/ELSE 语句的格式如下:

IF <statement>  (
..
..
) ELSE (
...
...
)

因此,只要您使用该格式,它就应该可以工作。

The first example is almost right, except that the format of an IF/ELSE statement in a batch file is as follow:

IF <statement>  (
..
..
) ELSE (
...
...
)

So just you use that format and it should work.

戏蝶舞 2024-12-05 05:00:52

你不一定需要使用 else,就像这样

@echo off
set name=
set /P TypeName=Name: %=%

if %TypeName% == "abcd" goto correctName
goto wrongName

:correctName
echo Correct Name
:end

:wrongName
echo Wrong Name
:end

如果 %TypeName% == "abcd" 它将跳转到 : CorrectName,如果不是,它将简单地落到下一行并跳转到 :wrongName。

You shouldn't necessarily need to use the else, like this

@echo off
set name=
set /P TypeName=Name: %=%

if %TypeName% == "abcd" goto correctName
goto wrongName

:correctName
echo Correct Name
:end

:wrongName
echo Wrong Name
:end

If the %TypeName% == "abcd" it will jump to :correctName, if it doesn't it will simply fall to the next line and jump to :wrongName.

等你爱我 2024-12-05 05:00:52

为了结束这篇文章,我以这种方式得到了预期的输出-

@echo off
set name=
set /P TypeName=Name: %=%

if "%TypeName%" == "abcd" (
echo Correct Name
) else (
echo Wrong Name
)

To give an end to this post,i got the expected output this way-

@echo off
set name=
set /P TypeName=Name: %=%

if "%TypeName%" == "abcd" (
echo Correct Name
) else (
echo Wrong Name
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文