如何在Shell脚本中使⽤if-else?
Moving ahead from our previous tutorial on , let’s understand how we can use if-else in shell scripts.
从上⼀章有关教程开始,让我们了解如何在shell脚本中使⽤if-else。
Conditional programming is an important part of any programming language because executing every single statement in our program is, more often than not, undesirable.
条件编程是任何编程语⾔的重要组成部分,因为执⾏我们程序中的每条语句通常都是不希望的。
And we need a way to conditionally execute statements. The if-else in shell scripts serves this exact situation.
我们需要⼀种有条件地执⾏语句的⽅法。 shell脚本中的if-else可以满⾜这种情况。
Shell脚本中的条件 (Conditions in Shell Scripts)
One of the most important parts of conditional programming is the if-else statements. An if-else statement allows you to execute iterative conditional statements in your code.
条件编程的最重要部分之⼀是if-else语句。 if-else语句允许您在代码中执⾏迭代条件语句。
We use if-else in shell scripts when we wish to evaluate a condition, then decide to execute one set between two or more sets of statements using the result.
当我们希望评估条件时,我们在外壳脚本中使⽤if-else,然后决定使⽤结果在两组或更多组语句之间执⾏⼀组。
This essentially allows us to choose a response to the result which our conditional expression evaluates to.
从本质上讲,这使我们可以选择对条件表达式求值结果的响应。
shell脚本中的if-else如何⼯作? (How does if-else in shell scripts work?)
Now we know what is an if-else function and why is it important for any programmer, regardless of their domain. To understand if-else in shell scripts, we need to break down the working of the conditional function.
现在我们知道什么是if-else函数,以及为什么它对任何程序员都重要,⽆论其领域如何。 要了解shell脚本中的if-else,我们需要分解条件函数的⼯作。
Let us have a look at the syntax of the if-else condition block.
让我们看⼀下if-else条件块的语法。
if [condition]
then
statement1
shell代码else
statement2
fi
Here we have four keywords, namely if, then, else and fi.
这⾥我们有四个关键字,即if,then,else和fi。
1. The keyword if is followed by a condition.
关键字if后跟⼀个条件 。
2. This condition is evaluated to decide which statement will be executed by the processor.
评估该条件以确定处理器将执⾏哪个语句。
3. If the condition evaluates to TRUE, the processor will execute the statement(s) followed by the keyword then. In the
syntax, it is mentioned as statement1.
如果条件评估为TRUE,则处理器将执⾏语句,然后执⾏关键字then 。 在语法中,它被称为statement1 。
4. In a case where the condition evaluates to FALSE, the processor will execute the statement(s) followed by the keyword
else. This is denoted as statement2 in the function syntax.
在条件评估为FALSE的情况下,处理器将执⾏语句,然后执⾏关键字else 。 这在函数语法中表⽰为statement2 。
An important thing to keep in mind is that, like C programming, shell scripting is case sensitive. Hence, you need to be careful while using the keywords in your code.
要记住的重要⼀点是,与C编程⼀样,shell脚本也区分⼤⼩写。 因此,在代码中使⽤关键字时需要⼩⼼。
如何在shell脚本中使⽤if-else (How to use if-else in shell script)
It is easy to see the syntax of a function and believe you know how to use it. But it is always a better choice to understand a function through examples because they help you understand the role that different aspects of a function play.
很容易看到函数的语法,并相信您知道如何使⽤它。 但是,通过⽰例理解函数始终是⼀个更好的选择,因为它们可以帮助您理解函数的不同⽅⾯所扮演的⾓⾊。
Here are some useful examples of if-else in shell scripts to give you a better idea of how to use this tool.
以下是⼀些有⽤的shell脚本中if-else⽰例,可让您更好地了解如何使⽤此⼯具。
Command Description
&&Logical AND
||Logical OR
$0Argument the command that’s used to run the script
$1First argument (change number to access further arguments)
-eq Equality check
-ne Inequality check
-lt Less Than
-le Less Than or Equal
-gt Greater Than
-ge Greater Than or Equal
命令描述
&&逻辑与
||逻辑或
$ 0参数0,即⽤于运⾏脚本的命令
$ 1第⼀个参数(更改编号以访问其他参数)
-eq平等检查
-ne不平等检查
-lt少于
-le⼩于或等于
-gt⽐...更棒
-ge⼤于或等于
1.使⽤if-else检查两个数字是否相等 (1. Using if-else to check whether two numbers are equal)
When trying to understand the working of a function like if-else in a shell script, it is good to start things simple. Here, we initialize two variables a and b, then use the if-else function to check if the two variables are equal. The bash script should look as follows for this task.
当试图了解shell脚本中if-else之类的功能的⼯作时,最好从简单的⾓度开始。 在这⾥,我们初始化两个变量a和b,然后使⽤if-else函数检查两个变量是否相等。 此任务的bash脚本应如下所⽰。
#!/bin/bash
m=1
n=2
if [ $n -eq $m ]
then
echo "Both variables are the same"
else
echo "Both variables are different"
fi
Output:
输出:
Both variables are different
2.使⽤if-else⽐较两个值 (2. Using if-else to compare two values)
The more common use of if-else in shell scripts is for comparing two values. Comparing a variable against another variable or a fixed value helps is used in a variety of cases by all sorts of programmers.
shell脚本中if-else的更常见⽤法是⽐较两个值。 将变量与另⼀个变量或固定值进⾏⽐较会在各种情况下被各种程序员使⽤。
For the sake of this example, we will be initializing two variables and using the if-else function to find the variable which is greater than the other.
为了这个⽰例,我们将初始化两个变量,并使⽤if-else函数查⽐另⼀个⼤的变量。
#!/bin/bash
a=2
b=7
if [ $a -ge $b ]
then
echo "The variable 'a' is greater than the variable 'b'."
else
echo "The variable 'b' is greater than the variable 'a'."
fi
Output:
输出:
The variable 'b' is greater than the variable 'a'.
3.使⽤if-else检查数字是否为偶数 (3. Using if-else to check whether a number is even)
Sometimes we come across situations where we need to deal with and differentiate between even and odd numbers. This can be done with if-else in shell scripts if we take the help of the modulus operator.
有时我们遇到需要处理和区分偶数和奇数的情况。 如果我们借助模数运算符,可以在shell脚本中使⽤if-else完成此操作。
The modulus operator divides a number with a divisor and returns the remainder.
模运算符将数字除以除数,然后返回余数。
As we know all even numbers are a multiple of 2, we can use the following shell script to check for us whether a number is even or odd.
众所周知,所有偶数都是2的倍数,我们可以使⽤以下shell脚本来检查数字是偶数还是奇数。
#!/bin/bash
n=10
if [ $((n%2))==0 ]
then
echo "The number is even."
else
echo "The number is odd."
fi
Output:
输出:
The number is even
As you can see, we’ve enclosed a part of the condition within double brackets. That’s because we need the modulus operation to be performed before the condition is checked.
如您所见,我们已经将条件的⼀部分括在双括号中。 这是因为我们需要在检查条件之前执⾏模运算。
Also, enclosing in double brackets runs statements in C-style allowing you to process some C-style commands within bash scripts.
另外,⽤双括号括起来可以使⽤C风格的语句,使您可以在bash脚本中处理某些C风格的命令。
4.使⽤if-else作为简单密码提⽰ (4. Using if-else as a simple password prompt)
The if-else function is known for its versatility and range of application. In this example, we will use if-else in shell script to make the interface for a password prompt.
if-else函数以其多功能性和应⽤范围⽽闻名。 在此⽰例中,我们将在shell脚本中使⽤if-else来创建密码提⽰的界⾯。
To do this, we will ask the user to enter the password and store it in the variable pass.
为此,我们将要求⽤户输⼊密码并将其存储在变量pass中。
If it matches the pre-defined password, which is ‘password’ in this example, the user will get the output as -“The password is correct”.
如果它与预定义的密码(在此⽰例中为“ password”)匹配,则⽤户将获得以下输出:“密码正确”。
Else, the shell script will tell the user that the password was incorrect and ask them to try again.
否则,shell脚本将告诉⽤户密码不正确,并要求他们重试。
#!/bin/bash
echo "Enter password"
read pass
if [ $pass="password" ]
then
echo "The password is correct."
else
echo "The password is incorrect, try again."
fi
Bash Password
Bash密码
结论 (Conclusion)
The function of if-else in shell script is an important asset for shell programmers. It is the best tool to use when you need to execute a set of statements based on pre-defined conditions.
Shell脚本中if-else的功能是Shell程序员的重要资产。 当您需要根据预定义条件执⾏⼀组语句时,它是最好的⼯具。
The if-else block is one, if not the most essential part of conditional programming. By regulating the execution of specific statements you not only make your code more efficient but you also free up the
precious time which the processor might have wasted executing statements which are unnecessary for a specific case.
if-else块是条件编程中最重要的部分(如果不是)。 通过规范特定语句的执⾏,您不仅可以提⾼代码效率,还可以释放处理器可能浪费的宝贵时间,这对于特定情况是不必要的。
We hope this tutorial was able to help you understand how to use the if-else function. If you have any queries, feedback or suggestions, feel free to reach out to us in the comments below.
我们希望本教程能够帮助您了解如何使⽤if-else函数。 如果您有任何疑问,反馈或建议,请随时通过以下评论与我们联系。
翻译⾃:
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论