bash extglob模式匹配在包含在函数中时中断

bash extglob模式匹配在包含在函数中时中断,bash,function,shopt,extglob,Bash,Function,Shopt,Extglob,这很有效 shopt -s extglob find /usr/!(^*|@*) -maxdepth 0 -cmin +1 -exec echo {} \; shopt -u extglob 这将返回一个错误 syntax error near unexpected token `(' function test { shopt -s extglob find /usr/!(^*|@*) -maxdepth 0 -cmin +1 -exec echo {} \; shopt -u

这很有效

shopt -s extglob
find /usr/!(^*|@*) -maxdepth 0 -cmin +1 -exec echo {} \;
shopt -u extglob
这将返回一个错误

syntax error near unexpected token `('

function test {
  shopt -s extglob
  find /usr/!(^*|@*) -maxdepth 0 -cmin +1 -exec echo {} \;
  shopt -u extglob
}

test

我缺少什么,可以在函数中使用它?

问题是bash需要两次打开extglob:

  • 在分析脚本时

  • 在执行实际命令时

  • 通过将
    shopt
    包含到函数体中,1。不满意。如果将
    shopt
    的范围扩大到包含函数声明,bash将正确解析函数,但在运行函数时会失败(即2.不满足):

    shopt-s extglob
    功能测试{
    查找/usr/!(^*|@*)-maxdepth 0-cmin+1-exec echo{}\;
    }
    shopt-u extglob
    
    错误:

    find: ‘/usr/!(^*|@*)’: No such file or directory
    
    因此,只需在脚本开头打开
    shoptextglob
    ,就可以了。 或者,如果您真的需要在其他地方将其关闭,请在函数内部和外部将其打开和关闭:

    #/bin/bash
    shopt-s extglob
    功能测试{
    shopt-s extglob
    查找/usr/!(^*|@*)-maxdepth 0-cmin+1-exec echo{}\;
    shopt-u extglob
    }
    shopt-u extglob
    测试
    
    问题是bash需要两次打开
    extglob

  • 在分析脚本时

  • 在执行实际命令时

  • 通过将
    shopt
    包含到函数体中,1。不满意。如果将
    shopt
    的范围扩大到包含函数声明,bash将正确解析函数,但在运行函数时会失败(即2.不满足):

    shopt-s extglob
    功能测试{
    查找/usr/!(^*|@*)-maxdepth 0-cmin+1-exec echo{}\;
    }
    shopt-u extglob
    
    错误:

    find: ‘/usr/!(^*|@*)’: No such file or directory
    
    因此,只需在脚本开头打开
    shoptextglob
    ,就可以了。 或者,如果您真的需要在其他地方将其关闭,请在函数内部和外部将其打开和关闭:

    #/bin/bash
    shopt-s extglob
    功能测试{
    shopt-s extglob
    查找/usr/!(^*|@*)-maxdepth 0-cmin+1-exec echo{}\;
    shopt-u extglob
    }
    shopt-u extglob
    测试