zoukankan      html  css  js  c++  java
  • 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]#

  • 相关阅读:
    eventbus3-intellij-plugin插件搜不到
    flutter控件之CheckBox
    Java中常见数据结构:list与map -底层如何实现
    flutter控件之RadioButton
    git add Untracked files
    执行git push出现"Everything up-to-date"
    用flutter写一个精美的登录页面
    Android Studio最全插件整理
    Mac下git的环境搭建和基本使用
    上周热点回顾(7.1-7.7)团队
  • 原文地址:https://www.cnblogs.com/ajilisiwei/p/6681975.html
Copyright © 2011-2022 走看看