这是可行的
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我遗漏了什么,允许我在函数中使用它?
发布于 2021-01-02 04:11:38
问题是bash需要两次打开extglob:
解析脚本时的
执行实际命令时的
通过将shopt包含到函数体中,1.是不满足的。如果您扩大了shopt的范围以包含函数声明,bash将正确解析函数,但在运行它时将失败(即2。不满足):
shopt -s extglob
function test {
find /usr/!(^*|@*) -maxdepth 0 -cmin +1 -exec echo {} \;
}
shopt -u extglob错误:
find: ‘/usr/!(^*|@*)’: No such file or directory因此,只需在脚本的开头打开shopt extglob,就可以了。或者,如果你真的需要在其他地方关闭它,可以在函数内部和外部打开和关闭它:
#! /bin/bash
shopt -s extglob
function test {
shopt -s extglob
find /usr/!(^*|@*) -maxdepth 0 -cmin +1 -exec echo {} \;
shopt -u extglob
}
shopt -u extglob
testhttps://stackoverflow.com/questions/65533232
复制相似问题