Shell--字符串

好久没有用shell脚本,这节总结一些字符串的相关知识,字符串是Shell脚本最常用的数据类型。

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

  • 单引号

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

  • 双引号
    1、双引号对字符串中出现的$、”、`和\进行替换,其他字符原样输出;
    2、双引号可以出现反引号,转义字符

  • 反引号

    1、反引号有命令替换的作用

    $ echo the date is `date`
    the date is 2018714日 星期六 174525秒 CST

    2、反引号可以嵌套使用,但内层的单引号必须加上\ 进行转移,

    $ abc=`echo The number of users is \`who| wc -l\``
    $ echo $abc
    The number of users is 2
    

字符串常见操作表达式

这里写图片描述

  • 字符串比较
四种判断方式 ==  !=  < > (按ascii值比较大小)
str="abcd"
if [ "$str"x == "abcdsd"x ]; then

注:比较字符串是否相等,可以字符串后面+上个别的字符,如果str为空的话,可以防止表达式报错:[: =: unary operator expected

检测字符串是否为空

-z  检测字符串长度是否为0,为0返回 true。  [ -z $a ] 返回 false。
-n  检测字符串长度是否为0,不为0返回 true。 [ -n "$a" ] 返回 true。
str 检测字符串是否为空,不为空返回 true。   [ $a ] 返回 true
  • 字符串长度
string=abc12342341          //等号二边不要有空格  
echo ${#string}             //结果11  
expr length $string         //结果11  
  • 字符串截取
${string:position}$string中, 从位置$position开始提取子串
${string:position:length}$string中, 从位置$position开始提取长度为$length的子串

测试例子
string="abc12342341" 
echo ${string:4}      //2342341  从第4位开始截取后面所有字符串    
echo ${string:3:3}    //123      从第3位开始截取后面3echo ${string:3:6}    //123423   从第3位开始截取后面6echo ${string: -4}    //2341  :右边有空格   截取后4echo ${string:(-4)}   //2341  同上
echo ${str:(-6):5}    //34234 从倒数第6个位置向左提取5个字符字符串, 

注意:第四个例子,如果是负数的话最好用括号扩起来,或者加空格

  • 字符串匹配删除

规则

${string#substring}	从变量$string的开头, 删除最短匹配$substring的子串
${string##substring}	从变量$string的开头, 删除最长匹配$substring的子串
${string%substring}	从变量$string的结尾, 删除最短匹配$substring的子串
${string%%substring}	从变量$string的结尾, 删除最长匹配$substring的子串

测试例子

    test='c:/windows/boot.ini'
    $ echo ${test#/} (从头匹配斜杠/,删除匹配到最短的斜杠,没有匹配到,所以没有删除)
    c:/windows/boot.ini
    $ echo ${test#*/}  (删除 从头匹配删除匹配到最短以/结尾的字符串,*是匹配0个或者多个)
    windows/boot.ini
    $ echo ${test##*/} (删除 从头匹配匹配到最长的以/结尾的字符串,通常可以用来获取到文件名)
    boot.ini

    $ echo ${test%/*} (删除 从尾部匹配以/开始最短的字符串,通常可以获取到文件路径前缀)
    c:/windows
    $ echo ${test%%/*} (删除 从尾部匹配以/开始最长的字符串)
    c:

${test##*/} , 得到文件名
${test%/*} 得到目录地址

  • 字符串替换

表达式规则

${string/substring/replacement}	 	使用$replacement, 来代替第一个匹配的$substring
${string//substring/replacement}	使用$replacement, 代替所有匹配的$substring
${string/#substring/replacement}	如果$string的前缀匹配$substring, 那么就用$replacement来代替匹配到的$substring
${string/%substring/replacement}	如果$string的后缀匹配$substring, 那么就用$replacement来代替匹配到的$substring

测试例子

str="apple, tree, apple tree"
echo ${str/apple/APPLE}   # 替换第一次出现的apple
APPLE, tree, apple tree
echo ${str//apple/APPLE}  # 替换所有apple
APPLE, tree, APPLE tree
echo ${str/#apple/APPLE}  # 如果字符串str以apple开头,则用APPLE替换它
APPLE, tree, apple tree
echo ${str/%apple/APPLE}  # 如果字符串str以apple结尾,则用APPLE替换它(str是以tree结尾的)
apple, tree, apple tree

另一个例子

$ test='c:/windows/boot.ini'  
$ echo ${test/\//\\}  (匹配的为\/,匹配的子串/需要转义,所以添加\,同理替换的字符串\转义为\\)
c:\windows/boot.ini  
$ echo ${test//\//\\}  
c:\windows\boot.ini 
#${变量/查找/替换值} 一个“/”表示替换第一个,”//”表示替换所有,当查找中出现了:”/”请加转义符”\/”表示。 

猜你喜欢

转载自blog.csdn.net/wenqiang1208/article/details/81047747