我可以省略的部分,然后在if语句? [英] Can I omit the then part in an if statement?

查看:130
本文介绍了我可以省略的部分,然后在if语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将是正确的bash语法是这样的:

How would be the correct bash syntax for something like this:

if [ "$actual" == "$expected" ]; then 
  doNothing
else 
  echo "Error: actual: $actual. Expected: $expected"
fi

我要寻找的东西,对于变量的实际和预期的所有可能值的作品。变量的含量应不PTED间$ P $ /评估/以任何方式扩大。该脚本并不需要移植(一个bash唯一的解决办法是确定)。

I am looking for something that works for all possible values of the variables "actual" and "expected". The content of the variables must not be interpreted/evaluated/expanded in any way. The script does not need to be portable (a bash only solution is ok).

推荐答案

您可以用最简单的可用什么也不做的语句:

You could use the simplest do-nothing statement available:

if [ "$actual" = "$expected" ]; then 
  :
else 
  echo "Error: actual: $actual. Expected: $expected"
fi

(注意:一个 = 不是两个 [ / 测试

但是,一个更好的主意是刚刚反转测试并删除需要一种完全

But a better idea is to just invert the test and remove the need for that entirely:

if [ "$actual" != "$expected" ]; then
  echo "Error: actual: $actual. Expected: $expected"
fi

这篇关于我可以省略的部分,然后在if语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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