shell基本概念:
A Unix shell is a command-line interpreter or shell that provides a traditional Unix-like command line user interface. Bash is a Unix shell and command language written by Brian Fox for the GNU Project as a free software replacement for the Bourne shell
從語言角度上來說,shell就是執行在類Unix系統上的命令行語言。而shell腳本就是有一組合法的shell命令來組成。
腳本編寫基礎語法:
1、流程控制:
if判斷格式:
if [ condition1 ] # 此處為判斷條件,中括號內部首尾都要有一個空格 then command1 # 此處為該條件下所要執行的命令。多條命令以 換行 或 ; 作為分隔 command2 elif [ condition2 ] then command3 command4 else commandN fi # 注意fi結束
for循環格式:
for var in item1 item2 ... itemN do # 要循環執行的命令,以do開頭 command1 command2 ... commandN done # 要循環執行的命令,以done結束
while循環:
while [ condition ] # 要循環執行的命令,以do開頭
do command done # 要循環執行的命令,以done結束
判斷式的類型:
1)算術判斷
| 描述 | 例子 | 注意事項 |
| 相等(equal) | [ 2 -eq 2] | 首尾空格 |
| 不相等(not equal) | [ 2 -nq 3 ] | |
| 大於(greater than) | [ 2 -gt 1 ] | |
| 大於等於(greater equal) | [ 2 -ge 1 ] | |
| 小於(less than) | [ 3 -lt 5 ] | |
| 小於等於(less equal) | [ 3 -le 2 ] |
| 判斷式不用[]的情況 | 例子 |
| (())也可表示算術比較 | ((10>=8)) |
2)字符串判斷
| 描述 | 例子 | |
| 如果兩字符串相同,則結果為真 | [ string1 = string2 ] | |
| 如果兩字符串不相同,則結果為真 | [ string1 != string2 ] | |
| 如果字符串不是空,則結果為真 | [ -n "$var" ] | |
| 如果字符串是空,則結果為真 | [ -z "$var" ] | |
| 表達式中*表示0或者多個字符 | [[ "xxxx" == x* ]] | |
| 在表達式中?表⽰單個字符 | [[ xxx == x?? ]] | |
| 在引用變量的時候要記得加雙引號[ -z "$a"] 否則當a未定義時會語法報錯 |
3)邏輯判斷
| 描述 | 操作符 | 例子 | 注意事項 |
| 邏輯與(and) | -a | [ 2 -ge 3 -a 1 -le 2 ] | 也可用&&表示 [[ 2 -ge 1 && 3 -ge 4 ]];echo $? |
| 非 | ! | [ ! 2 -ge 1 ];echo $? | 該例子返回1 |
| 邏輯或(or) | -o | [ 2 -ge 1 -o 3 -ge 4 ];echo $? | 也可用||表示 [[ 2 -ge 1 || 3 -ge 4 ]];echo $? |
4)shell內置判斷
| 描述 | 操作符 | 例子 |
| 如果文件(目錄)存在(exist),則結果為真 | -e file | [ -e my ];echo $? |
| 如果文件是一個目錄,則結果為真 | -d file | for f in *;do echo $f;if [ -d $f ];then break;fi;done |
| 如果文件是一個普通文件,則結果為真 | -f file | |
| 如果文件可讀,則結果為真 | -r file | |
| 如果文件的長度不為0,則結果為真 | -s file | |
| 如果文件可寫,則結果為真 | -w file | |
| 如果文件可執行,則結果為真 | -x file | |
| [[]]是[]的擴展語法,在老的sh中並不⽀持。推薦用[] |
調用shell腳本的方法:
1、直接輸入腳本所在路徑進行調用;
前提:
1)腳本第一行添加 #!/bin/bash ,來告訴系統這個腳本需要什么解釋器來執行,即使用哪一種 Shell 2)使用chmod +x 文件路徑 給使用者賦予執行該腳本的權限 2、直接運行解釋器,后面加上 shell 腳本的文件名為參數; e.g. sh test.sh
調用shell腳本時傳參:
1、在腳本路徑后,輸入參數
2、多個參數以空格分隔
3、在腳本內部,$0表示獲取該腳本名,$1表示獲取調用腳本時傳的第一個參數 $2表示調用腳本時傳的第二個參數。。以此類推
e.g. sh test.sh 第一個參數 第二個參數 第三個參數
實例--根據進程名關鍵字kill對應的進程:
1)進程關鍵字教程,並寫死在判斷中的情況。
#! /bin/bash
# kill進程函數 killProcess(){ echo "filename:$0"; echo "firstParameter:$1" echo "firstParameter:$2" echo "firstParameter:$3" if [ $1 = 'kill供貨打樁' ]; then
# ps和grep和awk組合過濾出進程id,作為參數傳給xargs kill -9 ps -aux |grep 'uwsgi --http :8001 --chdir /root/Desktop/Suppli'|grep -v grep|awk '{print $2}'|xargs kill -9; ps -aux |grep 'uwsgi --http :8001 --chdir /root/Desktop/Suppli'; elif [ $1 = 'kill自動化定時' ]; then ps -aux | grep '/usr/sbin/CROND -n' | grep -v grep|awk '{print $2}' | xargs kill -9; ps -aux | grep 'python /root/Desktop/Automation/MainThread.py'| grep -v grep | awk '{print $2}' | xargs kill -9 ; ps -aux | grep 'python /root/Desktop/Automation/MainThread.py' fi } killProcess $1 $2 $3 # 傳參調用函數
2)直接傳進程關鍵字的情況
#! /bin/bash # kill進程函數 killProcess(){ echo "filename:$0"; echo "firstParameter:$1" echo "firstParameter:$2" echo "firstParameter:$3" ps -aux |grep $1|grep -v grep|awk '{print $2}'|xargs kill -9; } killProcess $1 $2 $3 # 傳參調用函數
