Shell 腳本編程(中級篇)

中級篇

一、結(jié)構(gòu)化命令

基礎(chǔ)篇的示例中,Shell 大多數(shù)情況下都以順序的方式依次執(zhí)行腳本中的每一條指令。
但是在實際情況中,很多程序都需要對腳本中命令的邏輯流進(jìn)行控制。即根據(jù)某些條件和狀態(tài)完成判斷,再決定其后的程序該以怎樣的規(guī)則執(zhí)行。
Shell 腳本支持一部分結(jié)構(gòu)化指令,如 if-then、case 等,用于修改程序的執(zhí)行流程。

1. if-then

最基本的結(jié)構(gòu)化語句即 if-then,其格式如下:

if command
then
    commands
fi

Bash Shell 中的 if-then 語句和其他編程語言中的類似語句稍有些不同,其作用如下:

  • 首先執(zhí)行 if 后面跟隨的命令 command
  • 如果該命令 command結(jié)束狀態(tài)(exit status)為 0(即該命令執(zhí)行成功),則 then 后面的命令依次被執(zhí)行
  • 如果該命令 command 的結(jié)束狀態(tài)為任何非 0 的值(該命令非正常退出),則不執(zhí)行 then 后面的命令,Bash Shell 移動到腳本中的下一條命令(即 fi 后面的內(nèi)容)

如下面的兩個示例:

$ cat if1.sh
#!/bin/bash

if pwd
then
    echo "It worked"
fi
echo "We are outside the if statement"
$ ./if1.sh
/Users/starky/program/scripts/shell
It worked
We are outside the if statement
$ cat if2.sh
#!/bin/bash

if IamNotaCommand
then
    echo "It worked"
fi
echo "We are outside the if statement"
$ ./if2.sh
./if2.sh: line 3: IamNotaCommand: command not found
We are outside the if statement

在上面的示例中,if1.sh 腳本里 if 后面的命令(pwd)成功執(zhí)行,則 then 后面的 echo 命令也跟著執(zhí)行。
if2.sh 腳本里 if 后面的命令(IamNotaCommand)執(zhí)行出錯,則 then 后面的 echo 命令不會被執(zhí)行。
無論如何,判斷結(jié)束后都會繼續(xù)執(zhí)行 fi 后面的語句。

2. if-else-then

if-else-then 語句的格式為:

if command
then
    commands
else
    commands
fi

當(dāng) if 后面的命令執(zhí)行后返回值為 0 時,則 then 后面的命令被執(zhí)行。
如果 if 后面的命令執(zhí)行后返回值不為 0,則執(zhí)行 else 后面的命令。

示例(if3.sh)如下:

#!/bin/bash

testuser=starky
if ls -d /Users/$testuser
then
    echo "The bash files for user $testuser are:"
    ls -a /Users/$testuser/.b*
    echo
else
    echo "The user $testuser does not exist on this system."
    echo
fi

運(yùn)行結(jié)果:

$ chmod +x if3.sh
$ ./if3.sh
/Users/starky
The bash files for user starky are:
/Users/starky/.bash_history /Users/starky/.bash_profile
/Users/starky/.bash_sessions:
...

但是將上述腳本中 testuser 變量的值改為系統(tǒng)中并不存在的用戶時(如 NoSuchUser),則運(yùn)行效果如下:

$ cat if3.sh
#!/bin/bash

testuser=NoSuchUser
if ls -d /Users/$testuser
then
    echo "The bash files for user $testuser are:"
    ls -a /Users/$testuser/.b*
    echo
else
    echo "The user $testuser does not exist on this system."
    echo
fi
$ ./if3.sh
ls: /Users/NoSuchUser: No such file or directory
The user NoSuchUser does not exist on this system.

其他結(jié)構(gòu)形式的 if-else-then 語句還包括:

if command1
then
    commands
elif
    command2
then
    more commands
fi

當(dāng)然理論上講可以使用足夠的 if-then-elif-then 語句,只不過類似情況下多使用更為恰當(dāng)?shù)?case 語句。

3. test 命令

上面提到的 if-then 語句,大多是這樣的形式:

if command
then
    commands
...

