Shell 的字符串处理
简单介绍了字符串的变量命名、截取、获取长度等操作,但通常我们对字符串的操作的需求远远不止这些,Shell本身一起已经内置了一些对字符串的操作。
判断和读取字符串
${var-default} 和 ${var=default}:如果var没有被声明,则使用$default为其值
a="linux" b="win" c=${d-$b} e=${a-$b} echo $c echo $e # 输出 # win # linux
${var:-default} 和 ${var:=default}:如果var没有被声明或者为空,则使用$default为其值
例子使用上例
c=${d-$b} e=${a-$b} echo $c echo $e # 输出 # win # linux
*${var+other} 和 ${var:+other}:如果var声明了,那么其值就是$other,否则就是null字符串(echo打印为一个空行)
a="linux" b="win" c=${d+$b} e=${a+$b} echo $c echo $e # 输出 # # win
${var?ERR_MSG} 和 ${var:?ERR_MSG}:如果var没有被声明,就会打印ERR_MSG(也就是说会打印出错误信息,用在调试中会非常有用)
a="linux" b="win" c=${d?$b} e=${a:?$b} echo $c echo $e # 输出 # test.sh:line 4: d: win
${!varprefix}* 和 ${!varprefix@}:匹配之前所有以varprefix开头进行声明的变量,这是一个数组
javaLang="java" javascriptLang="javascript" pythonLang="python" b="java123" for ele in ${!java*} do echo $ele done # 输出 # javaLang # javascriptLang
字符串长度获取、替换、截取
${#str}:获取长度
os="linux"
echo ${#os}
# 输出
# 5
${str:position:length}:从position位置开始截取长度为length的子串,其中length可以省略,省略则是截取到最后
lang="javascript" echo ${lang:4} echo ${lang:4:3} echo ${lang} # 输出 # script # sci # javascript
${str#substr}:从str开头删除最短匹配$substr的子串,匹配都是从开头匹配的,开头匹配不上则不删除
lang="javascript is good" echo ${lang#java*} echo ${lang#java} echo ${lang#[^b-c]} echo ${lang#[^j-z]} # 输出 # script is good # script is good # avascript is good # javascript is good
${str#substr}:从str开头删除最长匹配$substr的子串,匹配都是从开头匹配的,开头匹配不上则不删除
lang="javascript is good, good study" echo ${lang##*,} echo ${lang##java} # 输出 # good study # script is good, good study
${str%substr}:从str结尾删除最短匹配$substr的子串
lang="javascript is good, good,study" good="javascript is good, good study" echo ${lang%,*} echo ${good%,*} # 输出 # javascript is good, good # javascript is good
${str%%substr}:从str结尾删除最长匹配$substr的子串
lang="javascript is good, good,study" good="javascript is good, good study" echo ${lang%%,*} echo ${good%%,*} # 输出 # javascript is good # javascript is good
${str/substr/replacement}:使用$replacement来替代第一个匹配的substr子串
lang="java is good, javascript is good" lang2="python" bast="the best" echo ${lang/java/$lang2} echo ${lang/good/$bast} # 输出 # python is good, javascript is good # java is the best, javascript is good
${str//substr/replacement}:使用$replacement来替代所有匹配的substr子串
lang="java is good, javascript is good" lang2="python" bast="the best" echo ${lang//java/$lang2} echo ${lang//good/$bast} # 输出 # python is good, pythonscript is good # java is the best, javascript is the best
${str/#substr/replacement}:如果$str的前缀匹配$substr则使用$replacement替代
lang="java is good, javascript is good" lang2="python" echo ${lang/#java/$lang2} # 输出 # python is good, javascript is good
${str/%substr/replacement}:如果$str的后缀缀匹配$substr则使用$replacement替代
lang="java is good, javascript is good" bast="the bast" echo ${lang/%good/$bast} # 输出 # java is good, javascript is the best
使用实例
列出当前文件夹下所有文件含有的后缀名
function hasItem() { arr=$1; item=$2; for ele in ${arr[@]} do if [[ $item = $ele ]]; then return 0 fi done return 1 } arr=() for file in `ls` do ext=${file#*.} echo ${arr[@]} hasItem $arr $ext if [ $? -eq 1 ]; then length=${#arr[*]} if [ $length -eq 0 ]; then arr=($ext) else arr=("${arr[@]}" "${ext}") fi else arr=$arr fi done for name in ${arr[@]} do echo $name done
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论