zoukankan      html  css  js  c++  java
  • Shell总结03-shell变量的操作

    shell变量本质上都是字符串,而且为字符串操作提供了强大的功能

    字符串长度

    # 字符串长度
    stringY=123456789
    echo ${#stringY}                 # 9
    echo `expr length $stringY`      # 9
    echo `expr "$stringY" : '.*'`    # 9
    

    获取字符串子串

    stringX='0123456789abc'
    stringZ=0123456789
    
    echo ${stringZ:0}                 # 0123456789
    echo ${stringZ:1}                 # 123456789
    echo ${stringZ:7}                 # 789
    echo ${stringZ:7:2}               # 78
    # 使用:截取,下标从0开始
    # ${string:position}             #在 $string 中截取自 $position 起的字符串
    # ${string:position:length}      #在 $string 中截取自 $position 起,长度为 $length 的字符串
    
    # 使用expr截取
    echo `expr substr $stringZ 1 2`                          # 01
    echo `expr substr $stringZ 5 2`                          # 45
    # 使用expr截取,下标从1开始
    
    #echo `expr substr $stringX $position $length`  # 在$stringX中截取自$position起,长度为$length的字符串。
    echo `expr match ${stringX} '([0-9]{3})'`
    #在 $string 中截取substring字符串,其中 $substring 是正则表达式。                                                            
    

    子串替换

    stringZ=abcABC123ABCabc
    echo ${stringZ/abc/xyz}       # xyzABC123ABCabc
                                  # 将匹配到的第一个 'abc' 替换为 'xyz'。
    echo ${stringZ//abc/xyz}      # xyzABC123ABCxyz
                                  # 将匹配到的所有 'abc' 替换为 'xyz'。
    
    ${string/substring/replacement}
    #替换匹配到的第一个 $substring 为 $replacement。
    
    ${string//substring/replacement}
    #替换匹配到的所有 $substring 为 $replacement。
    
  • 相关阅读:
    文件搜索和图像裁剪
    Mat的复制
    map
    substr
    cin,scanf
    strstr
    Applying vector median filter on RGB image based on matlab
    sobel算子的一些细节
    matlab 有趣小细节
    高斯混合模型(GMM)
  • 原文地址:https://www.cnblogs.com/elfcafe/p/13127982.html
Copyright © 2011-2022 走看看