if 后面跟的是一條普通的 Shell 命令,根據(jù)該命令是否成功執(zhí)行,來確定 then 后面的命令是否執(zhí)行。

可以借助 test 命令,使用如下形式的 if-then 語句:

if test condition
then
    commands
fi

如果 test 命令后面的 condition 計算后為 TRUE,則 test 命令退出并返回一個值為 0 的狀態(tài)值。then 后面的命令被執(zhí)行。
如果 test 命令后面的 condition 計算后為 FALSE,則 test 命令退出并返回一個非零的狀態(tài)值。跳出當(dāng)前的 if-then 并繼續(xù)執(zhí)行后面的內(nèi)容。

如下面的兩個例子:

$ cat test_condition.sh
#!/bin/bash

my_variable="Full"

if test $my_variable
then
    echo "The $my_variable expression returns a True"
else
    echo "The $my_variable expression returns a False"
fi
$ ./test_condition.sh
The Full expression returns a True
$ cat test_condition2.sh
#!/bin/bash

my_variable=""

if test $my_variable
then
    echo "The $my_variable expression returns a True"
else
    echo "The $my_variable expression returns a False"
fi
$ ./test_condition2.sh
The  expression returns a False

Bash Shell 還提供了另外一種不需要使用 test 命令來完成條件檢查的 if-then 語句:

if [ condition ]
then
    commands
fi

方括號中的內(nèi)容用來定義判斷條件。注意方括號和 condition 之間的空格。
支持三種形式的判斷條件:

  • 數(shù)字比較
  • 字符串比較
  • 文件比較
數(shù)字比較
操作 描述
n1 -eq n2 檢查 n1 是否等于 n2
n1 -ge n2 檢查 n1 是否大于或等于 n2
n1 -gt n2 檢查 n1 是否大于 n2
n1 -le n2 檢查 n1 是否小于或等于 n2
n1 -lt n2 檢查 n1 是否 小于 n2
n1 -ne n2 檢查 n1 是否不等于 n2

示例如下:

$ cat numeric_test.sh
#!/bin/bash

value1=10
value2=11

if [ $value1 -gt 5 ]
then
    echo "The test value $value1 is greater than 5"
fi

if [ $value1 -eq $value2 ]
then
    echo "The values are equal"
else
    echo "The values are different"
fi
$ ./numeric_test.sh
The test value 10 is greater than 5
The values are different
字符串比較
操作 描述
str1 = str2 檢查 str1 和 str2 是否相同
str1 != str2 檢查 str1 和 str2 是否不相同
str1 < str2 檢查 str1 是否小于 str2
str1 > str2 檢查 str1 是否大于 str2
-n str1 檢查 str1 是否長度大于 0(不為空)
-z str1 檢查 str1 是否長度為 0(為空)

示例1:

$ cat welcome.sh
#!/bin/bash

testuser=starky

if [ $USER = $testuser ]
then
    echo "Welcome $testuser"
else
    echo "This is not $testuser"
fi
$ ./welcome.sh
Welcome starky

PS:在使用 str1 > str2str1 < str2 這種類型的條件時,需加上轉(zhuǎn)義符號。否則大于號或小于號會被當(dāng)作重定向處理。

示例2:

$ cat compare.sh
#!/bin/bash

val1=Testing
val2=testing

if [ $val1 \> $val2 ]
then
    echo "$val1 is greater than $val2"
else
    echo "$val1 is less than $val2"
fi
$ ./compare.sh
Testing is less than testing

PSif 條件中的比較依據(jù)的是基本 ASCII 順序,通過每個字符(從首字母開始)的 ASCII 值的比較來判斷大小順序。


-n-z 常常用來確定指定字符串是否為空。
示例3:

$ cat empty.sh
#!/bin/bash

val1=testing
val2=''

if [ -n $val1 ]
then
    echo "The string '$val1' is not empty"
else
    echo "The string '$val1' is empty"
fi

if [ -z $val2 ]
then
    echo "The string '$val2' is empty"
else
    echo "The string '$val2' is not empty"
fi

if [ -z $val3 ]
then
    echo "The string '$val3' is empty"
else
    echo "The string '$val3' is not empty"
fi
$ ./empty.sh
The string 'testing' is not empty
The string '' is empty
The string '' is empty

