Powershell If-Elseif-Else条件语句

Powershell is a really powerful scripting language. It provides  a lot of modern useful programming language features. It also have basic features like conditional statements like If . In this tutorial we will look syntax and use cases of If .

Powershell是一种非常强大的脚本语言。 它提供了许多现代有用的编程语言功能。 它还具有基本功能,例如条件语句(如If 。 在本教程中,我们将研究If的语法和用例。

If is used to check if specified condition/s are met and if the condition is met do something if not check other conditions or exit. We will look different usage types below.

如果用于检查是否满足指定的条件,以及是否满足条件,请执行其他操作(如果不检查其他条件)或退出。 我们将在下面介绍不同的用法类型。

句法 (Syntax)

Generic syntax of the if statement is like below. Other than first if line each line optional. There may be more than one elseif line

if语句的通用语法如下所示。 除了第一if行每行可选。 可能有不止一个elseif线

If (condition) {Block Command}

elseIf (condition) {Block Command}

...

else {Block Command}

单一条件 (Single Condition)

We will start with simplest usage type of If. We will only check a single condition. According to conditions status which is logical values true or false specified script will run or not. Now we will do an example.

我们将从If的最简单用法类型开始。 我们将只检查一个条件。 根据条件状态(逻辑值truefalse指定的脚本将运行或不运行。 现在我们将举一个例子。

In this example we will check if variable $a is bigger than 5 . If $a is bigger than 5 then we will print a message saying that $a value is bigger than 5.

在此示例中,我们将检查变量$a是否大于5。 如果$ a大于5,则我们将输出一条消息,指出$ a的值大于5。

$a=10

if ($a -gt 5){
Write-Host("$a is bigger then 5")
}

Now lets examine what this code mean.

现在让我们检查一下这段代码的含义。

  • $a=10 creates a numeric variable and assigns the value 10

    $a=10创建一个数字变量并将值分配为10

  • if ()  checks the statement between brackets and is the condition is true the code between curly brackets { Write-Host ("$a is bigger then 5")} is executed if not nothing will be executed

    if ()检查方括号之间的语句, if ()条件为true,则执行大括号之间的代码{ Write-Host ("$a is bigger then 5")}如果不执行任何操作,将执行

  • $a -gt 5 is the most important part -gt means greater then so if we try to read this statement variable a is greater then. This statement will be used as condition for if. In this example $a is 10 and this statement is true.

    $a -gt 5是最重要的部分-gt意味着更大,因此,如果我们尝试读取此语句,则变量a更大。 该语句将用作if的条件。 在此示例中,$ a为10,该语句为true。

LEARN MORE  Python Boolean Variable Types
了解更多Python布尔变量类型

多种条件(Multiple Conditions)

Previous example we have check single condition. But the real world consist of complex problems and we have to use complex and multiple conditions in decision making process. If can be used to provide multiple conditions by defining them in else lines.

在前面的示例中,我们检查了单个条件。 但是现实世界包含复杂的问题,我们在决策过程中必须使用复杂的多种条件。 通过在else行中定义If可以提供多个条件。

In this example we will check variable $a whether it is bigger than 10 or not.

在此示例中,我们将检查变量$a是否大于10。

$a=5

if ($a -gt 10){
Write-Host("$a is bigger then 10")
}
elseif ($a -le 10){
Write-Host("$a is lower then 10")
}
  • The if statement execution will start with first if line but the variable data is 5 and not bigger than 10. So this line will be skipped.

    if语句的执行将从第一条if行开始,但是变量数据为5并且不大于10。因此将跳过此行。

  • The if will resume with elseif statement and as we see that the ($a -le 10) condition will return true.

    if将使用elseif语句恢复,并且我们看到($a -le 10)条件将返回true。

  • Because of the elseif condition is true the code block of elseif will be executed.

    因为elseif条件为true,否则将执行elseif的代码块。

We can add more elseif conditions than one and all of them will have same syntax but different conditions. Here an other example which have more than one elseif

我们可以添加更多的elseif条件,而不是一个,并且所有条件将具有相同的语法,但条件不同。 这里,其具有多于一个的其它示例elseif

$a=5

if ($a -gt 10){
Write-Host("$a is bigger then 10")
}
elseif ($a -le 10){
Write-Host("$a is lower or equal to 10 and bigger than 0")
}
elseif ($a -lt 0){
Write-Host("$a is lower then 0")
}

其他 (Else)

Up to now we have defined our conditions explicitly by using if or elseif statements. What is we do not define the condition but if the previous conditions do not match we want to execute a code block? We call this else statement. Else is put as the last statement of the whole if block. If none of the previous conditions are met the last else code block is executed.

到目前为止,我们已经通过使用ifelseif语句明确定义了我们的条件。 我们没有定义条件是什么,但是如果先前条件不匹配,我们要执行代码块? 我们称此为else声明。 Else则作为整个if块的最后一个语句。 如果先前的条件都不满足,则执行最后的else代码块。

LEARN MORE  What is File System Block And Super Block?
了解更多什么是文件系统块和超级块?

In the example if the variable $a is not positive integer we will print a warning message to the console.

在示例中,如果变量$a不是正整数,我们将向控制台输出警告消息。

$a=-4

if ($a -gt 10){
Write-Host("$a is bigger then 10")
}
elseif ($a -ge 0){
Write-Host("$a is lower or equal to 10 and bigger than 0")
}
else{
Write-Host("$a is not positive integer")
}

翻译自: https://www.poftut.com/powershell-if-elseif-else-conditional-statement/

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值