| 会员登录 立即注册

打印 上一主题 下一主题

Bash Shell 脚本新手指南(三)

[复制链接]
跳转到指定楼层
楼主
3AAA 发表于 2022-3-20 11:11:05 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
楼主
3AAA
2022-3-20 11:11:05 2928 0 看楼主


欢迎来到面向初学者的 Bash Shell 脚本知识第三部分。这最后一篇文章将再来学习一些知识点,这些将使你为持续的个人发展做好准备。它将涉及到函数、用 if/elif语句进行比较,并以研究while循环作为结尾。

函数


让我们从一个看似困难但其实很简单的基本概念开始,即函数。把它看作是一种简单的方法,可以把脚本中被反复使用的部分放到一个可重复使用的组中。你在本系列第一篇或第二篇文章中所做的任何事情都可以放在一个函数中。因此,让我们把一个函数放到我们的 learnToScript.sh文件中。让我指出几个关键点。你将需要为你的函数起一个名字、一对小括号,以及用大括号包围放在你的函数中的命令。
#!/bin/bash#A function to return an echo statement.helloFunc {        echo "Hello from a function."}#invoke the first function helloFunchelloFunc
你会看到如下输出结果:
[zexcon@fedora ~]$ ./learnToScript.shHello from a function.[zexcon@fedora ~]$
函数是重复使用一组命令的好方法,但如果你能使它们在每次使用时对不同的数据进行操作,它们会更加有用。这就要求你在每次调用函数时提供数据,这称为参数。

要提供参数,你只需在调用函数时把它们加在函数名称后面。为了使用你提供的数据,你在函数命令中使用位置来引用它们。它们将被命名为 $1、$2、$3,以此类推,这取决于你的函数将需要多少个参数。

让我们修改上一个例子来帮助更好地理解这个问题。
#!/bin/bash#A function to return an echo statement.helloFunc {        echo "Hello from a function."        echo $1        echo $2        echo "You gave me $# arguments"}#invoke the function helloFunchelloFunc "How is the weather?" Fine
输出如下:
Hello from a function.How is the weather?FineYou gave me 2 arguments
输出中发生的事情是 helloFunc在每一行都做了一个回显。首先它回显了一个Hello from a function,然后它继续回显变量$1的值,结果是你传递给helloFunc的"How is the weather?"。然后它将继续处理变量$2,并回显其值,这是你传递的第二个项目:Fine。该函数将以回显You gave me $# arguments结束。注意,第一个参数是一个用双引号括起来的单个字符串"How is the weather?"。第二个参数Fine没有空格,所以不需要引号。

除了使用 $1、$2等之外,你还可以通过使用变量$#来确定传递给函数的参数数量。这意味着你可以创建一个接受可变参数数量的函数。

关于 bash 函数的更多细节,网上有很多好的参考资料。这里有一个可以让你入门的资料。

我希望你能了解到函数如何在你的 bash 脚本中提供巨大的灵活性。

数值比较


如果你想进行数字比较,你需要在方括号 中使用以下运算符之一:
    -eq(等于)-ge(等于或大于)-gt(大于)-le(小于或等于)-lt(小于)-ne(不相等)

因此,举例来说,如果你想看 12 是否等于或小于 25,可以像 [ 12 -le 25 ]这样。当然,12 和 25 可以是变量。例如,[ $twelve -le $twentyfive ]。(LCTT 译注:注意保留方括号和判断语句间的空格)

if 和 elif 语句


那么让我们用数字比较来介绍 if语句。Bash 中的if语句将以if开始, 以fi结束。if语句 以if开始,然后是你想做的检查。在本例中,检查的内容是变量numberOne是否等于1。如果numberOne等于1,将执行then语句,否则将执行else语句。
#!/bin/bashnumberTwelve=12if [ $numberTwelve -eq 12 ]then        echo "numberTwelve is equal to 12"elif [ $numberTwelve -gt 12 ]then        echo "numberTwelve variable is greater than 12"else        echo "neither of the statemens matched"fi
输出如下:
[zexcon@fedora ~]$ ./learnToScript.shnumberTwelve variable is equal to 12
你所看到的是 if语句的第一行,它在检查变量的值是否真的等于12。如果是的话,语句就会停止,并发出numberTwelve is equal to 12的回显,然后在fi之后继续执行你的脚本。如果变量大于12的话,就会执行elif语句,并在fi之后继续执行。当你使用if或if/elif语句时,它是自上而下工作的。当第一条语句是匹配的时候,它会停止并执行该命令,并在fi之后继续执行。

