shell脚本if条件语句_Shell脚本初学者指南4:条件和If-Then语句

shell脚本if条件语句

shell脚本if条件语句

banner-01

We’ve covered enough of the basics in our guide on shell scripting that you should feel comfortable experimenting. In this week’s installment, we’ll be tackling some of the more fun stuff, like conditions and “if-then” statements.

我们已经在shell脚本指南中介绍了足够的基础知识,您可以轻松尝试一下。 在本周的分期付款中,我们将介绍一些更有趣的内容,例如条件和“ if-then”语句。

什么是条件? (What Are Conditions?)

In everyday language, we say that conditions are requirements that must be met for something to occur. For my laptop to be able to connect to the internet, there are several conditions that must be met, like having an ISP, the modem and/or router being on, my laptop being on, etc. It’s pretty simple, and if any one of those requirements are not met, the result – my laptop connecting to the internet – does not happen.

用日常语言来说,我们说条件是发生某件事所必须满足的条件。 为了使我的笔记本电脑能够连接到互联网,必须满足一些条件,例如拥有ISP,打开调制解调器和/或路由器,打开我的笔记本电脑等。这非常简单,并且如果有的话这些要求没有得到满足,结果-我的笔记本电脑连接到互联网-不会发生。

Conditions in the realm of computing work similarly. We can test whether a string matches another string, whether it doesn’t match another string, or even if it exists at all. Similarly, we can test numerical arguments to see if one is great than, less than, or equal to another. To get something to happen after the conditions of the test are met, we use “if-then” statements. Their format is pretty simple.

计算领域中的条件类似地工作。 我们可以测试一个字符串是否与另一个字符串匹配,是否与另一个字符串不匹配,甚至根本不存在。 同样,我们可以测试数值参数,以查看一个数值是否大于,小于或等于另一个。 为了在满足测试条件之后发生某些事情,我们使用“ if-then”语句。 它们的格式非常简单。

if CONDITION then command1 command2 … commandn fi

如果条件,那么command1 command2…commandn fi

如果陈述 (If Statements)

Let’s run a quick little test script, shall we?

让我们运行一个快速的小测试脚本,好吗?

if test $1 -gt $2 then echo “$1 is greater than $2” fi

如果测试$ 1 -gt $ 2然后回显“ $ 1大于$ 2” fi

test gt

You’ll notice that only when that condition is true will the script execute the following command. Otherwise, the “if” statement will exit. If there are any commands after the “if” statement, then they will be run as normal. I added the following line to the end of our above script to illustrate this:

您会注意到,只有在该条件为true时,脚本才会执行以下命令。 否则,“ if”语句将退出。 如果在“ if”语句之后有任何命令,则它们将正常运行。 我在上述脚本的末尾添加了以下行来说明这一点:

echo “This comes after the if statement”

回声“这是在if语句之后”

command post-if

Here are some other numerical operators you may want to try out:

这是您可能要尝试的其他一些数字运算符:

  • -eq: equal to

    -eq:等于
  • -ne: not equal to

    -ne:不等于
  • -lt: less than

    -lt:小于
  • -le: less than or equal to

    -le:小于或等于
  • -gt: greater than

    -gt:大于
  • -ge: greater than or equal to

    -ge:大于或等于

测试字符串 (Testing Strings)

Now, if we modify the first line of our script to be this:

现在,如果我们将脚本的第一行修改为:

if test $1 = $2

如果测试$ 1 = $ 2

then the condition will test if the two are equal. There’s a catch here though!! The use of an equals sign (=) compares two strings, and not numbers. If you wish to compare numbers, you’d need to use the “-eq” operator similarly to how we used “-gt” above.

然后条件将测试两者是否相等。 不过这里有个陷阱!! 使用等号(=)会比较两个字符串,而不是数字。 如果要比较数字,则需要使用“ -eq”运算符,类似于我们上面使用“ -gt”的方式。

comparing strings

Now, let’s make another modification:

现在,让我们进行另一个修改:

if test $1 != $2

如果测试$ 1!= $ 2

comparing strings wrongly

The inclusion of the exclamation point (!) acts as a “not” modifier. That is, it only runs the following command when the two strings do not match.

包含感叹号(!)充当“非”修饰符。 也就是说,仅在两个字符串不匹配时才运行以下命令。

Here’s a list of some more string-based tests you can use:

这是您可以使用的更多基于字符串的测试的列表:

  • string: using just an argument by itself tests if the string is not blank (null) or not defined in some way

    字符串:仅使用一个参数即可测试字符串是否为空(空)或未通过某种方式定义
  • -n string: this will test if the string is not blank and is defined

    -n字符串:这将测试字符串是否为空且已定义
  • -z string: this will test if the string is blank and is defined that way

    -z字符串:这将测试字符串是否为空并以这种方式定义

如果还有什么呢? (What Else About If?)

I’ll admit, that section title was definitely a bad pun. Okay, we know how to execute a command if a test is true, but what if we want to execute a different command if it is false? We can easily put the two together by adding a section to our “if-then” statements – an “else”!

我承认,该部分的标题绝对是一个双关语。 好的,我们知道在测试为真的情况下如何执行命令,但是如果我们想要执行另一个为假的命令怎么办? 我们可以通过在“ if-then”语句中添加一个部分-“ else”来轻松地将两者结合在一起!

if CONDITION then command1 command2 … commandn else command1 command2 … commandn fi

如果条件,则command1 command2…commandn否则command1 command2…commandn fi

Let’s put together a simple script.

让我们放一个简单的脚本。

ifthenelse

There’s everything with the proper indentation. If you look closely, you’ll notice that we used square brackets ( [ and ] ) instead of the test command. They’re functionally equivalent for our purposes, and you’re much more likely to see the square brackets for various reasons, so we’ll use them from now on.

一切都有适当的缩进。 如果仔细观察,您会发现我们使用方括号([和])代替了test命令。 就我们的目的而言,它们在功能上是等效的,由于各种原因,您更有可能看到方括号,因此从现在开始我们将使用它们。

Here’s what the output will look like:

输出结果如下所示:

ifthenelse test

It’s that easy!

就这么简单!

现在我该怎么做? (What Do I Do Now?)

Now that you know how to use “if-then-else” statements, you can run scripts that can perform tests. For example, you can run a script that will calculate an md5 hash of a file and then compare it to the one you downloaded in a file to see if they match.

现在您知道如何使用“ if-then-else”语句,您可以运行可以执行测试的脚本。 例如,您可以运行一个脚本,该脚本将计算文件的md5哈希 ,然后将其与文件中下载的md5哈希进行比较,以查看它们是否匹配。

For some bonus points, you can create a script that has a “for” loop, but uses test conditions instead of reading lines out of a list file…

为了获得一些奖励积分,您可以创建一个具有“ for”循环的脚本,但是使用测试条件而不是从列表文件中读取行…



We’re getting to some of the more interesting parts in our Beginner’s Guide to Shell Scripting. If you missed the previous lessons, here’s a quick list for you to check out:

我们将在《 Shell脚本初学者指南》中介绍一些更有趣的部分。 如果您错过了上一课,可以通过以下快速清单进行检查:

  1. The Basics of Shell Scripting

    Shell脚本编写的基础

  2. Using For Loops

    使用循环

  3. More Basic Commands

    更多基本命令

  4. What are the differences between Linux shells?

    Linux Shell之间有什么区别?

  5. How to use Basic Regular Expressions

    如何使用基本正则表达式

If you’ve made or used scripts that utilize testing conditions, if-then-else statements, and “for” loops, share with us down in the comments!

如果您编写或使用的脚本利用了测试条件,if-then-else语句和“ for”循环,请在注释中与我们分享!

翻译自: https://www.howtogeek.com/70822/the-beginners-guide-to-shell-scripting-4-conditions-and-if-then-statements/

shell脚本if条件语句

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值