上述腳本中 val3 變量自始至終沒有被定義,被自動判斷為空字符串。

文件比較
操作 描述
-d file 檢查 file 是否存在且是一個目錄
-e file 檢查 file 是否存在
-f file 檢查 file 是否存在且是一個文件
-r file 檢查 file 是否存在且可讀
-s file 檢查 file 是否存在且不為空
-w file 檢查 file 是否存在且可寫
-x file 檢查 file 是否存在且可執(zhí)行
-O file 檢查 file 是否存在且其屬主為當(dāng)前用戶
-G file 檢查 file 是否存在且其默認(rèn)數(shù)組和當(dāng)前用戶相同
file1 -nt file2 檢查 file1 是否比 file2 更新(newer than)
file1 -ot file2 檢查 file1 是否比 file2 更老(older than)

示例1,目錄檢查:

$ cat check_dir.sh
#!/bin/bash

directory=/Users/starky
if [ -d $directory ]
then
    echo "The $directory directory exists"
    cd $directory
    ls
else
    echo "The $directory directory does not exist"
fi
$ ./check_dir.sh
The /Users/starky directory exists
Applications    Downloads   Music       extract.sh  program
Desktop     Library     Pictures    id_rsa.pub  software
Documents   Movies      Public      miniconda3  vim.tar.gz

示例2,刪除空文件:

$ cat check_empty.sh
#!/bin/bash

file_name=empty

# 檢查該文件是否存在
if [ -f $file_name ]
then
    # 該文件存在,則繼續(xù)檢查其內(nèi)容是否非空
    if [ -s $file_name ]
    then
        # 內(nèi)容不為空
        echo "The $file_name file exists and has data in it."
        echo "Will not remove this file"
    else
        # 內(nèi)容為空
        echo "The $file_name file exists, but is empty."
        echo "Deleting empty file..."
        rm $file_name
    fi
else
    # 該文件不存在
    echo "File, $file_name, does not exist."
fi
$ ./check_empty.sh
File, empty, does not exist.
$ touch empty
$ ls empty
empty
$ ./check_empty.sh
The empty file exists, but is empty.
Deleting empty file...
$ ls empty
ls: empty: No such file or directory
4. 組合判斷

if-then 語句允許使用布爾邏輯來完成多個條件的組合判斷。

  • [ condition1 ] && [ condition2 ] 邏輯與
  • [ condition1 ] || [ condition2 ] 邏輯或

示例:

cat compound_test.sh
#!/bin/bash

if [ -d $HOME ] && [ -w $HOME/testing ]
then
    echo "The file exists and you can write to it"
else
    echo "I cannot write to the file"
fi
$ ./compound_test.sh
I cannot write to the file
$ touch ~/testing
$ ./compound_test.sh
The file exists and you can write to it
if-then 中的雙括號

Bash Shell 在 if-then 語句中還提供了一些更高級的特性:

  • 雙小括號(數(shù)學(xué)表達(dá)式)
  • 雙中括號(字符串處理函數(shù))
使用雙小括號

雙小括號允許在條件測試時使用(相對于 test 命令)更高級的數(shù)學(xué)表達(dá)式。
格式:(( expression ))
其中 expression 除了支持前面提到過的基本的數(shù)學(xué)操作符外,還支持以下操作符:

操作符 描述
val++ Post-increment
val-- Post-decrement
++val Pre-increment
--val Pre-decrement
! 邏輯非
~ 按位否定
**
<< 向左按位移位
>> 向右按位移位
& 按位邏輯與
| 按位邏輯或
&& 邏輯與
雙管道符 邏輯或

示例:

$ cat parenthesis.sh
#!/bin/bash

val1=10

if (( $val1 ** 2 > 90 ))
then
    (( val2 = $val1 ** 2 ))
    echo "The square of $val1 is $val2"
fi
$ ./parenthesis.sh
The square of 10 is 100
使用雙中括號

雙中括號為 test 命令中的字符串比較提供了更高級的特性,尤其是支持 模式匹配。
示例:

cat pattern.sh
#!/bin/bash

if [[ $USER == s* ]]
then
    echo "Hello $USER"
