extglob的案例声明 [英] Case statement with extglob
问题描述
启用 extglob 时,我想将变量与
*( )x*
(在正则表达式中:/^ * x.*/
)
此:
main(){
shopt -s extglob
local line=' x bar'
case "$line" in
*( )x*) ;;
*) ;;
esac
}
main "$@"
给我一个语法错误.删除extglob括号,或将 shopt -s extglob
移到 main
之外,以解决外部问题.为什么?为什么 shopt -s extglob
命令需要放在外面?
is giving me a syntax error. Either removing the extglob parentheses or moving shopt -s extglob
outside of main
, to the outer scope, fixes the problem.
Why? Why does the shopt -s extglob
command need to be outside?
推荐答案
bash必须解析该函数才能创建该函数,并且由于您使用的扩展glob语法通常无效,因此无法解析该函数除非extglob在创建函数时上打开.
bash has to parse the function in order to create it, and since the extended glob syntax you're using would normally be invalid, it can't parse the function unless extglob is on when the function is created.
最终结果:在声明该函数和运行该函数时,必须同时启用extglob.函数中的
Net result: extglob has to be on both when the function is declared and when it runs. The shopt -s extglob
line in the function takes care of the second requirement, but not the first.
顺便说一句,在其他一些地方这可能是个问题.例如,如果您有一个 while
或 for
循环,则bash需要在开始运行之前解析整个循环.因此,如果循环中的某些内容使用了extglob,则必须在循环开始之前将其启用.
BTW, there are some other places where this can be a problem. For example, if you have a while
or for
loop, bash needs to parse the entire loop before beginning to run it. So if something in the loop uses extglob, you have to enable it before the beginning of the loop.
这篇关于extglob的案例声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!