shell腳本
一茫打、什么是 shell script
shell script 是利用 shell 的功能所寫的一個(gè)“程序 (program)”居触,這個(gè)程序是使用純文本文件,將一些 shell 的語法與指令(含外部指令)寫在里面老赤, 搭配正則表達(dá)式轮洋、管道命令與數(shù)據(jù)流重導(dǎo)向等功能,以達(dá)到我們所想要的處理目的抬旺。
簡(jiǎn)單的說弊予,也就是可以使用一個(gè)普通的文本,寫上多條 shell 命令开财,一起執(zhí)行這些命令汉柒。
但是,在這個(gè)文件中可以添加一些邏輯判斷什么的责鳍。
二碾褂、shell script 規(guī)范
script 的功能;
script 的版本信息历葛;
script 的作者與聯(lián)絡(luò)方式正塌;
script 的版權(quán)宣告方式;
script 的 History (歷史紀(jì)錄)啃洋;
script 內(nèi)較特殊的指令,使用“絕對(duì)路徑”的方式來下達(dá)屎鳍;
script 運(yùn)行時(shí)需要的環(huán)境變量預(yù)先宣告與設(shè)置宏娄。
三、執(zhí)行腳本方式 (source script, sh script, ./script)
source script
父進(jìn)程中執(zhí)行腳本中代碼
sh script
子進(jìn)程中執(zhí)行腳本中的代碼逮壁,相當(dāng)于打開了一個(gè)子 shell ,一個(gè)全新的環(huán)境孵坚。
四、script 的默認(rèn)變量
特殊變量
$# :代表后接的參數(shù)“個(gè)數(shù)”,以上表為例這里顯示為“ 4 ”卖宠;
$@ :代表 "$1" "$2" "$3" "$4" 之意巍杈,每個(gè)變量是獨(dú)立的(用雙引號(hào)括起來);
$* :代表"$1<u>c</u>$2<u>c</u>$3<u>c</u>$4"扛伍,其中 <u>c</u> 為分隔字符较幌,默認(rèn)為空白鍵接箫, 所以本例中代表 "$1 $2 $3 $4"之意。
位置變量
位置變量是根據(jù)命令出現(xiàn)在命令行上的位置來確定的變量,在shell調(diào)用過程中熙涤,會(huì)按參數(shù)所在的位置進(jìn)行調(diào)用。
$0 $1 $2 $3 ...$9
eg : wc /etc/passwd /etc/group
Shell判斷表達(dá)式
test
示例:
[root@biudefor script]# touch a.txt
[root@biudefor script]# test -e a.txt;echo $?
0 # 測(cè)試成功穴豫,命令返回值為 0
[root@biudefor script]# test -e s.txt;echo $?
1 # 測(cè)試失敗渤滞,命令返回值為 非 0
[root@biudefor script]# test -f a.txt;echo $?
0
[root@biudefor script]# test -d a.txt;echo $?
1
2
示例:
[root@biudefor ~]# test -r a.txt; echo $?
0
[root@biudefor ~]# test -x a.txt; echo $?
1
[root@biudefor ~]# test -w a.txt; echo $?
0
[root@biudefor ~]# test -u a.txt; echo $? # 判斷 a.txt 文件是否具有 SUID 屬性
1
[root@biudefor ~]# cat a.txt # 查看 a.txt ,此文件內(nèi)容為空
[root@biudefor ~]# test -s a.txt; echo $? # 判斷 a.txt 文件中有內(nèi)容
1 # 命令返回值為 1 ,說明文件中沒有內(nèi)容
[root@biudefor ~]# echo "123" > a.txt
[root@biudefor ~]# test -s a.txt; echo $?
0
3
示例:
[root@biudefor ~]# touch b.txt
[root@biudefor ~]# ls -l a.txt
-rw-r--r-- 1 shark staff 4 12 17 22:59 a.txt
[root@biudefor ~]# ls -l b.txt
-rw-r--r-- 1 shark staff 0 12 17 23:05 b.txt
[root@biudefor ~]# test a.txt -nt b.txt; echo $? # 判斷 a.txt 是否比 b.txt 新
1 # 返回 1, 表示判斷表達(dá)式不成立
[root@biudefor ~]# test b.txt -nt a.txt; echo $?
0
硬鏈接:兩個(gè)文件的inode號(hào)是一樣的
4
示例:
[root@biudefor ~]# test 10 -eq 20; echo $?
1
[root@biudefor ~]# n1=10
[root@biudefor ~]# n2=20
[root@biudefor ~]# test $n1 -eq $n2; echo $?
1
[root@biudefor ~]# test $n1 -lt $n2; echo $?
0
[root@biudefor ~]# test $n1 -ne $n2; echo $?
0
5
這里的 string 可以是實(shí)際的字符串因俐,也可以是一個(gè)變量
這里說的字符串是否為 0 的意思是 字符串的長度是否為 0
示例:
[root@biudefor ~]# test -z ''; echo $? # 空字符串
0
[root@biudefor ~]# test -z ' '; echo $? # 含有一個(gè)空格的字符串
1
[root@biudefor ~]# test ! -z ' '; echo $? # 判斷含有一個(gè)空格的字符串拇惋,其長度為非 0 的字符串, 空格也算是字符串。
0
[root@biudefor ~]# test -z ${name}; echo $? # 變量未定義抹剩,shell 中認(rèn)為其長度為 0
0
[root@biudefor ~]# name=shark
[root@biudefor ~]# test -z ${name}; echo $?
1
[root@biudefor ~]# age='' # 定義變量撑帖,并且賦值為空字符串
[root@biudefor ~]# test -z ${age}; echo $? # shell 中,被賦值為空字符串的變量長度也為 0
0
在 shell 中吧兔,以下兩種情況磷仰,變量的長度均視為 0
1.變量未定義
2.變量定義了,但賦值為空字符串境蔼,比如 a='' , b=""
[root@kube-master script]# name=shark
[root@kube-master script]# age=shark
[root@kube-master script]# test $name == $age ;echo $?
0
[root@kube-master script]# test $name != $age ;echo $?
1
6
判斷符號(hào) []
[ -z "{HOME}" ] ; echo ?
必須要注意中括號(hào)的兩端需要有空白字符來分隔喔灶平! 假設(shè)我空白鍵使用“□”符號(hào)來表示,那么箍土,在這些地方你都需要有空白鍵:
- 在中括號(hào) [] 內(nèi)的每個(gè)元素之間都需要用空格來分隔逢享;
- 在中括號(hào)內(nèi)的變量,最好都以雙引號(hào)括號(hào)起來吴藻;
錯(cuò)誤示范
# 定義變量
[root@biudefor ~]# name="shark ops"
# 開始測(cè)試值是否相等
[root@biudefor ~]# [ ${name} == "xiguatian" ]
會(huì)報(bào)如下錯(cuò)誤信息:
bash: [: too many arguments
之前的錯(cuò)誤寫法 [ ${name} == "xiguatian" ] 的瞒爬,會(huì)變成這樣 [ shark ops == "xiguatian" ]
正確寫法應(yīng)該寫成這樣 [ "${name}" == "xiguatian" ] 的, 會(huì)變成這樣 [ "shark ops" == "xiguatian" ]
與或非
&&
邏輯與 [ ] && [ ]
||
邏輯或 [ ] || [ ]
!
邏輯非 [ ! ]