else
    echo "Sorry, I do not know you"
fi
$ ./pattern.sh
Hello starky
4. case 命令

很多時候,某個作為判斷條件的變量有多個可能的取值,而你需要根據(jù)不同的取值指導(dǎo)程序去做對應(yīng)的操作。
如果使用 if-then-else 語句,代碼免不了會拉得有些長。。。

不同于 if 語句(通過 if、elif 依次檢查同一個變量的所有取值),case 命令以類似列表的形式檢查同一個變量的所有取值:

case variable in
    pattern1 | pattern2) commands1;;
    pattern3) commands2;;
    *) default commands;;
esac

case 命令將指定的變量與多個不同的模式進(jìn)行比對,如果匹配,則執(zhí)行該模式后面的命令。
可以在同一行中定義多個模式(使用 | 符號分隔)。
星號表示當(dāng)之前的所有模式都不能被匹配時,默認(rèn)執(zhí)行的命令。

示例:

$ cat case_user.sh
#!/bin/bash

case $USER in
    rich | barbara)
        echo "Welcome, $USER"
        echo "Please enjoy your visit";;
    testing)
        echo "Special testing account";;
    jessica)
        echo "Do not forget to logout when you're done";;
    *)
        echo "Sorry, you are not allowed here";;
esac
$ ./case_user.sh
Sorry, you are not allowed here

上面的腳本等同于使用如下的 if 語句:

$ cat if_user.sh
#!/bin/bash

if [[ $USER = "rich" || $USER = "barbara" ]]
then
    echo "Welcome $USER"
    echo "Please enjoy your visit"
elif [ $USER = "testing" ]
then
    echo "Special testing account"
elif [ $USER = "jessica" ]
then
    echo "Do not forget to logout when you're done"
else
    echo "Sorry, you are not allowed here"
fi
$ ./if_user.sh
Sorry, you are not allowed here

二、更多結(jié)構(gòu)化命令

1. for 命令

很多時候,需要重復(fù)地執(zhí)行一系列命令直到某個特定的情況出現(xiàn)。
for 命令允許創(chuàng)建一個循環(huán)來遍歷某些值,并在循環(huán)過程中使用遍歷到的值執(zhí)行一系列特定的指令。語法格式:

for var in list
do
    commands
done
遍歷列表
$ cat for_list.sh
#!/bin/bash
for state in Alabama Alaska Arizona
do
    echo The next state is $state
done

echo "The last state we visited was $state"
$ ./for_list.sh
The next state is Alabama
The next state is Alaska
The next state is Arizona
The last state we visited was Arizona

每次 for 命令遍歷由后面列表(Alabama Alaska Arizona)提供的值時,會把列表中當(dāng)前項目的值賦值給 $state 變量(即第一次遍歷把 Alabama 賦值給 $state,第二次是 Alaska),該 $state 變量可以在后面的 do ... done 結(jié)構(gòu)中使用。

for 循環(huán)結(jié)束后 $state 變量依舊有效,即保留最后一次遍歷時得到的值(列表的最后一項 Arizona)而不會被銷毀。

遍歷字符串中的列表

Bash Shell 中的for 循環(huán)可以直接接收單個字符串作為參數(shù),并通過其中的空格將該字符串分割成多個遍歷的項目。

$ cat for_variable.sh
#!/bin/bash

states="Alabama Alaska Arizona"
states=$states" Arkansas"

for state in $states
do
    echo "Have you ever visited $state?"
done
$ ./for_variable.sh
Have you ever visited Alabama?
Have you ever visited Alaska?
Have you ever visited Arizona?
Have you ever visited Arkansas?

腳本中 state=$state" Arkansas" 可以用來在 $state 變量中的字符串末尾添加新的字符串,也等同于在列表末尾添加了新的項目。

從命令中獲取遍歷的列表

可以通過命令替換將命令的輸出作為 for 命令循環(huán)遍歷的列表:

$ cat states.txt
Alabama Alaska Arizona
$ cat for_command.sh
#!/bin/bash

for state in $(cat states.txt)
do
    echo "Visit beautiful $state"
done
$ ./for_command.sh
Visit beautiful Alabama
Visit beautiful Alaska
Visit beautiful Arizona
遍歷文件目錄

