Bash Shell 腳本新手指南(三)
歡迎來到面向初學(xué)者的 Bash Shell 腳本知識(shí)第三部分。這最后一篇文章將再來學(xué)習(xí)一些知識(shí)點(diǎn),這些將使你為持續(xù)的個(gè)人發(fā)展做好準(zhǔn)備。它將涉及到函數(shù)、用 if/elif 語句進(jìn)行比較,并以研究 while 循環(huán)作為結(jié)尾。
函數(shù)
讓我們從一個(gè)看似困難但其實(shí)很簡(jiǎn)單的基本概念開始,即函數(shù)。把它看作是一種簡(jiǎn)單的方法,可以把腳本中被反復(fù)使用的部分放到一個(gè)可重復(fù)使用的組中。你在本系列第一篇或第二篇文章中所做的任何事情都可以放在一個(gè)函數(shù)中。因此,讓我們把一個(gè)函數(shù)放到我們的 learnToScript.sh 文件中。讓我指出幾個(gè)關(guān)鍵點(diǎn)。你將需要為你的函數(shù)起一個(gè)名字、一對(duì)小括號(hào),以及用大括號(hào)包圍放在你的函數(shù)中的命令。
#A function to return an echo statement.
helloFunc() {
echo "Hello from a function."
}
#invoke the first function helloFunc()
helloFunc
你會(huì)看到如下輸出結(jié)果:
[zexcon@fedora ~]$ ./learnToScript.sh
Hello from a function.
[zexcon@fedora ~]$
函數(shù)是重復(fù)使用一組命令的好方法,但如果你能使它們?cè)诿看问褂脮r(shí)對(duì)不同的數(shù)據(jù)進(jìn)行操作,它們會(huì)更加有用。這就要求你在每次調(diào)用函數(shù)時(shí)提供數(shù)據(jù),這稱為參數(shù)。
要提供參數(shù),你只需在調(diào)用函數(shù)時(shí)把它們加在函數(shù)名稱后面。為了使用你提供的數(shù)據(jù),你在函數(shù)命令中使用位置來引用它們。它們將被命名為 $1、$2、$3,以此類推,這取決于你的函數(shù)將需要多少個(gè)參數(shù)。
讓我們修改上一個(gè)例子來幫助更好地理解這個(gè)問題。
#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
輸出中發(fā)生的事情是 helloFunc() 在每一行都做了一個(gè)回顯。首先它回顯了一個(gè) Hello from a function,然后它繼續(xù)回顯變量 $1 的值,結(jié)果是你傳遞給 helloFunc 的 "How is the weather?"。然后它將繼續(xù)處理變量 $2,并回顯其值,這是你傳遞的第二個(gè)項(xiàng)目:Fine。該函數(shù)將以回顯 You gave me $# arguments 結(jié)束。注意,第一個(gè)參數(shù)是一個(gè)用雙引號(hào)括起來的單個(gè)字符串 "How is the weather?"。第二個(gè)參數(shù) Fine 沒有空格,所以不需要引號(hào)。
除了使用 $1、$2 等之外,你還可以通過使用變量 $# 來確定傳遞給函數(shù)的參數(shù)數(shù)量。這意味著你可以創(chuàng)建一個(gè)接受可變參數(shù)數(shù)量的函數(shù)。
關(guān)于 bash 函數(shù)的更多細(xì)節(jié),網(wǎng)上有很多好的參考資料。這里有一個(gè)可以讓你入門的資料。
我希望你能了解到函數(shù)如何在你的 bash 腳本中提供巨大的靈活性。
數(shù)值比較 []
如果你想進(jìn)行數(shù)字比較,你需要在方括號(hào) [] 中使用以下運(yùn)算符之一:
- -eq (等于)
- -ge (等于或大于)
- -gt (大于)
- -le (小于或等于)
- -lt (小于)
- -ne (不相等)
因此,舉例來說,如果你想看 12 是否等于或小于 25,可以像 [ 12 -le 25 ] 這樣。當(dāng)然,12 和 25 可以是變量。例如,[ $twelve -le $twentyfive ]。(LCTT 譯注:注意保留方括號(hào)和判斷語句間的空格)
if 和 elif 語句
那么讓我們用數(shù)字比較來介紹 if 語句。Bash 中的 if 語句將以 if 開始, 以 fi 結(jié)束。if 語句 以 if 開始,然后是你想做的檢查。在本例中,檢查的內(nèi)容是變量 numberOne 是否等于 1。如果 numberOne 等于 1,將執(zhí)行 then 語句,否則將執(zhí)行 else 語句。
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。如果是的話,語句就會(huì)停止,并發(fā)出 numberTwelve is equal to 12 的回顯,然后在 fi 之后繼續(xù)執(zhí)行你的腳本。如果變量大于 12 的話,就會(huì)執(zhí)行 elif 語句,并在 fi 之后繼續(xù)執(zhí)行。當(dāng)你使用 if 或 if/elif 語句時(shí),它是自上而下工作的。當(dāng)?shù)谝粭l語句是匹配的時(shí)候,它會(huì)停止并執(zhí)行該命令,并在 fi 之后繼續(xù)執(zhí)行。
字符串比較 [[]]
這就是數(shù)字比較。那么字符串的比較呢?使用雙方括號(hào) [[]] 和以下運(yùn)算符等于或不等于。(LCTT 譯注:注意保留方括號(hào)和判斷語句間的空格)
- = (相等)
- != (不相等)
請(qǐng)記住,字符串還有一些其他的比較方法,我們這里不會(huì)討論,但可以深入了解一下它們以及它們是如何工作的。
#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 循環(huán)
在結(jié)束這個(gè)系列之前,讓我們看一下循環(huán)。一個(gè)關(guān)于 while 循環(huán)的例子是:“當(dāng) 1 小于 10 時(shí),在數(shù)值上加 1”,你繼續(xù)這樣做直到該判斷語句不再為真。下面你將看到變量 number 設(shè)置為 1。在下一行,我們有一個(gè) while 語句,它檢查 number 是否小于或等于 10。在 do 和 done 之間包含的命令被執(zhí)行,因?yàn)?while 的比較結(jié)果為真。所以我們回顯一些文本,并在 number 的值上加 1。我們繼續(xù)執(zhí)行,直到 while 語句不再為真,它脫離了循環(huán),并回顯 We have completed the while loop since $number is greater than 10.。
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 循環(huán)的結(jié)果如下:
[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 ~]$
正如你所看到的,實(shí)現(xiàn)這一目的所需的腳本量要比用 if 語句不斷檢查每個(gè)數(shù)字少得多。這就是循環(huán)的偉大之處,而 while 循環(huán)只是眾多方式之一,它以不同的方式來滿足你的個(gè)人需要。
總結(jié)
下一步是什么?正如文章所指出的,這是,面向 Bash Shell 腳本初學(xué)者的。希望我激發(fā)了你對(duì)腳本的興趣或終生的熱愛。我建議你去看看其他人的腳本,了解你不知道或不理解的地方。請(qǐng)記住,由于本系列每篇文章都介紹了數(shù)學(xué)運(yùn)算、比較字符串、輸出和歸納數(shù)據(jù)的多種方法,它們也可以用函數(shù)、循環(huán)或許多其他方法來完成。如果你練習(xí)所討論的基礎(chǔ)知識(shí),你將會(huì)很開心地把它們與你還要學(xué)習(xí)的所有其他知識(shí)結(jié)合起來。