在作shell批處理程序時候,常常會涉及到字符串相關操做。有不少命令語句,如:awk,sed均可以作字符串各類操做。 其實shell內置一系列操做符號,能夠達到相似效果,你們知道,使用內部操做符會省略啓動外部程序等時間,所以速度會很是的快。web
1、判斷讀取字符串值正則表達式
表達式 | 含義 |
---|---|
${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開頭進行聲明的變量 |
${!varprefix@} | 匹配以前全部以varprefix開頭進行聲明的變量 |
加入了「*」 不是意思是: 固然, 若是變量var已經被設置的話, 那麼其值就是$var.shell
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
[chengmo<a href=
"http://www.jobbole.com/members/localhost"
>@localhost<
/a
> ~]$
echo
${abc-
'ok'
}
ok
[chengmo<a href=
"http://www.jobbole.com/members/localhost"
>@localhost<
/a
> ~]$
echo
$abc
[chengmo<a href=
"http://www.jobbole.com/members/localhost"
>@localhost<
/a
> ~]$
echo
${abc=
'ok'
}
ok
[chengmo<a href=
"http://www.jobbole.com/members/localhost"
>@localhost<
/a
> ~]$
echo
$abc
ok
若是abc 沒有聲明「=" 還會給abc賦值。
[chengmo<a href=
"http://www.jobbole.com/members/localhost"
>@localhost<
/a
> ~]$ var1=11;var2=12;var3=
[chengmo<a href=
"http://www.jobbole.com/members/localhost"
>@localhost<
/a
> ~]$
echo
${!
v
@}
var1 var2 var3
[chengmo<a href=
"http://www.jobbole.com/members/localhost"
>@localhost<
/a
> ~]$
echo
${!
v
*}
var1 var2 var3
${!varprefix*}與${!varprefix@}類似,能夠經過變量名前綴字符,搜索已經定義的變量,不管是否爲空值
|
2、字符串操做(長度,讀取,替換)編程
表達式 | 含義 |
---|---|
${#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」能夠是一個正則表達式.windows
1.長度app
1
2
3
|
[web97@salewell97 ~]$
test
=
'I love china'
[web97@salewell97 ~]$
echo
${
#test}
12
|
${#變量名}獲得字符串長度函數
2.截取字串性能
1
2
3
4
5
|
[chengmo<a href=
"http://www.jobbole.com/members/localhost"
>@localhost<
/a
> ~]$
test
=
'I love china'
[chengmo<a href=
"http://www.jobbole.com/members/localhost"
>@localhost<
/a
> ~]$
echo
${
test
:5}
e china
[chengmo<a href=
"http://www.jobbole.com/members/localhost"
>@localhost<
/a
> ~]$
echo
${
test
:5:10}
e china
|
${變量名:起始:長度}獲得子字符串spa
3.字符串刪除code
1
2
3
4
5
6
7
8
9
10
|
[chengmo<a href=
"http://www.jobbole.com/members/localhost"
>@localhost<
/a
> ~]$
test
=
'c:/windows/boot.ini'
[chengmo<a href=
"http://www.jobbole.com/members/localhost"
>@localhost<
/a
> ~]$
echo
${
test
#/}
c:
/windows/boot
.ini
[chengmo<a href=
"http://www.jobbole.com/members/localhost"
>@localhost<
/a
> ~]$
echo
${
test
#*/}
windows
/boot
.ini
[chengmo<a href=
"http://www.jobbole.com/members/localhost"
>@localhost<
/a
> ~]$
echo
${
test
##*/}
boot.ini
[chengmo<a href=
"http://www.jobbole.com/members/localhost"
>@localhost<
/a
> ~]$
echo
${
test
%/*}
c:
/windows
[chengmo<a href=
"http://www.jobbole.com/members/localhost"
>@localhost<
/a
> ~]$
echo
${
test
%%/*}
|
${變量名#substring正則表達式}從字符串開頭開始配備substring,刪除匹配上的表達式。
${變量名%substring正則表達式}從字符串結尾開始配備substring,刪除匹配上的表達式。
注意:${test##*/},${test%/*} 分別是獲得文件名,或者目錄地址最簡單方法。
4.字符串替換
1
2
3
4
5
|
[chengmo<a href=
"http://www.jobbole.com/members/localhost"
>@localhost<
/a
> ~]$
test
=
'c:/windows/boot.ini'
[chengmo<a href=
"http://www.jobbole.com/members/localhost"
>@localhost<
/a
> ~]$
echo
${
test
/\
//
\\}
c:\windows
/boot
.ini
[chengmo<a href=
"http://www.jobbole.com/members/localhost"
>@localhost<
/a
> ~]$
echo
${
test
//
\
//
\\}
c:\windows\boot.ini
|
${變量/查找/替換值} 一個「/」表示替換第一個,」//」表示替換全部,當查找中出現了:」/」請加轉義符」\/」表示。
3、性能比較
在shell中,經過awk,sed,expr 等均可以實現,字符串上述操做。下面咱們進行性能比較。
1
2
3
4
5
6
7
8
|
[chengmo<a href=
"http://www.jobbole.com/members/localhost"
>@localhost<
/a
> ~]$
test
=
'c:/windows/boot.ini'
[chengmo<a href=
"http://www.jobbole.com/members/localhost"
>@localhost<
/a
> ~]$
time
for
i
in
$(
seq
10000);
do
a=${
#test};done;
real 0m0.173s
user 0m0.139s
sys 0m0.004s
[chengmo<a href=
"http://www.jobbole.com/members/localhost"
>@localhost<
/a
> ~]$
time
for
i
in
$(
seq
10000);
do
a=$(
expr
length $
test
);
done
;
real 0m9.734s
user 0m1.628s
|
速度相差上百倍,調用外部命令處理,與內置操做符性能相差很是大。在shell編程中,儘可能用內置操做符或者函數完成。使用awk,sed相似會出現這樣結果。
若是想深刻體驗LINUX系統的新手,也能夠先下載一個方德Linux軟件中心試用一下。
免費下載地址:http://www.nfs-cloud.cn:81/appCenter/open/softcenter