zoukankan      html  css  js  c++  java
  • bash's parameter expansion

    [bash's parameter expansion]

     #: find first from left, remove-left

     ##: find last from left, remove left

     %: find first from right, remove right

     %%: find last from right, remove right

     example 1:

    1 parameter     result
    2 -----------   ------------------------------
    3 ${NAME}       polish.ostrich.racing.champion
    4 ${NAME#*.}           ostrich.racing.champion
    5 ${NAME##*.}                         champion
    6 ${NAME%%.*}   polish
    7 ${NAME%.*}    polish.ostrich.racing

     example 2:

    1 parameter     result
    2 -----------   --------------------------------------------------------
    3 ${FILE}       /usr/share/java-1.4.2-sun/demo/applets/Clock/Clock.class
    4 ${FILE#*/}     usr/share/java-1.4.2-sun/demo/applets/Clock/Clock.class
    5 ${FILE##*/}                                                Clock.class
    6 ${FILE%%/*}
    7 ${FILE%/*}    /usr/share/java-1.4.2-sun/demo/applets/Clock

     string slice: 

    ${string:2:1}   # The third character of string (0, 1, 2 = third)
    ${string:1}     # The string starting from the second character
                    # Note: this is equivalent to ${string#?}
    ${string%?}     # The string with its last character removed.
    ${string: -1}   # The last character of string
    ${string:(-1)}  # The last character of string, alternate syntax
                    # Note: string:-1 means something entirely different; see below.
    
    ${file%.mp3}    # The filename without the .mp3 extension
                    # Very useful in loops of the form: for file in *.mp3; do ...
    ${file%.*}      # The filename without its last extension
    ${file%%.*}     # The filename without all of its extensions
    ${file##*.}     # The extension only, assuming there is one. If not, will expand to ${file}

     toupper (^) and tolower (,):

    1 # string='hello, World!'
    2 parameter     result
    3 -----------   --------------------------------------------------------
    4 ${string^}    Hello, World! # First character to uppercase
    5 ${string^^}   HELLO, WORLD! # All characters to uppercase
    6 ${string,}    hello, World! # First character to lowercase
    7 ${string,,}   hello, world! # All characters to lowercase

    assignment:

    1 ${var-word}             # if var is defined, use var; otherwise, "word"
    2 ${var+word}             # if var is defined, use "word"; otherwise, nothing
    3 ${var=word}             # if var is defined, use var; otherwise, use "word" AND...
    4                         #   also assign "word" to var
    5 ${var?error}            # if var is defined, use var; otherwise print "error" and exit

    参考: http://bash.cumulonim.biz/BashFAQ(2f)073.html

     

  • 相关阅读:
    一句话评论设计模式六大原则
    keystore和truststore
    对于纯Java项目,JVM 各个类加载器的加载目标是什么?
    Keytool 使用总结
    饥饿的消费者(Hungry Consumer)模型
    javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: No trusted c
    如何判断对象已死?
    JMX 管理
    通过 jstat gcutil 来学习JVM 内存分配策略与 GC 发生时机
    程序员也应该有点艺术范儿,不要把“老年代”叫成“Old Generation”
  • 原文地址:https://www.cnblogs.com/tekkaman/p/3461317.html
Copyright © 2011-2022 走看看