zoukankan      html  css  js  c++  java
  • ${0##*/} 的意思

    ${0##*/}  是什么意思呢?

    我们做一个实验就知道了

    新建一个目录——mkdir -p /usr/school/grade/class/

    新建一个文件——touch /usr/school/grade/class/student

    在student文件里写上代码并保存——   

    echo $0
    echo ${0#*/}
    echo ${0##*/}

    运行代码——  sh  /usr/school/grade/class/student

    我们会得到三个结果分别是

    /usr/school/grade/class/student
    usr/school/grade/class/student
    student

    这个时候再来讲解那三条指令

    $0,$1,$2 反正前面有一个$美元符号的,都象征着变量,而$0象征本身shell脚本文件的名字,也就是 /usr/school/grade/class/student

    最右边的 / ,象征着你要寻找,你要匹配的东西,在哪里找呢?在文件名字 /usr/school/grade/class/student 里面找

    #象征要寻找最左边的/

    ##象征要寻找最右边的/

    而*是通配符,象征着任意长度的字符串

    所以在##条件下的 */ , 就象征着/usr/school/grade/class/这一段

    找到了这一段之后,就把它截取,扔掉,剩下的就是student这个文件名字

    所以 ${0##*/} 的作用是寻找一个路径下的具体文件名字

    /usr/school/grade/class/student,就得到 student


    再看官方解释

    ${parameter#word}
    ${parameter##word}
    The word is expanded to produce a pattern just as in filename expansion (see section 3.5.8 Filename Expansion). If the pattern matches the beginning of the expanded value of parameter, then the result of the expansion is the expanded value of parameter with the shortest matching pattern (the `#' case) or the longest matching pattern (the `##' case) deleted. If parameter is `@' or `*', the pattern removal operation is applied to each positional parameter in turn, and the expansion is the resultant list. If parameter is an array variable subscripted with `@' or `*', the pattern removal operation is applied to each member of the array in turn, and the expansion is the resultant list.

    再举一个例子

    在suse系统上,这个脚本 /sbin/service

    拥有ROOT权限,才能够使用service服务,否则会报错

    lijunda@linux-d5wb:~> /sbin/service
    service: only root can use service

    service这个东西,就是从上面/sbin/service 经过 echo ${0##*/}得到的

    可以cat里面的代码验证一下


    if test "$(id -u)" -ne 0; then
       echo "${0##*/}: only root can use ${0##*/}" 1>&2
       exit 1
    fi





    我们再看下一个,${0%/*}

    先贴出官网解释

    ${parameter%word}
    ${parameter%%word}
    The word is expanded to produce a pattern just as in filename expansion. If the pattern matches a trailing portion of the expanded value of parameter, then the result of the expansion is the value of parameter with the shortest matching pattern (the `%' case) or the longest matching pattern (the `%%' case) deleted. If parameter is `@' or `*', the pattern removal operation is applied to each positional parameter in turn, and the expansion is the resultant list. If parameter is an array variable subscripted with `@' or `*', the pattern removal operation is applied to each member of the array in turn, and the expansion is the resultant list.

    ${0##*/}
    ${0%/*}

    这两个命令的共同点,都是截取,丢掉,前者扔掉的是  */这部分,后者扔掉的是/*这部分

    不同点的地方在于,#是从左边头部开始寻找起,%是从尾部开始寻找起(If the pattern matches a trailing portion)

    ${0%/*} 这个命令,对于/usr/school/grade/class/student,从右边找起,得到是 /student 这个部分,然后扔掉

    如果是¥{0%%/*},找到的就是 /usr/school/grade/class/student

    综上所述

    对于同一个文件 /usr/school/grade/class/student 

    ${0%/*}得到前面/usr/school/grade/class

    ${0##*/}得到的是后面 student

  • 相关阅读:
    java 事件监听机制组成
    关于父进程和子进程的关系(UAC 绕过思路)
    Fort.js – 时尚、现代的进度提示效果
    Hive学习之函数DDL和Show、Describe语句
    js完美的div拖拽实例代码
    SSH2框架实现注冊发短信验证码实例
    再看C#中的托付和事件
    RGB(FFFFFF)转255:255:255
    单一目的聚集操作
    智慧城市,在中国的北海边再画一个圈——大连“中国首届智慧城市协同创新峰会”请你带好笔
  • 原文地址:https://www.cnblogs.com/amma/p/5575679.html
Copyright © 2011-2022 走看看