欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

shell特殊变量(3)

程序员文章站 2024-03-08 18:39:04
...
  1. 变量子串

shell特殊变量(3)

  1. ${#parameter}
#基础用法 
[aaa@qq.com ~]# oldboy=oldbing 
[aaa@qq.com ~]# echo ${oldboy}
oldbing
[aaa@qq.com ~]# echo ${#oldboy} 
7
#企业面试题 ##4.3-1面试题: I am oldboy linux,welcome to our training. 显示这串字符中 单词字符数大于6的单词 
##分析: 
###1.通过for循环 取出每个单词
###2.判断 字符数 > 6 ? 
###1)大于6显示 
###2)不大于 继续 读取下1个单词
[aaa@qq.com ~]# vim  /server/scripts/2.5-3-word.sh 
#!/bin/bash 
#author oldboy  
str="I am oldboy linux,welcome to our training. "
for n in  $str 
do   
     if [ ${#n} -gt  6 ]   #great  than    
      then        
          echo ${#n} $n    
     fi 
 done
 
 
 [aaa@qq.com ~]# vim  /server/scripts/2.5-3-word.sh 
 #!/bin/bash 
 #author oldboy 
str="I am oldboy linux,welcome to our training. "
for n in  $str
 do    
         if [ ${#n} -gt  6 ]   #great  than   
         then        
               echo ${#n} $n    
         fi 
 done




[aaa@qq.com ~]# echo I am oldboy linux,welcome to our training. I am oldboy linux,welcome to our training.
 [aaa@qq.com ~]# 
 [aaa@qq.com ~]# echo I am oldboy linux,welcome to our training.| 
 > awk -vRS="[ ,.]" 'length()>6'
 >  welcome 
 >  training

2)变量截取

[aaa@qq.com ~]# oldboy=oldboyedu.com 
[aaa@qq.com ~]# #echo ${oldboy:起始点:一共取多少个字符} [aaa@qq.com ~]# 
[aaa@qq.com ~]# echo ${oldboy:4:3} 
oye
 #截取功能字符是从0开始 
 [aaa@qq.com ~]# echo ${oldboy:3:3} 
 boy 
 [aaa@qq.com ~]# echo ${oldboy:3}
  boyedu.com

3)删除
shell特殊变量(3)

#简单使用
 [aaa@qq.com ~]# test='I am oldboy teacher' 
 [aaa@qq.com ~]# echo  ${test#I} 
 am oldboy teacher 
 [aaa@qq.com ~]# echo  ${test#o}
  I am oldboy teacher 
  [aaa@qq.com ~]# echo  ${test#I am o} 
  ldboy teacher 
  [aaa@qq.com ~]# echo  ${test#*o} 
  ldboy teacher 
  [aaa@qq.com ~]# echo  ${test##*o} 
  y teacher
[aaa@qq.com ~]# test='I am oldboy teacher' 
[aaa@qq.com ~]# 
[aaa@qq.com ~]# 
[aaa@qq.com ~]# echo ${test%r} 
I am oldboy teache 
[aaa@qq.com ~]# echo ${test%er} 
I am oldboy teach 
[aaa@qq.com ~]# echo ${test%o*} 
I am oldb 
[aaa@qq.com ~]# echo ${test%%o*} 
I am 
#应用 
##简单 sed 比较麻烦 使用 变量子串里面删除功能 
[aaa@qq.com ~]# net='/etc/sysconfig/networkscripts/ifcfg-eth0' [aaa@qq.com ~]# 
[aaa@qq.com ~]# echo ${net##*/} 
ifcfg-eth0 
[aaa@qq.com ~]# echo ${net%%/*}
[aaa@qq.com ~]# echo ${net%/*} 
/etc/sysconfig/network-scripts 
[aaa@qq.com ~]# 
[aaa@qq.com ~]# dirname  /etc/sysconfig/networkscripts/ifcfg-eth0 
/etc/sysconfig/network-scripts
 [aaa@qq.com ~]# basename  /etc/sysconfig/networkscripts/ifcfg-eth0 
 ifcfg-eth0
#使用linux命令  效率 比 变量子串 低 
aaa@qq.com ~]# time   for n  in  {1..10000} ;do echo ${#n} &>/dev/null ; done 
real    0m0.154s 
user    0m0.097s
sys 0m0.056s 
[aaa@qq.com ~]# time   for n  in  {1..10000} ;do echo ${n}|wc -L &>/dev/null ; done 
real    0m20.
292s user    0m8.
485s sys 0m11.692s
  • 小结:
  • 变量子串删除部分 主要用来在脚本中删除路径 里面的名字 或路径部分

4) 替换
shell特殊变量(3)

[aaa@qq.com ~]# export LANG=C 
[aaa@qq.com ~]# 
[aaa@qq.com ~]# test='I am oldboy teacher'
 [aaa@qq.com ~]# echo ${test/[a-z]/oldboy}
  I oldboym oldboy teacher 
  [aaa@qq.com ~]# echo ${test//[a-z]/oldboy}
   I oldboyoldboy oldboyoldboyoldboyoldboyoldboyoldboy oldboyoldboyoldboyoldboyoldboyoldboyoldboy 
   [aaa@qq.com ~]#
    [aaa@qq.com ~]# test='I am oldboy teacher' 
    [aaa@qq.com ~]# echo ${test//oldboy/}
     I am teacher 
     [aaa@qq.com ~]# echo ${test/oldboy/} #把oldboy替换为空 即删除 
     I am teacher

5) 变量子串小结
shell特殊变量(3)

shell特殊变量(3)
shell特殊变量(3)
shell特殊变量(3)

相关标签: 第二阶段架构