一.统计字符串长度

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基础--字符串和变量的操作的更多相关文章

  1. shell基础——字符串处理(转载)

    Shell的字符串处理   1 得到长度   %x="abcd"  #方法一      %expr length $x      4  # 方法二      %echo ${#x} ...

  2. 『忘了再学』Shell基础 — 21、变量的测试与内容置换

    目录 1.什么是变量的测试与内容置换 2.变量的测试与内容置换 3.示例 例1: 例2: 例3: 1.什么是变量的测试与内容置换 我们之前说过,在Shell中,一个变量未定义,和一个变量为空值的输出效 ...

  3. Linux学习笔记(16)shell基础之Bash变量

    1. 用户自定义变量 (1)变量设置规则 ① 变量名称可由字母.数字和下划线组成,但不能以数字开头: ② 变量的默认类型为字符串类型,如果要对数值运算,则必须指定变量类型为数值型: ③ 变量用等号连接 ...

  4. 『忘了再学』Shell基础 — 11、变量定义的规则和分类

    目录 1.定义变量的规则 2.变量的分类 1.定义变量的规则 在定义变量时,有一些规则需要遵守 变量名称可以由字母.数字和下划线组成,但是不能以数字开头.如果变量名是2name则是错误的. 在Bash ...

  5. shell 将字符串作为变量名并打印

    使用shell的eval实现此功能.代码如下: #!/bin/sh IP9="127.0.0.1" i=9 eval echo \$IP${i} #!/bin/sh WEBIP0= ...

  6. shell基础:环境变量

    子shell是在父shell中打开的shell. 使用pstree查看进程树. $调用环境变量 set查看所有变量内容, env查询环境变量 只是临时改变

  7. shell基础——字符串连接

    #!/bin/sh str1="hello" str2="world" echo str1=$str1, str2=$str2 strconn1=$str1$s ...

  8. shell基础:位置参数变量

    位置参数名称,作用不变.变得是传入参数. 抽象问题,大多为年长资格老师少数年轻老师,故而问的技术细节少,抽象理论知识多,比如什么是软件工程,问什么会有软件工程.有事注重的是品质,有的注重出身. 每种都 ...

  9. shell基础:用户自定义变量

随机推荐

  1. drawRect:和layoutSubview的区别

    关于这两个方法的区别 还是有点意思的. UIView的setNeedsDisplay和setNeedsLayout方法.首先两个方法都是异步执行的.setNeedsDisplay会调用自动调用draw ...

  2. ZAB 算法

    ZAB (Zookeeper Atomic Broadcast )  zookeeper原子消息广播协议 保证:分布式数据一致性  所有事务请求必须由一个全局唯一的服务器来协调处理,这样的服务器被称为 ...

  3. python学习之老男孩python全栈第九期_数据库day002知识点总结 —— MySQL数据库day2(全部)

    一. 复习1. MySQL: - 服务端 - 客户端2. 通信交流 - 授权 - SQL语句 - 数据库 创建数据库: create database db1 default charset utf8 ...

  4. Nib加载的方式实现自定义TableView

    实现的效果 实现原理: 通过在主界面中用Bundle 的LoadNib的方式进行自定义窗体文件加载并渲染界面 实现步骤 Step One 创建TableViewCell 窗体 Step Two 定义接 ...

  5. 使用 Vuejs 开发 chrome 插件的注意事项

    使用 Vuejs 开发 chrome 插件 chrome 插件的开发其实并不难,web开发者可以使用 html, css, javascript 轻松的开发实用的 chrome 插件. 一个好的 ch ...

  6. html打造动画【系列3】- 小猫笑脸动画

    猫咪容器 咱们每次画一个图片,肯定先要确定一个容器,几确定一下图形的位置和大小. <div class="mao_box"> <div class="m ...

  7. Ubuntu下编译opencv 和Ubuntu使用ffmpeg实现音频、视频的抽取

    一.使用Ubuntu编译opencv (前提是Ubuntu内已经正确配置了opencv,个人采用opencv3.2) g++ 1.cpp -o 1 `pkg-config --cflags --lib ...

  8. Java集合 -- HashSet 和 HashMap

    HashSet 集合 HashMap 集合 HashSet集合 1.1 Set 接口的特点 Set体系的集合: A:存入集合的顺序和取出集合的顺序不一致 B:没有索引 C:存入集合的元素没有重复 1. ...

  9. Django settings介绍

    """ Django settings for macboy project. Generated by 'django-admin startproject' usin ...

  10. 如何忽略.gitignore文件的提交

    1.默认的.gitignore文件无法忽略,如果想要忽略可以把.gitignore里面的文件转移到项目下面的 .git/info/exclude 里面, 2..gitignore可以直接使用插件,参照 ...