1 什麼是shell script?
shell script是通過shell 的功能所寫的一個program,這個程序 是使用純文本文件,將一些shell的語法與命令寫在裏面 ,搭配正則式,管道命令和數據流重定向等功能,以達到我們所想要的功能。
2.1 編寫第一個script
#!/bin/bash
# this is first shell script
# printf Hello World
echo -e "Hello World \a \n"
exit 0
第一行 #!/bin/bash shen聲明使用的是哪一個shell
其餘的 # 後面的內容都爲註釋。
ehco 那一行即爲主程序。 這一行的內容是 打印 Hello World。 -e參數代表識別"" 中 需要顯示內容的特殊字符。 這裏\a 即爲發出警告聲,\n 爲換行。
exit 0爲告知程序執行結果,執行完腳本後,可以通過 echo $? 這一句命令來判斷上一個命令的執行結果。
2.2 script 的執行方式區別
- 利用直接執行的方式。
在命令行下直接執行絕對路徑或者相對路徑,或者sh script.sh
這種執行方式 script是在子進程的bash內執行。當子進程完成後,子進程內的各項變量或操作將會結束而不會傳回父進程。
2 .利用source來執行腳本。
# source script.sh
這個script就會在這個bash 內部生效。這也是爲什麼不註銷系統的時候,修改.bashrc 裏面的配置並使其生效的時候,使用source ~/.bashrc 而不是 sh ~/.bashrc。
3 script中常用的命令和判斷式
3.1 test 命令
tset 命令用來檢測系統上面某些文件或者是相關的屬性。
例如:
test -e ./test .sh && echo “exit”|| echo “not exit”
這句命令是判斷test.sh這個文件存不存在,然後再命令行中打印相關結果,如果存在就打印exit,反之就打印not exit。
Test後面接的常用命令解釋: test -e file
關於某個文件名的文件類型的判斷:
-e 該文件名是否存在、
-f 該文件名是否存在且爲文件
-d 該文件名是否存在且爲目錄
關於文件權限的檢測:test -r file
-r 檢測該文件名是否存在且具有 可讀 權限
-w 檢測該文件名是否存在且具有 可寫 權限
-x 檢測該文件名是否存在且具有 可執行 權限
-s 檢測改文件名是否存在且爲 非空白文件
兩個文件之間的比較:test file1 -nt file2
-nt (newer than)判斷文件1 是否比文件2 新。
-ot (older than ) 判斷文件1 是否比 文件2 舊。
-ef 判斷文件1 和文件2 是否是同一個文件,可用在判斷hard link的判定上。 主要是判斷兩個文件是否指向於同一個inode。
兩個整數之間的判斷:test n1 -eq n2
-eq 兩個數是否相等
-ne 兩個數字是否不等
-gt n1 是否大於n2
-lt n1 是否小於n2
-ge n1 是否大於等於n2
-le n1是否小於等於n2
判斷字符串的數據:
Test -z string 判斷字符串是否爲0,如果爲空字符串,則爲true
Test -n string 判斷字符串是否不爲0,如果爲空字符串,則爲false
注意:-n可以省略
Test str1 = str2 判斷str1 和str2 是否相等,相等則爲true
Test str1 != str2 判斷str1 和str2 是否不相等,相等則爲false
多重條件判斷:
-a 兩個條件同時成立。 test -r file -a -x file,當文件同時具有可讀和可執行權限的時候,才爲true
-o 任意一個條件成立。test -r file -o -x file,當文件同時具有可讀或者可執行權限的時候,就爲true
! 反向的狀態。 test ! -x file 當file不具有x時,則爲true。
3.2 利用 判斷符號[]
[ -z “$HOME” ]; echo $?
上面是判斷HOME這個變量是否爲空。中括號的使用方法和test幾乎一樣,只是中括號常用於添加判斷式中。後面會講到。
注意事項:
1 在中括號[]裏面的每一個組件都要有空格鍵來隔開。
2 在中括號[]裏面的變量都以雙引號來括號起來。
3 在中括號[]裏面的常量都以雙引號或單引號來括號起來。
譬如:
Name = shell sh
[ $NAME == “sh” ] 這是不對的,會報 bash: [: too many argument
上面判斷會等於[ shell sh == “sh” ] 一個判斷式僅能有兩個數據,但是這個判斷式有三個數據了。應該 [ “shell sh” == “sh” ]纔對。
hyz@ubuntu:~/test$ cat zhongkouhao.sh
#!/bin/bash
# Program:
# show user choice
# History:
# 2019/08/11
read -p "Please Input you choice Y/N:" choice
[ "$choice" == "y" -o "$choice" == 'Y' ] && echo "OK,continue" && exit 0
[ "$choice" == "n" -o "$choice" == 'N' ] && echo "not ok,interrupt" && exit 0
echo "I do not know you choice" && exit 0
上面就是讀到控制檯的參數,然後進行判斷和相關的打印。
3.3 shell script的默認變量 $0 $1
Script 針對變量名是已經有設置好一些變量名稱了。
/shell/var.sh opt1 opt2 opt3 opt4
$0 &1 &2 &3 &4
特殊變量:
$#: 代表後面接的參數的個數
$@: 代表”$1” ,“$2”, “$3”
$*: 代表 “$1c$2c$3c”,其中c爲分隔字符,默認爲空格鍵。
Shift:造成參數變量號碼偏移。
hyz@ubuntu:~/test$ cat var.sh
#!/bin/bash
# Program:
# show script name,par and so
# History:
# 2019/08/11
echo "total par numver is : $#"
echo "you whole pa is '$@' "
echo "you whole par is $* "
echo "the firtt numver is $1"
shift
echo "total par numver is : $#"
echo "you whole pa is '$@' "
shift 2
echo "total par numver is : $#"
echo "you whole pa is '$@' "
hyz@ubuntu:~/test$ source var.sh one two three four five
total par numver is : 5
you whole pa is 'one two three four five'
you whole par is one two three four five
the firtt numver is one
total par numver is : 4
you whole pa is 'two three four five'
total par numver is : 2
you whole pa is 'four five'
4 條件判斷式
4.1 if...then
單層判斷式:
if [條件判斷]; then
條件成立時的操作
fi
可以將所有條件寫入一箇中括號裏面也可以寫入多箇中括號用 && 或者|| 來隔開。
[ “$choice” == “Y” -o “$choice” == ‘y’ ]
等同於:
[ “$choice” == “Y” ] || [ “$choice” == ‘y’ ]
多重條件判斷式:
if [條件判斷1]; then
條件1成立乾的事
elif [條件判斷2 ]; then
條件1 不成立,條件2成立 做的事
else
條件1 和條件2 都不成立乾的事
fi
4.2 case..esac
case $變量名稱 in
“第一個變量內容”)
程序段
;;
“第二個變量內容”)
程序段
;;
*)
不包括第一個變量和第二個變量的執行程序段
exit 1;
;;
esac
4.3 function
Function fname(){
程序段
}
因爲shell script的執行是由上而下 由左向右,所以function都需要設置在程序的最前面。
另外,Function也是擁有內置變量的,他的內置變量與shell script類似,函數名稱代表$0,後續的變量也是以$1 $2 Kauai代替的。
hyz@ubuntu:~/test$ cat funtion.sh
#!/bin/bash
# Program:
# user fucntion print information
# History:
# 2019/08/11
function print(){
echo "you choice is $1"
}
echo "Thsi program will print you selection"
case $1 in
"one")
print 1
;;
"two")
print 2
;;
*)
echo "Usage $0 [one|two]"
;;
esac
hyz@ubuntu:~/test$ source funtion.sh one
Thsi program will print you selection
you choice is 1
hyz@ubuntu:~/test$ source funtion.sh three
Thsi program will print you selection
Usage bash [one|two]
5 循環 loop
5.1 while do done,until do done (不定時循環)
僞代碼:
While [conditon]
do
Program
done
當 condition成立的時候就進行循環。
Until [conditon]
do
Program
done
當condition成立的時候終止循環。
5.2 for...do...done (定時循環)
for var in con1 con2 con3
do
Program
done
$var 的變量內容在循環工作時一直在改變:
1 第一次循環,$var 爲con1
2 第二次循環,$var 爲con2
3 第三次循環,$var 爲con3
......
for var in $(seq 1 100)
$var的變量的內容是 從1 到100 ,seq是指連續的意思。
另外一種寫法:
for ((初始值;限制值;執行步長))
do
Program
done
和c語言類似:
初始值: 某個變量在循環當中的初始值,直接以類似i =1設置好;
限制值: 當變量的值在這個限制值的範圍內,就繼續進行循環,例如i<100;
執行步長: 沒做一次循環,變量的變化值,例如i=i+1;
hyz@ubuntu:~/test$ cat for.sh
#!/bin/bash
# Program:
# printf 1+2+...+n
# History:
# 2019/08/11
read -p "Please input a number,I will cont for 1+2+...+n: " nu
s=0
for (( i=1; i<=$nu; i++ ))
do
s=$(( $s + $i ))
done
echo "The result is $s"
hyz@ubuntu:~/test$ source for.sh
Please input a number,I will cont for 1+2+...+n: 5
The result is 15
hyz@ubuntu:~/test$
6 shell script 的追蹤和調試
# sh [-nvx] script.sh
-n 不要執行script,進查詢語法的問題
-v 在執行script的時候,先將script你們的內容輸出到屏幕上
-x 將使用到的script內容顯示到屏幕上
由sh -x的方式來將命令執行過程也顯示出來,如此用戶可以判斷程序代碼執行到哪一段時會出現相關的信息。
感謝linux鳥哥的私房菜。