面向初学者的 Bash Shell 脚本(第 3 部分)

欢迎来到初学者级别的 Bash Shell 脚本的第 3 部分。 最后一篇文章将介绍一些其他项目,这些项目将使您为您的持续个人发展做好准备。 它将涉及函数,使用与 if/elif 语句的比较,并以查看 while 循环结束。

职能

让我们从一个看似困难但实际上很简单的基本概念开始,即函数。 将此视为一种简单的方法,可以将反复使用的脚本的一部分放入一个可重用的组中。 您在第 1 或第 2 条中所做的任何事情都可以放入函数中。 因此,让我们在 learnToScript.sh 文件中添加一个函数。 让我指出一些事情。 您将需要一个函数名称、左括号和右括号以及用于将函数中包含的命令括起来的大括号。

#!/bin/bash

#A function to return an echo statement.
helloFunc() {
        echo "Hello from a function."
}

#invoke the first function helloFunc()
helloFunc

您将在下面看到输出。

[zexcon@fedora ~]$ ./learnToScript.sh 
Hello from a function.
[zexcon@fedora ~]$ 

函数是重用一组命令的好方法,但如果您可以让它们在每次使用时对不同的数据进行操作,它们会更加有用。 这要求您在每次调用该函数时提供称为参数的数据。

要提供参数,您只需在调用它时将它们添加到函数名之后。 要使用您提供的数据,请使用功能命令中的位置参考。 它们将被命名为 $1、$2、$3 等等,具体取决于您的函数需要的参数数量。

让我们修改最后一个 example 以帮助更好地理解这一点。

#!/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 helloFunc()
helloFunc "How is the weather?" Fine

输出如下所示。

Hello from a function.
How is the weather?
Fine
You gave me 2 arguments

输出中发生的是 helloFunc() 在每一行上都做了一个回显。 首先它会回显“来自函数的 Hello”,然后它会回显变量 $1 的值,这是您传递给 helloFunc 的“天气怎么样?”的结果。 然后它将移动到变量 $2 并回显它的值,即您传递“Fine”的第二项。 该函数将通过返回回显“你给了我 $# 参数”来完成。 请注意,第一个参数是用双引号括起来的单个字符串,“天气怎么样?” . 第二个,“Fine”,没有空格,所以不需要引号。

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

网上有很多很好的参考资料,了解更多关于 bash 职能。 这是一个可以帮助您入门的方法.

我希望你能看到函数如何在你的 bash 脚本。

数值比较 []

如果要进行数值比较,则需要在方括号之间使用以下运算符之一 [] .

-eq(相等)

-ge(等于或大于)

-gt(大于)

-le(小于或等于)

-lt(小于)

-ne(不等于)

因此对于 example 如果您想查看 12 是否等于或小于 25,您会喜欢 [ 12 -le 25 ]. 当然,12 和 25 可以是变量。 为了 example, [$twelve -le $twentyfive].

if 和 elif 语句

所以让我们用我们的数值比较来介绍 if 语句。 Bash 中的 if 语句将以 if 开头并以 fi 结尾。 你从 if 开始,然后是你想要做的检查。 在这种情况下,检查变量 numberOne 是否等于 1。如果 numberOne 等于 1,它将执行 then 语句,否则将执行 else 语句。

#!/bin/bash

numberTwelve=12

if [ $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.sh
numberTwelve variable is equal to 12

您看到的是 if 语句中的第一行,检查变量值是否真的等于 12。如果是,语句停止并回显“numberTwelve 等于 12”,并且脚本的执行将在 fi 之后继续. 如果变量大于 12,它将执行 elif 语句并在 fi 之后再次继续执行。 当您使用 if 或 if/elif 语句时,它会自上而下地工作。 当第一个语句匹配时,它会停止并执行该命令并在 fi 之后继续。

字符串比较 [[]]

这就是数字比较。 比较字符串呢? 使用双方括号 [[]]和以下运算符相等或不相等。

=(等于)

!=(不等于)

请记住,字符串还有其他几个比较,我们不会讨论,但会深入了解它们以及它们是如何工作的。

#!/bin/bash

#variable with a string
    stringItem="Hello"

#This will match since it is looking for an exact match with $stringItem
    if [[ $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 match
    if [[ $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.sh
The string is an exact match.
The string does not match because of the capitalized H.
[zexcon@fedora ~]$

While 循环

在结束这个系列之前,让我们看一下循环。 一个 example while 循环是“当 1 小于 10 时将值加一”,您继续这样做,直到它不再为真。 下面你会看到变量 number 设置为 1。在下一行,我们有 while 语句检查 number 是否小于或等于 10。do 和 done 之间包含的命令被执行,因为 while 比较是真的。 所以我们回显一些文本并将数字的值加一。 我们继续,直到 while 语句不再为真,它跳出循环并回显“我们已经完成了 while 循环,因为 $number 大于 10。”

#!/bin/bash

number=1

while [ $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.sh 
We checked the current number is 1 so we will increment once
We checked the current number is 2 so we will increment once
We checked the current number is 3 so we will increment once
We checked the current number is 4 so we will increment once
We checked the current number is 5 so we will increment once
We checked the current number is 6 so we will increment once
We checked the current number is 7 so we will increment once
We checked the current number is 8 so we will increment once
We checked the current number is 9 so we will increment once
We checked the current number is 10 so we will increment once
We have completed the while loop since 11 is greater than 10.
[zexcon@fedora ~]$ 

正如您所看到的,如果您继续使用 if 语句检查每个数字,那么实现这一点所需的脚本数量要少得多。 这是循环的重要部分,而 while 循环只是众多选项中的第一个,它们以不同的方式做事以满足您的个人需求。

结论

接下来是什么? 正如文章指出的那样,它是 Bash Shell Scripting for Beginners。 希望我激发了对脚本的兴趣或终生热爱。 我建议您查看其他脚本并查找您不知道或不了解的内容。 请记住,随着每篇文章介绍了更多进行数学运算、比较字符串、输出和输入数据的方法,它们也可以通过函数、循环或许多其他方式完成。 如果您练习所讨论的基础知识,您将会很高兴将它们与您仍将要学习的所有其他选项结合起来。 出去玩玩,我会在附近见到你 Fedora Linux 世界。