• ${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

  • 相关阅读:
    python-文件操作
    python之-字符编码
    课程总结
    IO流文件输出流的应用
    字符串的基本操作
    数据结构字符串实训报告
    窗口的切换
    事件处理
    Java异常处理
    二维数组实现转置
  • 原文地址:https://www.cnblogs.com/amma/p/5575679.html
Copyright © 2020-2023  润新知