前言:自從大學畢業參加工做以來,接觸的開發工做都是在服務端完成,因而接觸了比較多的Linux當作開發機使用,或多或少有一些重複性的工做,因而開始琢磨學習一些shell腳本的知識,以便處理這些繁瑣的事情。html
shell種類有不少bash、sh、csh、ksh等,不一樣的環境下使用的也不一樣,不過bash應該是使用最普遍了~也是Linux發行版默認的;shell
主要就是掌握常見的命令以及語法格式吧~而後就能夠上手幹活了:數組
命令以下:bash
- echo:輸出命令到終端
- read:從終端中讀入命令
- readonly:將命令標記爲只讀命令
- unset:刪除變量,可是不能刪除readonly標記的變量
- printf:usage: printf format-string [arguments...] eg:printf "%s %s %s\n" a b c d e f g h i j
轉義字符:ide
- \\:反斜槓
- \a:警報
- \b:退格
- \f:換頁
- \n:換行
- \r:回車
- \t:tab鍵
- \v:垂直製表符
特殊變量:函數
- $0:當前腳本的文件名
- $n:表示傳遞給文件或者函數的參數,n是幾就表示第幾個參數
- $#:表示參數的個數
- $*:表示全部的參數
- $?:上個命令的退出狀態或則函數的返回值
- $$:當前shell的進程ID
- $@:也是表示全部參數,和$*的區別就是:"$@"會顯示全部的參數,每一個一行,"$*"將全部的參數顯示在一行
命令替換:`command`學習
DATE=`date`
echo "Date is $DATE"USERS=`who | wc -l`
echo "Logged in user are $USERS"UP=`date ; uptime`
echo "Uptime is $UP"
變量替換:測試
- ${var} 變量原本的值
- ${var:-word} 若是變量var爲空或則Unset,那麼返回word,可是不改變var的值
- ${var:=word}若是變量var爲空或則unset,那麼返回word,並將var的值設置爲word
- ${var:?messgage}若是變量var爲空或則unset,那麼messgae將送到標準錯誤輸出,腳本中止運行
- ${var:+word}若是變量var被定義,那麼返回word,但不改變var的值
運算符:url
- 算術運算符(expr):
- + 加法 `expr $a + $b`
- - 減法 `expr $a - $b`
- * 乘法 `expr $a \* $b`
- / 除法 `expr $b / $a`
- % 取餘 `expr $b % $a`
- = 賦值 a=$b 把變量b的值賦給a
- == 相等,用於比較兩個數字,相同則返回true [$a == $b]
- != 不相等。用於比較兩個數字,不相同則返回true [$a != $b]
- eg:
#!/bin/sh
a=10
b=20
val=`expr $b % $a`
18.echo "b % a : $val"
- 關係運算符
- -eq 檢測兩個數是否相等,相等則返回true
- -ne 檢測兩個數是否不相等,不相等則返回true
- -gt 檢測左邊的數是否大於右邊的數,若是是,則返回true
- -lt 檢測左邊的數是否小於右邊的數,若是是,則返回ture
- -ge 檢測左邊的數是否大於等於右邊的數,若是是,則返回true
- -le 檢測右邊的數是否小於等於右邊的數,若是是,則返回true
- eg:
#!/bin/sh
a=10
b=20
if [ $a -eq $b ]
then
echo "$a -eq $b : a is equal to b"
else
echo "$a -eq $b: a is not equal to b"
fi
- 布爾運算符:
- ! 非運算 [!false] 返回true
- -o 或運算 [$a -lt 20 -o $b -gt 100]
- -a 與運算 [$a -lt 20 -a $b -gt 100]
- 字符串運算符:中括號兩邊要留個空隙
- = 檢測兩個字符串是否相等,相等則返回true [ $a = $b ]返回false
- != 檢測兩個字符串是否相等,不相等返回true [ $a != $b ]返回true
- -z 檢測字符串長度是否爲0, 爲0返回ture [ -z $a ]
- -n 檢測字符串長度是否不爲0,不爲0返回true [ -n $a ]
- str 檢測字符串是否爲空,不爲空返回true [ $a ] 返回true
- 文件測試運算符:
- -b file 檢測是不是塊設備文件 [-b $file]
- -c file 檢測是否是字符設備文件 [-c $file]
- -d file 檢測是否是目錄 [-d $file]
- -f file 檢測是否是普通文件(既不是目錄,也不是設備文件) [-f $file]
- -g file 檢測是否設置了SGID位
- -k file 檢測是否設置了(sticky bit)
- -p file 檢測是否具備管道
- -u file 檢測是否設置了SUID位
- -r file 檢測是否可讀
- -w file 檢測是否可寫
- -x file 檢測是否能夠執行
- -s file 檢測是否爲空文件(大小爲0)
- -e file 檢測文件是否存在
字符串操做:spa
- ${#string} $string的長度
- ${string:position} 在$string中,從位置$position開始提取子串
- ${string:position:length} 在$string中,從位置$position開始提取長度爲$length的子串
- ${string#substring} 從變量$string的開頭,刪除最短匹配$substring的子串
- ${string##substring} 從變量$string的開頭,刪除最長匹配$substring的子串
- ${string%substring} 從變量$string的結尾,刪除最短匹配$substring的子串
- ${string%%substring} 從變量$string的結尾,刪除最長匹配$substring的子串
- ${string/substring/replacement} 在$string中用$replacement取代第一個匹配的$substring
- ${string//substring/replacement} 在$string中用$replacement取代所有匹配的$substrng
- ${string/#substring/replacement} 若是$string的前綴匹配$substring,那麼用$replacement取代$substring
- ${string/%substring/replacement} 若是$string的後綴匹配$substring,那麼用$replacement取代$substring
數組的操做:
定義數組:array_name=(value0 value1 value2 value3)數組的操做:
讀取數組:${array_name[2]}
讀取數組的元素的個數:${#array_name[@]}/${#array_name[*]}
返回數組的全部的下標:${!array_name[@]}/${!array_name[*]}
返回數組的全部的元素:${array_name[@]}/${!array_name[*]}
注意的事項:
- 變量的初始化中間不能有空格,eg: name="Spider"
- 變量的使用時候,最好以${name}這種形式,確認變量的邊界;eg: echo 'I am ${name}-spiders'
- 註釋只能用#號
- if..else使用:
- if then... fi 語句;
- if then... else ... fi 語句;
- if then... elif then... else ... fi 語句。
- case..switch
-
echo 'Input a number between 1 to 4'
echo 'Your number is:\c'
read aNum
case $aNum in
1) echo 'You select 1'
;;
2) echo 'You select 2'
;;
3) echo 'You select 3'
;;
4) echo 'You select 4'
;;
*) echo 'You do not select a number between 1 to 4'
;;
esac
- for循環:
-
for 變量 in 列表
do
command1
command2
...
commandN
done
- while循環:
-
while command
do
Statement(s) to be executed if command is true
done
- until循環:
-
until command
do
Statement(s) to be executed until command is false
done
- break and continue ,和其它語言不一樣的是,shell中的能夠後加數字n,表示第幾層循環
- 函數:
- 注意,$10 不能獲取第十個參數,獲取第十個參數須要${10}。當n>=10時,須要使用${n}來獲取參數
function_name () {
list of commands
[ return value ]
}
command > file 輸出重定向到file
- command < file 輸入重定向到file
- command >> file 輸出以追加的方式重定向到file
- n > file 將文件描述符重定向到file
- n >> file 將文件描述符以追加的方式重定向到file
- n >& m 將輸出文件m和n合併
- n <& m 將輸入文件m和n合併
- << tag 將開始標記tag和結束標記tag之間的內容做爲輸入
- 重定向,若是但願將 stdout 和 stderr 合併後重定向到 file,能夠這樣寫:
- 重定向到/dev/null 文件,屏蔽輸出結果
- 調試shell 腳本:在#! /bin/bash -x 就能夠看到腳本執行的順序
- 通配符
shell常見通配符:
字符 |
含義 |
實例 |
* |
匹配 0 或多個字符 |
a*b a與b之間能夠有任意長度的任意字符, 也能夠一個也沒有, 如aabcb, axyzb, a012b, ab。 |
? |
匹配任意一個字符 |
a?b a與b之間必須也只能有一個字符, 能夠是任意字符, 如aab, abb, acb, a0b。 |
[list] |
匹配 list 中的任意單一字符 |
a[xyz]b a與b之間必須也只能有一個字符, 但只能是 x 或 y 或 z, 如: axb, ayb, azb。 |
[!list] |
匹配 除list 中的任意單一字符 |
a[!0-9]b a與b之間必須也只能有一個字符, 但不能是阿拉伯數字, 如axb, aab, a-b。 |
[c1-c2] |
匹配 c1-c2 中的任意單一字符 如:[0-9] [a-z] |
a[0-9]b 0與9之間必須也只能有一個字符 如a0b, a1b... a9b。 |
{string1,string2,...} |
匹配 sring1 或 string2 (或更多)其一字符串 |
a{abc,xyz,123}b a與b之間只能是abc或xyz或123這三個字符串之一。 |