shell script的簡單使用

shell script的簡單介紹

shell變數

1.命名規則

  • 命名只能使用英文字母,數字和下劃線,首個字元不能以數字開頭
  • 中間不能有空格,可以使用下劃線(_)
  • 不能使用標點符號。
  • 不能使用bash里的關鍵字

2.定義變數:name=value
3.使用變數:$name
4.只讀變數:readonly name
5.刪除變數:uset name
6.變數類型

  • 局部變數:當前shell有效
  • 環境變數:所有的應用程式有效

shell字元串

  1. 雙引號:可解析變數,可以出現轉義字元
  2. 單引號:不解析變數,原樣輸出
  3. 字元串拼接:
name="hello"
# 使用雙引號拼接
greeting="hello, "$name" !"
greeting_1="hello, $name} !"
echo $greeting  $greeting_1
# 使用單引號拼接
greeting_2='hello, '$name' !'
greeting_3='hello, ${name} !'
echo $greeting_2  $greeting_3

輸出結果為:

hello, hello ! hello, hello} !
hello, hello ! hello, ${name} !
  1. 獲取字元串長度
string="abcd"
echo ${#string} #輸出 4
  1. 提取子字元串
string="hello world"
echo ${string:1:4}#輸出 ello
# 字元串下標從0開始
  1. 查找子字元串
string="hello world"
echo `expr index $string e`#輸出2

7.反引號和$()一樣,裡面的語句當作命令執行

shell數組

  1. 數組定義:arrname=(value0 value1 value3)
  2. 數組讀取:${arrname[]},中括弧內為數組下標,從0開始
  3. 獲取數組長度:length=${#arrname[@]},*號也可以
  4. 或許數組單個元素的長度:lengthn=${#arrname[n]},n為數組下標

shell注釋

  1. 單行注釋:#
  2. 多行注釋:<<EOF以EOF結束,EOF可以是任意字母

shell傳遞參數

  1. 向腳本傳參
  • $0:執行的文件名
  • $1:為第一個參數
  • $2:為第二個參數 以此類推
  1. 特殊參數
  • $#:傳遞到腳本的參數個數
  • $$:腳本運行的當前進程ID
  • $!:後台運行的最後一個進程ID
  • $?:回傳碼,顯示最後命令的退出狀態
  • $@,$*:顯示所有傳遞的參數

shell運算符

  1. 算數運算符

假設變數a=10,b=20

運算符 說明 舉例
+ 加法 expr $a + $b 結果為 30。
減法 expr $a - $b 結果為 -10。
* 乘法 expr $a \* $b 結果為 200。
/ 除法 expr $b / $a 結果為 2。
% 取余 expr $b % $a 結果為 0。
= 賦值 a=$b 將把變數 b 的值賦給 a。
== 相等 用於比較兩個數字,相同則返回 true。 [ $a == $b ] 返回 false。
!= 不相等 用於比較兩個數字,不相同則返回 true。 [ $a != $b ] 返回 true。
  1. 關係運算符
運算符 說明 舉例
-eq 檢測兩個數是否相等,相等返回 true。 [ $a -eq $b ] 返回 false。
-ne 檢測兩個數是否不相等,不相等返回 true。 [ $a -ne $b ] 返回 true。
-gt 檢測左邊的數是否大於右邊的,如果是,則返回 true。 [ $a -gt $b ] 返回 false。
-lt 檢測左邊的數是否小於右邊的,如果是,則返回 true。 [ $a -lt $b ] 返回 true。
-ge 檢測左邊的數是否大於等於右邊的,如果是,則返回 true。 [ $a -ge $b ] 返回 false。
-le 檢測左邊的數是否小於等於右邊的,如果是,則返回 true。 [ $a -le $b ] 返回 true。
  1. 邏輯運算符
運算符 說明 舉例
&& 邏輯的 AND [[ $a -lt 100 && $b -gt 100 ]] 返回 false
|| 邏輯的 OR [[ $a -lt 100 || $b -gt 100 ]] 返回 true
  1. 布爾運算符
運算符 說明 舉例
! 非運算,表達式為 true 則返回 false,否則返回 true。 [ ! false ] 返回 true。
-o 或運算,有一個表達式為 true 則返回 true。 [ $a -lt 20 -o $b -gt 100 ] 返回 true。
-a 與運算,兩個表達式都為 true 才返回 true。 [ $a -lt 20 -a $b -gt 100 ] 返回 false
  1. 字元串運算符
    假設變數a=hello,b=world
運算符 說明 舉例
= 檢測兩個字元串是否相等,相等返回 true。 [ $a = $b ] 返回 false。
!= 檢測兩個字元串是否相等,不相等返回 true。 [ $a != $b ] 返回 true。
-z 檢測字元串長度是否為0,為0返回 true。 [ -z $a ] 返回 false。
-n 檢測字元串長度是否不為 0,不為 0 返回 true。 [ -n “$a” ] 返回 true。
$ 檢測字元串是否為空,不為空返回 true。 [ $a ] 返回 true
  1. 文件測試運算符
操作符 說明 舉例
-b file 檢測文件是否是塊設備文件,如果是,則返回 true。 [ -b $file ] 返回 false。
-c file 檢測文件是否是字元設備文件,如果是,則返回 true。 [ -c $file ] 返回 false。
-d file 檢測文件是否是目錄,如果是,則返回 true。 [ -d $file ] 返回 false。
-f file 檢測文件是否是普通文件(既不是目錄,也不是設備文件),如果是,則返回 true。 [ -f $file ] 返回 true。
-g file 檢測文件是否設置了 SGID 位,如果是,則返回 true。 [ -g $file ] 返回 false。
-k file 檢測文件是否設置了粘著位(Sticky Bit),如果是,則返回 true。 [ -k $file ] 返回 false。
-p file 檢測文件是否是有管道,如果是,則返回 true。 [ -p $file ] 返回 false。
-u file 檢測文件是否設置了 SUID 位,如果是,則返回 true。 [ -u $file ] 返回 false。
-r file 檢測文件是否可讀,如果是,則返回 true。 [ -r $file ] 返回 true。
-w file 檢測文件是否可寫,如果是,則返回 true。 [ -w $file ] 返回 true。
-x file 檢測文件是否可執行,如果是,則返回 true。 [ -x $file ] 返回 true。
-s file 檢測文件是否為空(文件大小是否大於0),不為空返回 true。 [ -s $file ] 返回 true。
-e file 檢測文件(包括目錄)是否存在,如果是,則返回 true。 [ -e $file ] 返回 true

流程式控制制

  1. if 語句語法格式:
if condition
then
    command1 
    command2
    ...
    commandN 
fi
  1. if else 語法格式:
if condition
then
    command1 
    command2
    ...
    commandN
else
    command
fi
  1. if else-if else 語法格式:
if condition1
then
    command1
elif condition2 
then 
    command2
else
    commandN
fi

for循環一般格式為:

for var in item1 item2 ... itemN
do
    command1
    command2
    ...
    commandN
done
  1. while 循環一般格式
while condition
do
    command
done
  1. until 循環一般格式
until condition
do
    command
done
  1. case in格式
case 值 in
模式1)
    command1
    command2
    ...
    commandN
    ;;
模式2)
    command1
    command2
    ...
    commandN
    ;;
esac
  1. 跳出循環:
  • break跳出所有循環
  • continue跳出當前循環

函數定義

輸入輸出重定向

  1. 文件描述符
  • 標準輸入 0
  • 標準輸出 1
  • 標準錯誤輸出 2
  1. stdin<file 標準輸入重定向
  2. stdout>file 標準輸出重定向
  3. stderr>>file 標準錯誤輸出重定向,以追加的方式

shell文件包含

  1. .filename
  2. source filename