扣丁学堂Linux培训简述Shell编程之扩展变量

2019-03-01 15:16:31 1136浏览

在Linux开发技术中,关于Shell编程之扩展变量有多少的小伙伴知道或者是了解呢?本篇文章扣丁学堂Linux培训小编就给读者们分享一下Shell编程之扩展变量,希望对小伙伴有所帮助。



扣丁学堂Linux培训简述Shell编程之扩展变量



变量扩展:


变量扩展说明


Shell中变量扩展说明如下所示:


${var}:返回${var}的内容

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


${var:offset}:返回${var}从位置offset之后开始提取字符至结束

${var:offset:length}:返回${var}从offset之后,提取长度为length的字符


${var#word}:返回从${var}开头开始删除最短匹配的word子符串

${var##word}:返回从${var}开头开始删除最长匹配的word子符串


${var%word}:返回从${var}结尾开始删除最短匹配的word子符串

${var%%word}:返回从${var}结尾开始删除最长匹配的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} # 统计字符长度
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} # 从结尾删除最短匹配的字符 is
This is test strin
[root@localhost init.d]# echo ${var%%g} # 从结尾删除最长匹配的字符 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} # 变量已经定义,返回变量本身的值
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编程之扩展变量,希望对小伙伴们有所帮助,想要了解更多内容的小伙伴可以登录扣丁学堂官网咨询。



想要学好Linux开发小编给大家推荐口碑良好的扣丁学堂,扣丁学堂有专业老师制定的Linux学习路线图辅助学员学习,此外还有与时俱进的Linux视频教程供大家学习,想要学好Linux开发技术的小伙伴快快行动吧。扣丁学堂Linux技术交流群:422345477。



扣丁学堂微信公众号


【关注微信公众号获取更多学习资料】



查看更多关于“Linux培训资讯”的相关文章>>



标签: Linux培训 Linux视频教程 红帽Linux视频 Linux学习视频 Linux入门视频

热门专区

暂无热门资讯

课程推荐

微信
微博
15311698296

全国免费咨询热线

邮箱:codingke@1000phone.com

官方群:148715490

北京千锋互联科技有限公司版权所有   北京市海淀区宝盛北里西区28号中关村智诚科创大厦4层
京ICP备12003911号-6   Copyright © 2013 - 2019

京公网安备 11010802030908号