shell特殊变量(3)
程序员文章站
2024-03-08 18:39:04
...
- 变量子串
- ${#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)删除
#简单使用
[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) 替换
[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) 变量子串小结
上一篇: Thinkphp5.0自动生成模块及目录的方法详解
下一篇: 队列641、设计循环双端队列
推荐阅读
-
shell特殊变量(3)
-
vue @vue/cli3环境变量和模式
-
python anaconda 安装 环境变量 升级 以及特殊库安装的方法
-
python anaconda 安装 环境变量 升级 以及特殊库安装的方法
-
centos6 shell 中判断一个变量是否为空 的方法
-
linux中shell变量$#,[email protected],$0,$1,$2的含义解释
-
【转】Shell中脚本变量和函数变量的作用域 博客分类: linux命令unix shell作用域shelllocal函数变量
-
【转】Shell中脚本变量和函数变量的作用域 博客分类: linux命令unix shell作用域shelllocal函数变量
-
Java两个变量的互换(不借助第3个变量)具体实现方法
-
学习shell咯3 博客分类: Linux Bash编程CC++C#