字符串比较 [[]]


这就是数字比较。那么字符串的比较呢?使用双方括号 [[]]和以下运算符等于或不等于。(LCTT 译注:注意保留方括号和判断语句间的空格)
    =(相等)!=(不相等)

请记住,字符串还有一些其他的比较方法,我们这里不会讨论,但可以深入了解一下它们以及它们是如何工作的。
#!/bin/bash#variable with a stringstringItem="Hello"#This will match since it is looking for an exact match with $stringItemif [[ $stringItem = "Hello" ]]then        echo "The string is an exact match."else        echo "The strings do not match exactly."fi#This will utilize the then statement since it is not looking for a case sensitive matchif [[ $stringItem = "hello" ]]then        echo "The string does match but is not case sensitive."else        echo "The string does not match because of the capitalized H."fi
你将得到以下三行:
[zexcon@fedora ~]$ ./learnToScript.shThe string is an exact match.The string does not match because of the capitalized H.[zexcon@fedora ~]$

while 循环


在结束这个系列之前,让我们看一下循环。一个关于 while循环的例子是:“当 1 小于 10 时,在数值上加 1”,你继续这样做直到该判断语句不再为真。下面你将看到变量number设置为1。在下一行,我们有一个while语句,它检查number是否小于或等于10。在do和done之间包含的命令被执行,因为while的比较结果为真。所以我们回显一些文本,并在number的值上加1。我们继续执行,直到while语句不再为真,它脱离了循环,并回显We have completed the while loop since $number is greater than 10.。
#!/bin/bashnumber=1while [ $number -le 10 ]do        echo "We checked the current number is $number so we will increment once"        ((number=number+1))done        echo "We have completed the while loop since $number is greater than 10."
while循环的结果如下:
[zexcon@fedora ~]$ ./learnToScript.shWe checked the current number is 1 so we will increment onceWe checked the current number is 2 so we will increment onceWe checked the current number is 3 so we will increment onceWe checked the current number is 4 so we will increment onceWe checked the current number is 5 so we will increment onceWe checked the current number is 6 so we will increment onceWe checked the current number is 7 so we will increment onceWe checked the current number is 8 so we will increment onceWe checked the current number is 9 so we will increment onceWe checked the current number is 10 so we will increment onceWe have completed the while loop since 11 is greater than 10.[zexcon@fedora ~]$
正如你所看到的,实现这一目的所需的脚本量要比用 if语句不断检查每个数字少得多。这就是循环的伟大之处,而while循环只是众多方式之一,它以不同的方式来满足你的个人需要。

总结


下一步是什么?正如文章所指出的,这是,面向 Bash Shell 脚本初学者的。希望我激发了你对脚本的兴趣或终生的热爱。我建议你去看看其他人的脚本,了解你不知道或不理解的地方。请记住,由于本系列每篇文章都介绍了数学运算、比较字符串、输出和归纳数据的多种方法,它们也可以用函数、循环或许多其他方法来完成。如果你练习所讨论的基础知识,你将会很开心地把它们与你还要学习的所有其他知识结合起来。试试吧,让我们在 Fedora Linux 世界里见。

via: https://fedoramagazine.org/bash-shell-scripting-for-beginners-part-3

作者:Matthew Darnell选题:lujun9972译者:wxy校对:wxy

本文由 LCTT原创编译,Linux中国荣誉推出
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则 返回列表

3AAA 当前离线
白银会员

查看:2928 | 回复:0

关于我们  |   侵权投诉受理  |   联系我们  |   Archiver  |  
免责声明:邳州信息网所有言论只代表发表者个人观点,与本站无关
Copyright © 2009-2025 pzxxw.com 版权所有:邳州金银杏文化传媒有限公司  

苏公网安备 32038202000401号

快速回复 返回顶部 返回列表