LinuxStudyNoteBy_Silvers:
(E:\Video_Tutorials\Linux自學(xué)視頻\linux視頻教程)
16:55 2016/2/21
# 什么是shell腳本
Shell除了是命令解釋器之外還是一種編程語言,用Shell編寫的程序類似于DOS下的批處理程序。
用戶可以在文件中存放一系列的命令,通常將Shell編寫的程序稱為Shell腳本或Shell程序。
將命令、變量和流程控制有機(jī)地結(jié)合起來將會得到一個功能強(qiáng)大的編程工具。Shell腳本語言非常擅長處理文本類型的數(shù)據(jù),由于Linux系統(tǒng)中的所有配置文件都是純文本的,所以Shell腳本語言在管理Linux系統(tǒng)中發(fā)揮了巨大作用。
# 為什么要學(xué)習(xí)Shell Script
自動化管理
監(jiān)控管理
日志數(shù)據(jù)處理
自動數(shù)據(jù)備份
# Shell腳本中的成分
Shell腳本是以行為單位的,在執(zhí)行腳本的時候會分解成一行一行依次執(zhí)行。腳本中所包含的成分主要有:注釋、命令、Shell變量和結(jié)構(gòu)控制語句。
注釋——注釋部分用于對腳本進(jìn)行解釋和說明,在注釋行的前面要加上符號#,這樣在執(zhí)行腳本的時候Shell就不會對該行進(jìn)行解釋。
命令——在Shell腳本中可以出現(xiàn)任何在交互下可以使用的命令。
變量——Shell支持字符串變量和整型變量。
結(jié)構(gòu)控制語句——用于編寫復(fù)雜腳本的流程控制語句。
# Shell腳本的建立與執(zhí)行
用戶可以使用任何文本編輯器編輯Shell腳本文件,如vi,gedit等
對Shell腳本文件的調(diào)用可以采用兩種方式:
? 一種是將文件名作為Shell命令的參數(shù),其調(diào)用格式為:
? ? $ sh script-file
? ? $ ./script-file
? 當(dāng)要被執(zhí)行腳本文件沒有可執(zhí)行權(quán)限時只能使用這種調(diào)用方式。
? 另一種調(diào)用方式是先將腳本文件的可訪問權(quán)限改為可執(zhí)行,以便該文件可以作為執(zhí)行文件調(diào)用。具體方法是:
? ? $ chmod u+x script-file
? 當(dāng)執(zhí)行一個腳本文件時,Shell就產(chǎn)生一個子Shell(即一個子進(jìn)程)去執(zhí)行命令文件中的命令。因此,腳本文件中的變量值不能傳遞到當(dāng)前Shell(即父Shell)。
# Shell腳本的編碼規(guī)范
一個bash腳本的正確起始部分應(yīng)該以#!開頭:
? #!/bin/bash
在調(diào)用bash的腳本時候,以#!開頭的語句通知系統(tǒng)用何種解釋器執(zhí)行此腳本。
如果bash是你的默認(rèn)shell,那么腳本的開頭也不用非得寫上#!。但是如果你使用不同的shell來開啟一個腳本的話,比如tcsh,那么你就必須需要#!了。良好的shell編碼規(guī)范還要求以注釋形式說明如下的內(nèi)容:
? 腳本名稱、腳本功能、版權(quán)聲明、復(fù)雜腳本應(yīng)對算法做簡要說明。
# 第一個shell script
? #!/bin/bash
? # program:
? # This program shows "Hello World."
? echo -e "Hello World!\a\n"
? exit 0
# shell 變量
定義shell變量
name=string
name是變量名,變量名用小寫字母命名
= 是賦值符號。兩邊不能有空格,否則將視為命令
string是被賦值的變量值
例如:
? ? ? v1=centos
? ? ? v2='this is a shell script test.'
shell變量表達(dá)式
? 表達(dá)式? ? ? ? ? ? ? ? ? ? 說明
${#string}? ? ? ? ? ? ? ? $string的長度
${string:position}? ? ? ? ? ? 從position開始提取字符串
${string:position:length}? ? ? ? 從位置$position開始提取長度為$length的子串
${string#substring}? ? ? ? ? ? 從開頭刪除最短匹配子串
${stirng##substring}? ? ? ? ? ? 從開頭刪除最長匹配子串
${string%substring}? ? ? ? ? ? 從結(jié)尾刪除最短匹配子串
${string%%substring}? ? ? ? ? ? 從結(jié)尾刪除最長匹配子串
#變量的數(shù)值計(jì)算
變量的數(shù)值計(jì)算都是通過(())來計(jì)算的。
運(yùn)算符如下所示:
? 加、減、乘、除、模、冪運(yùn)算等
? ((a=2+3**2-100%5))
? echo $a
? echo ((a=2+3**2-100%5))
? test與[]
? 標(biāo)志? ? ? ? ? ? ? ? ? ? 說明
? -f? ? ? ? ? ? ? ? ? ? ? 文件是否存在
? -d? ? ? ? ? ? ? ? ? ? 目錄是否存在
? -r? ? ? ? ? ? ? ? ? ? 文件是否有讀權(quán)限
? -w? ? ? ? ? ? ? ? ? ? 文件是否有寫權(quán)限
? -x? ? ? ? ? ? ? ? ? ? 文件是否有執(zhí)行權(quán)限
? test實(shí)例
#!/bin/bash
filename=/home/Document
test -f $filename && echo 'exist' || echo 'not exist'? #filename文件是否存在?
test -d $filename && echo 'exist' || echo 'not exist'? #filename目錄是否存在?
test -r $filename && echo 'exist' || echo 'not exist'? #filename是否可讀?
test -w $filename && echo 'exist' || echo 'not exist'? #filename是否可寫?
test -x $filename && echo 'exist' || echo 'not exist'? #filename是否可執(zhí)行?
? []判斷注意
* 在中括號中必須都要使用空格來分隔
* 在中括號中的變量,最好都要以雙引號括起來
* 在中括號中的常數(shù),最好都以單引號括起來。
例如:
? [ "$a" == "$b" ] &&echo 'Yes' || echo "No"
? [ '12' == '10' ] &&echo'Yes' || echo "No"
[]更多的使用于條件判斷
? # 單分支判斷
? if []; then
? ? ? ? echo statement
? # 雙分支判斷
? if []; then
? ? ? echo statement
? else
? ? echo statement
? fi
? # 多分支判斷
? if []; then
? ? ? ? echo statement
? elif
? ? echo statement
? elif
? ? ? echo statement
? fi
? case $name in
? 1);;
? esac
#if...elif...fi多分支判斷代碼示例:
#!/bin/bash
# 多分支判斷代碼示例1
echo 'Please input your number.'
read number
if [ $number == 1 ];then
echo 'Your input number is 1.'
elif [ $number == 2 ];then
echo 'Your input number is 2.'
elif [ $number == 3 ];then
echo 'Your input number is 3'
else
echo 'I dont know what your input number.'
fi
#!/bin/bash
# 多分支判斷語句代碼示例2
echo 'Please input your hardware.'
read hd
if [ $hd == cpu ];then
echo 'Your cpu info is'
cat /proc/cpuinfo
elif [ $hd == mem ];then
echo 'Your memory info is'
cat /proc/meminfo
elif [ $hd == hard ];then
echo 'Your harddisk info is'
df -h
else
echo 'I dont know what you input.'
fi
#case;;;esac多分支語句代碼示例:
#!/bin/bash
echo 'Please input an number.'
read number
case $number in
1)
? ? echo 'Your input number is 1';;
2)
? ? echo 'Your input number is 2';;
3)? ?
? ? echo 'Your input number is 3';;
*)
? ? echo 'I dont know what your input';;
esac
# shell 腳本中的循環(huán)操作
a.
while condition; do
done;
b.
untile condition; do
done;
c.
for(());do
statement
done;
循環(huán)操作代碼示例:
#!/bin/bash
# [] -eq -ne -gt -ge -lt -le
# (()) == != > >= < <=
# while do 循環(huán)代碼示例:
i=10
while (($i>=5));do #數(shù)值的操作需要雙層小括號
echo $i;
((i--));
done;
Shell腳本中多分支,循環(huán)條件比較推薦使用中括號來寫;小括號只適用于數(shù)值的比較及操作
#!/bin/bash
# []? -eq? -ne -gt -ge? -lt -le
# (()) ==? !=? >? >=? <? <=
# while do 循環(huán)代碼示例:
i=10
while [ $i -gt 5 ];do
echo $i;
((i--));
done;
#until do循環(huán)
#!/bin/bash
a=10
until (($a<0));do
echo $a;
((a--));
done;
# while do 和until do 的區(qū)別:
? while do 條件表達(dá)式成立的情況下執(zhí)行循環(huán)體;
? until do 條件表達(dá)式不成立的情況下執(zhí)行循環(huán)體;
#for循環(huán)
#!/bin/bash
for((i=1;i<=10;i++));do
echo $i;
done;
# function 的使用
#!/bin/bash
# Output all information
function print()
{
? ? echo "Your input is $1"
}
echo "This program will priint your selection!"
case $1 in
"one")
? ? print 1;;
"two")
? ? print 2;;
"three")
? ? print 3;;
*)
? ? print "Usage $0 {one|two|three}";;
esac