批处理文件中的 IF ELSE 语法错误? [英] IF ELSE syntax error within batch file?

查看:28
本文介绍了批处理文件中的 IF ELSE 语法错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是批处理文件写入的新手,我正在编写一个脚本,该脚本随机打开三个网页之一并在延迟后循环.我在运行它时经常遇到语法错误,但我无法确定它在哪里.

I'm new to batch file writing and I'm writing a script that randomly opens one of three web pages and loops after a delay. I constantly get a syntax error when I run it, but I can't pinpoint where it is.

:main
@echo on
set location=""
set /A num=%random% %% 10 
if /A"%num%"=="0"
(
    set location="yahoo.com"
) 
else if /A"%num%"=="1"
(
    set location="msn.com"
)
else
(
    set location="google.com"
)

start "Chrome" chrome --new-window %location%
timeout /t 30 /nobreak >NUL
goto main

非常感谢您的帮助,并提前致谢!

Help would be much appreciated, and thanks in advance!

推荐答案

这里是根据 MC NDMagoo:

@echo off
:main
set location=
set /A num=%random% %% 10
if "%num%"=="0" (
   set "location=yahoo.com"
) else if "%num%"=="1" (
   set "location=msn.com"
) else (
   set "location=google.com"
)
start "Chrome" chrome.exe --new-window "%location%"
timeout /t 30 /nobreak >NUL
goto main

这是遵循 Ed Heal 建议的批处理代码:

And here is the batch code following advice of Ed Heal:

@echo off
:main
set location=
set /A num=%random% %% 10
if "%num%"=="0" set "location=yahoo.com" & goto OpenSite
if "%num%"=="1" set "location=msn.com"   & goto OpenSite
set "location=google.com"
:OpenSite
start "Chrome" chrome.exe --new-window "%location%"
timeout /t 30 /nobreak >NUL
goto main

请参阅使用 Windows 批处理文件的具有多个命令的单行,了解代码 set "location=..." &转到 OpenSite.

此额外信息适用于 rudicangiotti,因为他在 Middas.

This extra information is for rudicangiotti because of his comment below answer written by Middas.

ifelse 没有必要在一个命令中使用括号.

It is not necessary for if and else with just a single command to use parentheses.

因此代码块

if "%num%"=="0" (
   set "location=yahoo.com"
) else if "%num%"=="1" (
   set "location=msn.com"
) else (
   set "location=google.com"
)

被解析为

if "%num%"=="0" (
   set "location=yahoo.com"
) else (
   if "%num%"=="1" (
      set "location=msn.com"
   ) else (
      set "location=google.com"
   )
)

也许这用 C/C++ 更容易理解,它不需要 else 与命令 if 或属于匹配 if 的右括号在同一行代码>如果.

Perhaps this is easier to understand with C/C++ which does not require that else is on same line as command if or the closing parenthesis which belongs to the matching if.

完整的可编译C/C++示例代码:

A complete compilable C/C++ example code:

#ifdef __cplusplus
#include <cstdio>    /* printf */
#include <cstdlib>   /* rand   */
#include <cstring>   /* strcmp */
#else
#include <stdio.h>   /* printf */
#include <stdlib.h>  /* rand   */
#include <string.h>  /* strcmp */
#endif

int main (int argc, char* argv[])
{
   const char* sLocation;
   int iNum = rand() % 10;

   if(iNum == 0) sLocation = "yahoo.com";
   else if(iNum == 1) sLocation = "msn.com";
   else sLocation = "google.com";
   printf("Number is %d and location is "%s".
",iNum,sLocation);

   /* Some not really useful code to avoid warnings. */
   if(argc > 1)
   {
      if(!strcmp(argv[1],"/?"))
      {
         printf("There is no help for this small demo application.
");
      }
   }
   return 0;
}

同样在 C/C++ 中,else if 语句没有关键字作为 Visual Basic 中的 ElseIf 关键字或 #elif 指令预处理器.

Also in C/C++ there is no keyword for an else if statement as the ElseIf keyword in Visual Basic or #elif directive of the preprocessor.

因此上面的条件块也可以写成:

Therefore the above condition block could be written also as:

/* Variant 1: Same usage of brackets and indents like in first batch example. */
if(iNum == 0) {
   sLocation = "yahoo.com";
} else if(iNum == 1) {
   sLocation = "msn.com";
} else {
   sLocation = "google.com";
}

/* Variant 2: Same usage of brackets like in first batch example,
   but this time with indents as it would be 100% correct according
   to processing. It is not possible to use this syntax in batch
   files because the second if must be on same line as first else. */
if(iNum == 0) {
   sLocation = "yahoo.com";
} else
   if(iNum == 1) {
      sLocation = "msn.com";
   } else {
      sLocation = "google.com";
   }

/* Variant 3: Same usage of brackets like in second batch example,
   but without omitting not necessary brackets for first else block. */
if(iNum == 0) {
   sLocation = "yahoo.com";
} else {
   if(iNum == 1) {
      sLocation = "msn.com";
   } else {
      sLocation = "google.com";
   }
}

/* Variant 4: One more variant not possible in batch file,
   but using a very common style for C/C++ programmers. */
if(iNum == 0)
{
   sLocation = "yahoo.com";
}
else if(iNum == 1)
{
   sLocation = "msn.com";
}
else
{
   sLocation = "google.com";
}

/* Variant 5: This is variant 3 in coding style of variant 4. */
if(iNum == 0)
{
   sLocation = "yahoo.com";
}
else
{
   if(iNum == 1)
   {
      sLocation = "msn.com";
   }
   else
   {
      sLocation = "google.com";
   }
}

这个代码块可以用 C/C++ 编写,有更多的变体,考虑到括号和缩进的不同风格.

This code block could be written in C/C++ with a lot more variants taking the different styles for brackets and indents into account.

对于批处理文件编码来说,真正有趣的是变体 1 到变体 3,其中变体 2 显示了变体 1 如果可能在批处理文件中的实际外观.但是没有人括号和缩进 else if 块,如变体 3 所示,还有几个 else if 块插入最后一个 ifelse 将被定位到最右边.

Really interesting for batch file coding are the variants 1 to 3 whereby variant 2 shows how variant 1 should really look like if it would be possible in batch files. But nobody brackets and indents else if blocks as shown in variant 3 as with several more else if blocks inserted the last if and else would be positioned thus far to right.

这篇关于批处理文件中的 IF ELSE 语法错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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