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

查看:75
本文介绍了批处理文件中是否存在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 ND Magoo :

@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

有关代码set "location=..." & goto OpenSite的说明,请参见使用Windows批处理文件的单行包含多个命令.

See Single line with multiple commands using Windows batch file for an explanation of code set "location=..." & goto 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\".\n",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.\n");
      }
   }
   return 0;
}

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

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的样子.但是没有人将方括号3和缩进的else if块如变体3所示,因为在最后一个ifelse插入了多个else if块之后,它们的位置因此居右.

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.

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

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