Shell指令碼中替換字串等操作
一、判斷讀取字串值
表示式 含義 ${var} 變數var的值, 與$var相同 ${var-DEFAULT} 如果var沒有被宣告, 那麼就以$DEFAULT作為其值 * ${var:-DEFAULT} 如果var沒有被宣告, 或者其值為空, 那麼就以$DEFAULT作為其值 * ${var=DEFAULT} 如果var沒有被宣告, 那麼就以$DEFAULT作為其值 * ${var:=DEFAULT} 如果var沒有被宣告, 或者其值為空, 那麼就以$DEFAULT作為其值 * ${var+OTHER} 如果var聲明瞭, 那麼其值就是$OTHER, 否則就為null字串 ${var:+OTHER} 如果var被設定了, 那麼其值就是$OTHER, 否則就為null字串 ${var?ERR_MSG} 如果var沒被宣告, 那麼就列印$ERR_MSG * ${var:?ERR_MSG} 如果var沒被設定, 那麼就列印$ERR_MSG * ${!varprefix*} 匹配之前所有以varprefix開頭進行宣告的變數 ${[email protected]} 匹配之前所有以varprefix開頭進行宣告的變數
加入了“*” 不是意思是: 當然, 如果變數var已經被設定的話, 那麼其值就是$var.
[[email protected]localhost ~]$ echo ${abc-'ok'} ok [[email protected] ~]$ echo $abc
[[email protected] ~]$ echo ${abc='ok'} ok [[email protected] ~]$ echo $abc ok
如果abc 沒有宣告“=" 還會給abc賦值。
[[email protected] ~]$ var1=11;var2=12;var3= [[email protected] ~]$ echo ${[email protected]
} var1 var2 var3 [[email protected] ~]$ echo ${!v*} var1 var2 var3${!varprefix*}與${[email protected]}相似,可以通過變數名字首字元,搜尋已經定義的變數,無論是否為空值。
二、字串操作(長度,讀取,替換)
表示式 含義 ${#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} 使用$replacement, 來代替第一個匹配的$substring ${string//substring/replacement} 使用$replacement, 代替所有匹配的$substring ${string/#substring/replacement} 如果$string的字首匹配$substring, 那麼就用$replacement來代替匹配到的$substring ${string/%substring/replacement} 如果$string的字尾匹配$substring, 那麼就用$replacement來代替匹配到的$substring
說明:"* $substring”可以是一個正則表示式.
1.長度
[[email protected] ~]$ test='I love china' [[email protected] ~]$ echo ${#test} 12
${#變數名}得到字串長度
2.擷取字串
[[email protected] ~]$ test='I love china' [[email protected] ~]$ echo ${test:5} e china [[email protected] ~]$ echo ${test:5:10} e china
${變數名:起始:長度}得到子字串
3.字串刪除
[[email protected] ~]$ test='c:/windows/boot.ini' [[email protected] ~]$ echo ${test#/} c:/windows/boot.ini [[email protected] ~]$ echo ${test#*/} windows/boot.ini [[email protected] ~]$ echo ${test##*/} boot.ini
[[email protected] ~]$ echo ${test%/*} c:/windows [[email protected] ~]$ echo ${test%%/*}
${變數名#substring正則表示式}從字串開頭開始配備substring,刪除匹配上的表示式。
${變數名%substring正則表示式}從字串結尾開始配備substring,刪除匹配上的表示式。
注意:${test##*/},${test%/*} 分別是得到檔名,或者目錄地址最簡單方法。
4.字串替換
[[email protected] ~]$ test='c:/windows/boot.ini' [[email protected] ~]$ echo ${test/\//\\} c:\windows/boot.ini [[email protected] ~]$ echo ${test//\//\\} c:\windows\boot.ini
${變數/查詢/替換值} 一個“/”表示替換第一個,”//”表示替換所有,當查詢中出現了:”/”請加轉義符”\/”表示。
三、效能比較
在shell中,通過awk,sed,expr 等都可以實現,字串上述操作。下面我們進行效能比較。
[[email protected] ~]$ test='c:/windows/boot.ini' [[email protected] ~]$ time for i in $(seq 10000);do a=${#test};done;
real 0m0.173s user 0m0.139s sys 0m0.004s
[[email protected] ~]$ time for i in $(seq 10000);do a=$(expr length $test);done;
real 0m9.734s user 0m1.628s
速度相差上百倍,呼叫外部命令處理,與內建操作符效能相差非常大。在shell程式設計中,儘量用內建操作符或者函式完成。使用awk,sed類似會出現這樣結果。