zoukankan      html  css  js  c++  java
  • Learning_the_bash_Shell_Third_Edition 5/n

    Patterns and Pattern Matching

     wildcard(通配符)

    Operator

    Meaning

    ${variable#pattern}

    If the pattern matches the beginning of the variable’s value, delete the shortest part that matches and return the rest.

    ${variable##pattern}

    If the pattern matches the beginning of the variable’s value, delete the longest part that matches and return the rest.

    ${variable%pattern}

    If the pattern matches the end of the variable’s value, delete the shortest part that matches and return the rest.

    ${variable%%pattern}

    If the pattern matches the end of the variable’s value, delete the longest part that matches and return the rest.

    ${variable/pattern/string}

    The longest match to pattern in variable is replaced by string. In the first form, only the first match is replaced. In the second form, all matches are replaced. If the pattern begins with a #, it must match at the start of the variable. If it begins with a %, it must match with the end of the variable. If string is null, the matches are deleted. If variable is @ or *, the operation is applied to each positional parameter in turn and the expansion is the resultant list.

    ${variable//pattern/string}

    The longest match to pattern in variable is replaced by string. In the first form, only the first match is replaced. In the second form, all matches are replaced. If the pattern begins with a #, it must match at the start of the variable. If it begins with a %, it must match with the end of the variable. If string is null, the matches are deleted. If variable is @ or *, the operation is applied to each positional parameter in turn and the expansion is the resultant list.

    The classic use for pattern-matching operators is in stripping off components of pathnames, such as directory prefixes and filename suffixes. With that in mind, here is an example that shows how all of the operators work. Assume that the variable path has the value /home/cam/book/long.file.name; then:

    Expression Result
    ${path##/*/} long.file.name
    ${path#/*/} cam/book/long.file.name
    $path /home/cam/book/long.file.name
    ${path%.*} /home/cam/book/long.file
    ${path%%.*} /home/cam/book/long
  • 相关阅读:
    [RTT例程练习] 3.1 动态内存管理之rt_malloc和rt_free
    [RTT例程练习] 3.3 静态内存管理,内存池mempool
    [RTT例程练习] 6.2 在 Finsh 中运行自定义函数
    [RTT例程练习] 2.9 事件机制event
    [SCons 有点翻译的scons学习] 3. 生成和使用库
    [RTT例程练习] 3.2 动态内存管理之rt_realloc和free
    vim 启动 python的自动补全
    [RTT例程练习] 6.1 Finsh 的基本使用
    ELF文件重定位
    [RTT例程练习] 4.2 动态定时器
  • 原文地址:https://www.cnblogs.com/winditsway/p/14473116.html
Copyright © 2011-2022 走看看