1. shell腳本基礎
1.1 shell腳本介紹
-
什麼是shell腳本
一系列的shell命令的集合, 還可以加入一些邏輯操作(if else for) 將這些命令放入一個文件中.
- 文件
- shell命令 * n個
- 可以加入邏輯
- 需要在linux的終端中執行
# test.sh ls pwd if [ xxx ] cd ..
-
什麼是shell命令?
$ ls $ cd $ pwd
-
shell腳本的基本格式
-
命名格式
- 一般命名規則 : xxxxx.sh (建議以.sh爲後綴命名)
-
書寫格式
# test.sh #是shell腳本中的註釋 # 第一行如果忘了了寫, 使用默認的命令解析器 /bin/sh #!/bin/bash # 指定解析shell腳本的時候使用的命令解析器 /bin/sh也可以 # 一系列的shell命令 ls pwd cp rm
-
-
shell腳本的執行
# shell腳本編寫完成之後, 必須添加執行權限 chmod u+x xxx.sh # 執行shell腳本 ./xxx.sh sh test.sh
1.2 shell腳本中的變量
-
變量的定義
-
位置變量
執行腳本的時候, 可以給腳本傳遞參數, 在腳本內部接收這些參數, 需要使用位置變量
# 已經存在的腳本test.sh #!/bin/bash echo "hello , world, $0" echo "第一個參數: $1" echo "第2參數: $2" echo "第3個參數: $3" echo "第4個參數: $4" echo "第5個參數: $5" echo "第6個參數: $6" # 執行test.sh $ ./test.sh 11 22 3 4 5 6 aa bb hello , world, ./test.sh 第一個參數: 11 第2參數: 22 第3個參數: 3 第4個參數: 4 第5個參數: 5 第6個參數: 6
- $0: 執行的腳本文件的名字
- $1: 第一個參數
- $2: 第2個參數
- $3: 第三個參數
- …
-
特殊變量
- $#: 獲取傳遞的參數的個數
- $@: 給腳本傳遞的所有的參數
- $?: 腳本執行完成之後的狀態, 失敗>0 or 成功=0
- $$: 腳本進程執行之後對應的進程ID
# test.sh #!/bin/bash echo "hello , world, $0" echo "第一個參數: $1" echo "第2參數: $2" echo "第3個參數: $3" echo "第4個參數: $4" echo "第5個參數: $5" echo "第6個參數: $6" echo "傳遞的參數個數: $#" echo "傳遞的所有的參數: $@" echo "當前腳本的進程ID: $$" $ ./test.sh aa bb cc dd ee ff 8 9 0 hello , world, ./test.sh 第一個參數: aa 第2參數: bb 第3個參數: cc 第4個參數: dd 第5個參數: ee 第6個參數: ff 傳遞的參數個數: 9 傳遞的所有的參數: aa bb cc dd ee ff 8 9 0 當前腳本的進程ID: 47946 # 腳本執行狀態查看 $ echo $? 0 -> 成功 非0 -> 失敗
-
普通變量取值
# 變量定義 value=123 # 默認以字符串處理 value1 = "123 456" echo $value # 如何取變量的值: - $變量名 - ${變量名}
-
取命令執行之後的結果值
# 取值的兩種方式: var=$(shell命令) var=`shell命令`
-
引號的使用
# 雙引號 echo "當前文件: $var" - 打印的時候會將var中的值取出並輸出 # 單引號 echo '當前文件: $var' - 將字符串原樣輸出
1.3 條件判斷和循環
-
shell腳本中的if條件判斷
# if語句 # 注意事項: - if 和 []直接有一個空格 - [ 條件 ] : 條件的前後都有空格 - else if => elif - if [ 條件判斷 ];then 邏輯處理 -> shell命令 xxx xxxx xxxx fi # =================== if [ 條件判斷 ] then 邏輯處理 -> shell命令 xxx xxx fi # if ... elif .. fi if [ 條件判斷 ];then 邏輯處理 -> shell命令 xxx xxxx xxxx elif [ 條件判斷 ];then shell命令 elif [ 條件判斷 ];then shell命令 elif [ 條件判斷 ];then shell命令 else shell命令 fi
# if.sh #!/bin/bash # 需要對傳遞到腳本內部的文件名做判斷 if [ -d $1 ];then echo "$1 是一個目錄!" elif [ -s $1 ];then echo "$1 是一個文件, 並文件不爲空" else echo "$1 不是目錄, 有肯能不存在, 或文件大小爲0" fi
-
shell腳本for循環
# shell中的循環 for/ while # 語法: for 變量 in 集合; do;done for var in 集合;do shell命令 done
# for.sh #!/bin/bash # 對當前目錄下的文件進行遍歷 list=`ls` for var in $list;do echo "當前文件: $var" echo '當前文件: $var' done # 運行腳本 $ ./for.sh 當前文件: a 當前文件: $var 當前文件: abc 當前文件: $var 當前文件: for.sh 當前文件: $var 當前文件: if.sh 當前文件: $var 當前文件: test.sh 當前文件: $var
1.4 shell腳本中的函數
# 沒有函數修飾, 沒有參數, 沒有返回值
# 格式
funcName(){
# 得到第一個參數
arg1=$1
# 得到第2個參數
arg2=$2
函數體 -> shell命令 + 邏輯循環和判斷
mkdir /root/abc
}
# 沒有參數列表, 但是可以傳參
# 函數調用
funcName aa bb cc dd
# 函數調用之後的狀態:
0 -> 調用成功
非0 -> 失敗
#!/bin/bash
# 判斷傳遞進行來的文件名是不是目錄, 如果不是, 創建...
# 定義函數
is_directory()
{
# 得到文件名, 通過參數得到文件名
name=$1
if [ -d $name ];then
echo "$name 是一個目錄!"
else
# 創建目錄
mkdir $name
if [ 0 -ne $? ];then
echo "目錄創建失敗..."
exit
fi
echo "目錄創建成功!!!"
fi
}
# 函數調用
is_directory $1
shell條件測試參考
-
文件狀態測試
-b filename 當filename 存在並且是塊文件時返回真(返回0) -c filename 當filename 存在並且是字符文件時返回真 -d pathname 當pathname 存在並且是一個目錄時返回真 -e pathname 當由pathname 指定的文件或目錄存在時返回真 -f filename 當filename 存在並且是正規(普通)文件時返回真 -g pathname 當由pathname 指定的文件或目錄存在並且設置了SGID 位時返回真 -h/-L filename 當filename 存在並且是符號鏈接文件時返回真 (或 filename) -k pathname 當由pathname 指定的文件或目錄存在並且設置了"粘滯"位時返回真 -p filename 當filename 存在並且是命名管道時返回真 -r pathname 當由pathname 指定的文件或目錄存在並且可讀時返回真 -s filename 當filename 存在並且文件大小大於0 時返回真 -S filename 當filename 存在並且是socket 時返回真 -t fd 當fd 是與終端設備相關聯的文件描述符時返回真 -u pathname 當由pathname 指定的文件或目錄存在並且設置了SUID 位時返回真 -w pathname 當由pathname 指定的文件或目錄存在並且可寫時返回真 -x pathname 當由pathname 指定的文件或目錄存在並且可執行時返回真 -O pathname 當由pathname 存在並且被當前進程的有效用戶id 的用戶擁有時返回真(字母O 大寫) -G pathname 當由pathname 存在並且屬於當前進程的有效用戶id 的用戶的用戶組時返回真 file1 -nt file2 file1 比file2 新時返回真 file1 -ot file2 file1 比file2 舊時返回真 f1 -ef f2 files f1 and f2 are hard links to the same file -
常見字符串測試
-z string 字符串string 爲空串(長度爲0)時返回真 -n string 字符串string 爲非空串時返回真 str1 = str2 字符串str1 和字符串str2 相等時返回真 str1 == str2 同 = str1 != str2 字符串str1 和字符串str2 不相等時返回真 str1 < str2 按字典順序排序,字符串str1 在字符串str2 之前 str1 > str2 按字典順序排序,字符串str1 在字符串str2 之後 -
常見數值測試
nt1 -eq int2 如果int1 等於int2,則返回真 int1 -ne int2 如果int1 不等於int2,則返回真 int1 -lt int2 如果int1 小於int2,則返回真 int1 -le int2 如果int1 小於等於int2,則返回真 int1 -gt int2 如果int1 大於int2,則返回真 int1 -ge int2 如果int1 大於等於int2,則返回真 -
測試時使用的邏輯操作符
-a 邏輯與,操作符兩邊均爲真,結果爲真,否則爲假。&& -o 邏輯或,操作符兩邊一邊爲真,結果爲真,否則爲假。||
nt2,則返回真 |
---|
int1 -ne int2 |
int1 -lt int2 |
int1 -le int2 |
int1 -gt int2 |
int1 -ge int2 |
-
測試時使用的邏輯操作符
-a 邏輯與,操作符兩邊均爲真,結果爲真,否則爲假。&& -o 邏輯或,操作符兩邊一邊爲真,結果爲真,否則爲假。|| ! 邏輯否,條件爲假,結果爲真。