如果Windows批处理/然后/ else语句 [英] if/then/else statements in Windows batch

查看:120
本文介绍了如果Windows批处理/然后/ else语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在shell脚本我有以下的code:

In a shell script I have the following code:

if echo Mr.32 ; then
  echo Success
else
  echo Failed
  exit
fi

什么是Windows批处理文件相当于语法?

What is the equivalent syntax for Windows batch files?

推荐答案

我有一个很难设想,当ECHO将失败,并返回的ERRORLEVEL不等于0,我想,如果输出已经被重定向到一个它可能失败文件和目标驱动器已满。

I'm having a hard time envisioning when ECHO would fail with a returned ERRORLEVEL not equal 0. I suppose it could fail if the output has been redirected to a file and the target drive is full.

CptHammer已发布使用ERRORLEVEL一个很好的解决方案,虽然它使用GOTO不必要的。它可以在不GOTO使用来完成:

CptHammer has posted a good solution using ERRORLEVEL, although it uses GOTO unnecessarily. It can be done without GOTO using:

ECHO Mr.32
if errorlevel 1 (
  echo Failed
  exit /b
) else (
  echo Success
)

有一个更简单的采取的任何命令的成功或失败的行动方式。

There is a simpler way to take action on SUCCESS or FAILURE of any command.

command && success action || failure action

在你的情况

ECHO Mr.32&& (
  echo Success
) || (
  echo Failed
  exit /b
)

这篇关于如果Windows批处理/然后/ else语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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