zoukankan      html  css  js  c++  java
  • shell 几中专用修饰符 :- :+ := ${variable:offset:length}

    1.${variable:-word}
    ${variable:-word} 如果variable已经被设置了,且不为空,则代入它的值,否则代入word;

    $ fruit=peach

    $ echo ${fruit:-plum}

    peach

    $ echo ${newfruit:-apple}

    apple

     
    2.
    ${variable:=word}

    ${variable:=word}如果variable已经被设置且不为空,则代入它的值,否则代入word,并且在后面variable始终为word的值。位置参量不能用这种方式赋值。

    $ name=

    $ echo ${name:=peter}

    peter


    $ echo $name

      peter


     3.${variable:+word} 

    ${variable:+word} 如果变量variable已被设置且值为非空,代入word值,否则什么也不代入,

    $ foo=grapes

    $ echo ${foo:+pears}

    pears

    $ echo $foo

       grapes


    4. ${variable:?word} 

    ${variable:?word} 如果变量variable已被设置值且为非空,就代入它的值,否则输出word并从shell中退出,如果有省略了word,就会显示信息:parameter null or not set.

    $ echo ${namex:?"namex is undefined"}

      namex: namex is undefined


    5.$ echo ${y?]

    $ echo ${y?]

     y: parameter null or not set


    6.${variable:offset}

    ${variable:offset} 获得变量从variable值中位置从offset开始的子串,偏移为从0到串尾。

    ${variable:offset:length} 获得variable值位置从offset开始长度为length的子串。


    $var=notebook

    $ echo ${var:0:4}

     note

    $ echo ${var:4:4]

    book

  • 相关阅读:
    JAVA学习25天
    Java学习第24天
    Java学习第23天
    Java学习22天
    第3周
    Java21
    day23作业
    day23
    Typecho使用技巧
    搭建Typecho博客
  • 原文地址:https://www.cnblogs.com/xialiaoliao0911/p/7523964.html
Copyright © 2011-2022 走看看