欧美成人午夜免费全部完,亚洲午夜福利精品久久,а√最新版在线天堂,另类亚洲综合区图片小说区,亚洲欧美日韩精品色xxx

扣丁學堂Linux培訓簡述Shell編程之擴展變量

2019-03-01 15:16:31 2198瀏覽

在Linux開發(fā)技術中,關于Shell編程之擴展變量有多少的小伙伴知道或者是了解呢?本篇文章扣丁學堂Linux培訓小編就給讀者們分享一下Shell編程之擴展變量,希望對小伙伴有所幫助。



扣丁學堂Linux培訓簡述Shell編程之擴展變量



變量擴展:


變量擴展說明


Shell中變量擴展說明如下所示:


${var}:返回${var}的內(nèi)容

${#var}:返回${var}的字符長度


${var:offset}:返回${var}從位置offset之后開始提取字符至結(jié)束

${var:offset:length}:返回${var}從offset之后,提取長度為length的字符


${var#word}:返回從${var}開頭開始刪除最短匹配的word子符串

${var##word}:返回從${var}開頭開始刪除最長匹配的word子符串


${var%word}:返回從${var}結(jié)尾開始刪除最短匹配的word子符串

${var%%word}:返回從${var}結(jié)尾開始刪除最長匹配的word子符串


${var/oldstring/newstring}:使用newstring替換第一個匹配的字符oldstring

${var//oldstring/newstring}:使用newstring替換所有匹配的字符oldstring


${var:-word}:如果變量var的值為空或未賦值,則將word做為返回值,常用于防止變量為空或未定義而導致的異常

${var:=word}:如果變量var的值為空或未賦值,則將word賦值給var并返回其值。


${var:?word}:如果變量var的值為空或未賦值,則將word做為標準錯誤輸出,否則則輸出變量的值,常用于捕捉因變量未定義而導致的錯誤并退出程序

${var:+word}:如果變量var的值為空或未賦值,則什么都不做,否則word字符將替換變量的值


其中${var:-word}、${var:=word}、${var:?word}、${var:+word}中的冒號也可以省略,則將變量為空或未賦值修改為未賦值,去掉了為空的檢測, 即運算符僅檢測變量是否未賦值



量擴展示例:


[root@localhost init.d]# var="This is test string"
[root@localhost init.d]# echo $var
This is test string
[root@localhost init.d]# echo ${var}
This is test string
[root@localhost init.d]# echo ${#var} # 統(tǒng)計字符長度
19
[root@localhost init.d]# echo ${var:5} # 從第5個位置開始截取字符
is test string
[root@localhost init.d]# echo ${var:5:2} # 從第5個位置開始截取2個字符
is
[root@localhost init.d]# echo ${var#This} # 從開頭刪除最短匹配的字符 is
is test string
[root@localhost init.d]# echo ${var##This} # 從開頭刪除最長匹配的字符 is
is test string
[root@localhost init.d]# echo ${var%g} # 從結(jié)尾刪除最短匹配的字符 is
This is test strin
[root@localhost init.d]# echo ${var%%g} # 從結(jié)尾刪除最長匹配的字符 is
This is test strin
[root@localhost init.d]# echo ${var/is/newis} # 替換第一個匹配的字符
Thnewis is test string
[root@localhost init.d]# echo ${var//is/newis} # 替換所有匹配到的字符
Thnewis newis test string

[root@localhost init.d]# echo $centos # 變量未定義

[root@localhost init.d]# echo ${centos:-UNDEFINE} # 變量為空,返回UNDEFINE
UNDEFINE
[root@localhost init.d]# centos="CentOS"
[root@localhost init.d]# echo ${centos:-UNDEFINE} # 變量已經(jīng)定義,返回變量本身的值
CentOS
[root@localhost init.d]# unset centos # 取消變量值
[root@localhost init.d]# echo $centos

[root@localhost init.d]# result=${centos:=UNDEFINE}
[root@localhost init.d]# echo $result
UNDEFINE
[root@localhost init.d]# echo $centos # 變量值為空,則將UNDEFINE賦值給centos
UNDEFINE
[root@localhost init.d]# unset centos
[root@localhost init.d]# echo ${centos:?can not find variable centos}

-bash: centos: can not find variable centos # 變量值為空,輸出自定義錯誤信息

[root@localhost init.d]# centos="IS DEFINED"
[root@localhost init.d]# echo ${centos:?can not find variable centos}
IS DEFINED  #變量值已定義,則輸出變量值
[root@localhost init.d]# unset centos
[root@localhost init.d]# echo ${centos:+do nothing} # 變量值為空,什么都不操作輸出

[root@localhost init.d]# centos="do"
[root@localhost init.d]# echo ${centos:+do nothing} # 變量已賦值,則輸出自定義的消息
do nothing


以上就是扣丁學堂Linux在線學習小編給大家分享的Shell編程之擴展變量,希望對小伙伴們有所幫助,想要了解更多內(nèi)容的小伙伴可以登錄扣丁學堂官網(wǎng)咨詢。



想要學好Linux開發(fā)小編給大家推薦口碑良好的扣丁學堂,扣丁學堂有專業(yè)老師制定的Linux學習路線圖輔助學員學習,此外還有與時俱進的Linux視頻教程供大家學習,想要學好Linux開發(fā)技術的小伙伴快快行動吧??鄱W堂Linux技術交流群:422345477。



扣丁學堂微信公眾號


【關注微信公眾號獲取更多學習資料】



查看更多關于“Linux培訓資訊”的相關文章>>



標簽: Linux培訓 Linux視頻教程 紅帽Linux視頻 Linux學習視頻 Linux入門視頻

熱門專區(qū)

暫無熱門資訊

課程推薦

微信
微博
15311698296

全國免費咨詢熱線

郵箱:codingke@1000phone.com

官方群:148715490

北京千鋒互聯(lián)科技有限公司版權所有   北京市海淀區(qū)寶盛北里西區(qū)28號中關村智誠科創(chuàng)大廈4層
京ICP備2021002079號-2   Copyright ? 2017 - 2022
返回頂部 返回頂部