shell基础--字符串和变量的操作
一.统计字符串长度
1.wc –L
[root@~_~day4]# echo "hello" | wc -L
5
2.expr length string
[root@~_~day4]# echo `expr length "hello"`
5
3.${#String}
[root@~_~day4]# a="hello"
[root@~_~day4]# echo ${#a}
5
压力测试实验:
[root@~_~~]# echo $chars
hello world
[root@~_~~]# time for i in $(seq 11111);do count=${#chars};done
real 0m0.069s
user 0m0.069s
sys 0m0.001s
[root@~_~~]# time for i in $(seq 11111);do count=`echo expr length "#chars"`;done
real 0m3.949s
user 0m0.481s
sys 0m3.853s
[root@~_~~]# time for i in $(seq 11111);do count=`echo ${chars}| wc -L`;done
real 0m13.643s
user 0m2.743s
sys 0m14.989s
注意:内置命令运行比管道快得多,速度快慢如下:${#string}>${string} |wc -L >${expr length "$string"}
二.字符串模式匹配
1.查看所有帮助:
首先:man bash
然后:/Parameter Expansion
2.截取字符串长度
${parameter:offset} :从offset截取到字符串结尾
${parameter:offset:length} :从offset截取长度为length的字符串
[root@~_~day4]# a="hello world"
[root@~_~day4]# echo ${a:2:3}
Llo
[root@~_~day4]# echo ${a:2}
llo world
3.替换
${parameter/pattern/string}:用一种匹配模式pattern匹配字符串,并用string替代
(1).${myString/#beginStr/replaceStr} :前面匹配
(2).${myString/%endStr/replaceStr}:后面匹配
(3).${myString/matchStr/replaceStr}:匹配第一个matchStr
[root@~_~day4]# a="hello world"
[root@~_~day4]# echo ${a/#hello/"你好"}
你好 world
[root@~_~day4]# echo ${a/%world/"世界"}
hello 世界
[root@~_~day4]# a="hello hello world"
[root@~_~day4]# echo ${a/hello/"你好"}
你好 hello world
4.删除
${parameter#word}:从变量string开头开始删除最短匹配$word子串
${parameter##word}:从变量string开头开始删除最长匹配$word子串
${parameter%word}:从变量string结尾开始删除最短匹配$word子串
${parameter%%word}:从变量string结尾开始删除最长匹配$word子串
[root@~_~day4]# echo ${a##hello}
hello worldworld
[root@~_~day4]# echo ${a#hello}
hello worldworld
[root@~_~day4]# a="hello hello worldworld"
[root@~_~day4]# echo ${a%world}
hello hello world
[root@~_~day4]# echo ${a%%world}
hello hello world
三.空值处理
1.${parameter:-word} Use Default Values. 当${parameter}的值为空或是没有设定,用word的值将作为表达式的值,否则${parameter}就是表达式的值
[root@~_~day4]# echo ${myvalue:-hello}
hello
[root@~_~day4]# echo ${myvalue}
[root@~_~day4]# myvalue="test"
[root@~_~day4]# echo ${myvalue:-hello}
test
2.${parameter:=word} Assign Default Values. 当${parameter}的值为空或是没有设定,将word的值赋予${parameter}将并作为表达式的值,否则${parameter}就是表达式的值
[root@~_~day4]# echo ${secondevalue:=hello}
hello
[root@~_~day4]# echo ${secondevalue}
hello
[root@~_~day4]#
3.${parameter:?word} Display Error if Null or Unset
当${parameter}值为空或者没有设定的时候,用[word]值作为标准错误输出提示并退出shell且返回非0状态。否则它就是该表达式的值
[root@~_~day4]# echo ${myparam:?"the value is empty"}
-bash: myparam: the value is empty
[root@~_~day4]# echo ${myparam}
[root@~_~day4]#
4.${parameter:+word} Use Alternate Value
当${parameter}值为空或者没有设定的时候,表达式返回null。否则用[word]替换表达式的值。
[root@~_~day4]# echo ${myparam:+"the value is empty"}
[root@~_~day4]# myparam="hello"
[root@~_~day4]# echo ${myparam:+"the value is empty"}
the value is empty
[root@~_~day4]#
shell基础--字符串和变量的操作的更多相关文章
- shell基础——字符串处理(转载)
Shell的字符串处理 1 得到长度 %x="abcd" #方法一 %expr length $x 4 # 方法二 %echo ${#x} ...
- 『忘了再学』Shell基础 — 21、变量的测试与内容置换
目录 1.什么是变量的测试与内容置换 2.变量的测试与内容置换 3.示例 例1: 例2: 例3: 1.什么是变量的测试与内容置换 我们之前说过,在Shell中,一个变量未定义,和一个变量为空值的输出效 ...
- Linux学习笔记(16)shell基础之Bash变量
1. 用户自定义变量 (1)变量设置规则 ① 变量名称可由字母.数字和下划线组成,但不能以数字开头: ② 变量的默认类型为字符串类型,如果要对数值运算,则必须指定变量类型为数值型: ③ 变量用等号连接 ...
- 『忘了再学』Shell基础 — 11、变量定义的规则和分类
目录 1.定义变量的规则 2.变量的分类 1.定义变量的规则 在定义变量时,有一些规则需要遵守 变量名称可以由字母.数字和下划线组成,但是不能以数字开头.如果变量名是2name则是错误的. 在Bash ...
- shell 将字符串作为变量名并打印
使用shell的eval实现此功能.代码如下: #!/bin/sh IP9="127.0.0.1" i=9 eval echo \$IP${i} #!/bin/sh WEBIP0= ...
- shell基础:环境变量
子shell是在父shell中打开的shell. 使用pstree查看进程树. $调用环境变量 set查看所有变量内容, env查询环境变量 只是临时改变
- shell基础——字符串连接
#!/bin/sh str1="hello" str2="world" echo str1=$str1, str2=$str2 strconn1=$str1$s ...
- shell基础:位置参数变量
位置参数名称,作用不变.变得是传入参数. 抽象问题,大多为年长资格老师少数年轻老师,故而问的技术细节少,抽象理论知识多,比如什么是软件工程,问什么会有软件工程.有事注重的是品质,有的注重出身. 每种都 ...
- shell基础:用户自定义变量
随机推荐
- 四、获取IP地址工具包
由于getHostAddress()方法在Linux下读取hosts文件获取的是127.0.0.1 InetAddress.getLocalHost().getHostAddress() 所以这里采用 ...
- MyBatis源码解析之日志记录
一 .概述 MyBatis没有提供日志的实现类,需要接入第三方的日志组件,但第三方日志组件都有各自的Log级别,且各不相同,但MyBatis统一提供了trace.debug.warn.error四个级 ...
- 查看Windows日志
之前,在Windows服务管理器中启动WCF服务时,出现“本地计算机上的XXX服务启动后停止.某些服务在未由其它服务或程序使用时将自动停止.”问题,最后通过查看Windows日志中的详细信息才得以解决 ...
- Spring boot 入门二:Spring Boot配置文件详解
一.自定义属性 当我们创建一个springboot项目的时候,系统默认会为我们在src/main/java/resources目录下创建一个application.properties.同时也支持ym ...
- html 里面的 role 属性是什么意义和用途
使用role属性告诉辅助设备(如屏幕阅读器)这个元素所扮演的角色,属于WAI-ARIA. 例如点击的按钮,就是role="button":会让这个元素可点击:本质上是增强语义性,当 ...
- Spring Hibernate JPA 联表查询 复杂查询
今天刷网,才发现: 1)如果想用hibernate注解,是不是一定会用到jpa的? 是.如果hibernate认为jpa的注解够用,就直接用.否则会弄一个自己的出来作为补充. 2)jpa和hibern ...
- Dynamics 365 App for Outlook 与 Dynamics 365 for Outlook(已被弃用)
在最新的版本中Dynamics 365 for Outlook(Outlook 客户端)已被弃用 随 Dynamics CRM 2016(版本 8.0)引入的 Dynamics 365 App for ...
- Keras & Theano 输出中间层结果
Keras & Theano get output of an intermediate layer 1.使用函数模型API,新建一个model,将输入和输出定义为原来的model的输入和想要 ...
- RxJava + Retrofit完成网络请求
1.前言 本文基于RxJava.Retrofit的使用,若是对RxJava或Retrofit还不了解的简友可以先了解RxJava.Retrofit的用法再来看这篇文章. 在这片文章之前分别单独介绍过R ...
- 通过ES6实现的Ajax类
个人学习用途而已,仅供参考. class Ajax { constructor(xhr) { xhr = window.XMLHttpRequest ? new XMLHttpRequest() : ...