头歌--shell脚本入门 变量、字符串--第3关:shell 字符串

任务描述

本关带领大家熟悉 shell 的变量并掌握其使用。

相关知识

字符串概念

     字符串是 shell 编程中最常用最有用的数据类型(除了数字和字符串,也没啥其它类型好用了),字符串可以用单引号,也可以用双引号,也可以不用引号。

单引号 双引号 反引号的区别

     双引号 中可以有变量; 双引号里可以先转义字符;双引号中的单引号输出时维持单引号不变。请下面的案例: 例 1:

  1. #! /bin/bash
  2. var="aaa
  3. bbb
  4. ccc"
  5. echo ${var}
  6. ##输出结果为
  7. aaa bbb ccc

例 2:

  1. #! /bin/bash
  2. var="aaa
  3. bbb
  4. ccc"
  5. echo "${var}"
  6. ##输出结果为
  7. aaa
  8. bbb
  9. ccc

     单引号: 会忽略所有的特殊字符,即任何字符都会原样输出,包括定义的变量; 单引号字串中不能出现单引号。

  1. #! /bin/bash
  2. var="aaa
  3. bbb
  4. ccc"
  5. echo '${var}'
  6. ##输出结果为
  7. ${var}
  8. ###单引号无法解析变量,只会原样输出

    反引号 :  有命令替换的作用见例 3;反引号可以嵌套使用,但内层的单引号必须加上\ 进行转义见例 4。 例 3:

  1. $ echo the date is `date`
  2. the date is Tue Feb 4 18:08:12 CST 2020
  3. #这里的反引号 `date` 及为命令"date" 的结果,因此像引用一个命令返回的结果作为变量用 反引号 替换,也可以使用 $(date)的方式来替换使用命令结果

例 4:

  1. $ abc=`echo The number of users is \`who| wc -l\``
  2. $ echo $abc
  3. The number of users is 4
  4. ## 这里 `who|wc -l` 是指 返回的正在登录系统的用户的个数的结果 为4
  5. ## 因为反引号嵌套了反引号,所以在需要加上\转义

字符串常见操作表达式


示例:

获取字符串长度

  1. string="abcdefg"
  2. echo ${#string}

字符串截取

  1. ${string:position} //在$string中, 从位置$position开始提取子串
  2. ${string:position:length} //在$string中, 从位置$position开始提取长度为$length的子串
  3. 测试例子
  4. string="abc12342341"
  5. echo ${string:4} //2342341 从第4位开始截取后面所有字符串
  6. echo ${string:3:3} //123 从第3位开始截取后面3位
  7. echo ${string:3:6} //123423 从第3位开始截取后面6位
  8. echo ${string: -4} //2341 :右边有空格 截取后4位
  9. echo ${string:(-4)} //2341 同上
  10. echo ${str:(-6):5} //34234 从倒数第6个位置向左提取5个字符字符串,

字符串匹配删除

  1. ${string#substring} //从变量$string的开头, 删除最短匹配$substring的子串
  2. ${string##substring} //从变量$string的开头, 删除最长匹配$substring的子串
  3. ${string%substring} //从变量$string的结尾, 删除最短匹配$substring的子串
  4. ${string%%substring} //从变量$string的结尾, 删除最长匹配$substring的子串
  5. 测试例子
  6. test='c:/windows/boot.ini'
  7. $ echo ${test#/} (从头匹配斜杠/,删除匹配到最短的斜杠,没有匹配到,所以没有删除)
  8. c:/windows/boot.ini
  9. $ echo ${test#*/} (删除 从头匹配删除匹配到最短以/结尾的字符串,*是匹配0个或者多个)
  10. windows/boot.ini
  11. $ echo ${test##*/} (删除 从头匹配匹配到最长的以/结尾的字符串,通常可以用来获取到文件名)
  12. boot.ini
  13. $ echo ${test%/*} (删除 从尾部匹配以/开始最短的字符串,通常可以获取到文件路径前缀)
  14. c:/windows
  15. $ echo ${test%%/*} (删除 从尾部匹配以/开始最长的字符串)
  16. c:

字符串替换

  1. 表达式规则
  2. ${string/substring/replacement} 使用$replacement, 来代替第一个匹配的$substring
  3. ${string//substring/replacement} 使用$replacement, 代替所有匹配的$substring
  4. ${string/#substring/replacement} 如果$string的前缀匹配$substring, 那么就用$replacement来代替匹配到的$substring
  5. ${string/%substring/replacement} 如果$string的后缀匹配$substring, 那么就用$replacement来代替匹配到的$substring
  6. 测试列子:
  7. str="apple, tree, apple tree"
  8. echo ${str/apple/APPLE} # 替换第一次出现的apple
  9. APPLE, tree, apple tree
  10. echo ${str//apple/APPLE} # 替换所有apple
  11. APPLE, tree, APPLE tree
  12. echo ${str/#apple/APPLE} # 如果字符串str以apple开头,则用APPLE替换它
  13. APPLE, tree, apple tree
  14. echo ${str/%apple/APPLE} # 如果字符串str以apple结尾,则用APPLE替换它(str是以tree结尾的)
  15. apple, tree, apple tree
  16. 测试列子2:
  17. $ test='c:/windows/boot.ini'
  18. $ echo ${test/\//\\} (匹配的为\/,匹配的子串/需要转义,所以添加\,同理替换的字符串\转义为\\)
  19. c:\windows/boot.ini
  20. $ echo ${test//\//\\}
  21. c:\windows\boot.ini
  22. #${变量/查找/替换值} 一个“/”表示替换第一个,”//”表示替换所有,当查找中出现了:”/”请加转义符”\/”表示。

字符串比较     四种判断方式 == != < > (按 ascii 值比较大小), == 相等则为真,!= 不相等则为真。

  1. str="bacd"
  2. ##判断字符串是否相等 这里的== 也可以换做 = 但是一般情况下使用==
  3. if [ "$str"x == "abcdsd"x ]; then
  4. echo "相等"
  5. fi
  6. ##不相等则为真
  7. if [ "$str"x != "abcdsd"x ]; then
  8. echo "不相等"
  9. fi
  10. ##注意 比较字符串是否相等,可以字符串后面+上个别的字符,如果str为空的话,可以防止表达式报错:[: =: unary operator expected

实践要求

按照右侧“代码文件”中提示的要求补全代码,本关的目的,是希望通过学习,对于字符串的使用能有一个大致的了解,在实际工作中遇到需要对某一个字符串处理的时候,能够找到对应的方法。

评测说明

     按要求补全代码后,点击测评按钮 ,这里点击评测按钮,平台会对你编写的代码进行测试,当你的结果与预期输出一致时,即为通过。

 代码 

# !/bin/bash

string1="Hello www.educoder.net, hello linux! hello Shell ,Hello CNCF,hello Kubernetes,redis"


#********* Begin *********#

##1、str1 为 替换string1中第一次出现的 "Hello"为"hello"后的字符串
str1=${string1/Hello/hello}

##2、str2 为 替换string1中所有的"Hello"为"hello"后的字符串
str2=${string1//Hello/hello}

##3、str3 为 删除string1中的字符串"Kubernetes,"后的字符串
str3=${string1/Kubernetes,/ }


##4、变量letnth为字符串string1的长度,判断string1长度 完成如下代码:
##如果超过50则输出 "Length over 50" 如果小于等于50则输出"Length less than 50 "

lenth=${#string1}

if [ $lenth -gt 50 ]; then

        echo "Length over 50"
else
        echo "Length less than 50"

fi


#*********  End  *********#

echo $str1
echo $str2
echo $str3

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值