可以使用 for 命令直接循環(huán)目錄中的文件(結(jié)合 * 符號):

$ cat for_dir.sh
#!/bin/bash

for file in /Users/starky/.vim* /Users/starky/badtest
do
    if [ -d "$file" ]
    then
        echo "$file is a directory"
    elif [ -f "$file" ]
    then
        echo "$file is a file"
    else
        echo "$file doesn't exit"
    fi
done
$ ./for_dir.sh
/Users/starky/.vim is a directory
/Users/starky/.viminfo is a file
/Users/starky/.vimrc is a file
/Users/starky/badtest doesn't exit

注意腳本中的 if [ -d "$file" ] 段代碼, "$file" 被雙引號包裹了起來。
原因是文件名中包含空格是合法的,但是在 Shell 腳本中,如果 $file 變量的值包含空格,則不能直接在 if [ -d ... ] 中使用,所以這里使用了雙引號。

2. C 語法的 for 命令

Bash Shell 也支持語法類似于 C 語言形式的 for 循環(huán):
for (( variable assignment ; condition ; iteration process ))

$ cat for_c.sh
#!/bin/bash

for (( i=1; i <= 10; i++ ))
do
    echo "The next number is $i"
done
$ ./for_c.sh
The next number is 1
The next number is 2
The next number is 3
The next number is 4
The next number is 5
The next number is 6
The next number is 7
The next number is 8
The next number is 9
The next number is 10
3. while 命令

while 命令允許用戶定義一個判斷指令,然后循環(huán)執(zhí)行一系列命令,并在每次執(zhí)行前檢查判斷指令的返回值(exit status)。直到判斷指令的返回值不為 0,終止循環(huán)。

while 命令的格式為:

while test command
do
    other commands
done

示例程序如下:

$ cat while.sh
#!/bin/bash

var=5
while [ $var -gt 0 ]
do
    echo $var
    var=$[ $var -1 ]
done
$ ./while.sh
5
4
3
2
1
4. break 命令

break 命令用于跳出循環(huán)

$ cat break.sh
#!/bin/bash

for var in 1 2 3 4 5 6 7 8 9 10
do
    if [ $var -eq 5 ]
    then
        break
    fi
    echo "Iteration number: $var"
done
echo "The for loop is completed"
$ ./break.sh
Iteration number: 1
Iteration number: 2
Iteration number: 3
Iteration number: 4
The for loop is completed
5. 處理循環(huán)的輸出

可以使用管道重定向處理循環(huán)的輸出信息,直接將管道或重定向命令加在循環(huán)語句塊的末尾即可。

$ cat output.sh
#!/bin/bash

for (( a = 1; a < 5; a++ ))
do
    echo "The number is $a"
done > numbers.txt

echo "The command is finished"
$ ./output.sh
The command is finished
$ cat numbers.txt
The number is 1
The number is 2
The number is 3
The number is 4

參考資料

Linux Command Line and Shell Scripting Bible 3rd Edition

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 官網(wǎng) 中文版本 好的網(wǎng)站 Content-type: text/htmlBASH Section: User ...
    不排版閱讀 4,727評論 0 5
  • 第 2 章 SHELL 基礎(chǔ)知識2.1 shell腳本我們在上面簡單介紹了一下什么是shell腳本,現(xiàn)在我們來進(jìn)一...
    LiWei_9e4b閱讀 1,652評論 0 0
  • 一、Python簡介和環(huán)境搭建以及pip的安裝 4課時實驗課主要內(nèi)容 【Python簡介】: Python 是一個...
    _小老虎_閱讀 6,353評論 0 10
  • 初讀《World of Warcraft編年史》就被其宏大的構(gòu)思和奇妙的想象所震撼。 利用簡書作為筆記本,來簡述這...
    南森兮閱讀 390評論 1 2
  • 在茂密的樹林中,常??梢钥吹綇闹θ~間透過的一道道光柱,類似于這種自然現(xiàn)象,就是丁達(dá)爾現(xiàn)象。攝影是光線捕捉的藝術(shù),丁...
    伊水淺墨閱讀 406評論 0 0

友情鏈接更多精彩內(nèi)容