简体   繁体   English

模式在交互式 shell 中有效,但在脚本中无效

[英]Pattern works in interactive shell but not in script

In my interactive shell I can store a pattern in a variable在我的交互式 shell 中,我可以将模式存储在变量中

$ targets="?(pcm52|pcm61)"
$ ls -l config/devdesc.$targets
-rw-r--r-- 1 kfa kfa 113 Dec 16 13:43 config/devdesc.pcm52
-rw-r--r-- 1 kfa kfa  14 Dec 16 13:44 config/devdesc.pcm61

But if I create a shell script但是如果我创建一个 shell 脚本

targets="?(pcm52|pcm61)"
ls -l config/devdesc.$targets

I get this error running it我得到这个错误运行它

$ bash -x /tmp/e.sh
+ targets='?(pcm52|pcm61)'
+ ls -l 'config/devdesc.?(pcm52|pcm61)'
ls: cannot access 'config/devdesc.?(pcm52|pcm61)': No such file or directory

Why does it fail when put into a script?为什么放入脚本时会失败?

From Bash manual page:来自 Bash 手册页:

If the extglob shell option is enabled using the shopt builtin, several extended pattern matching operators are recognized.如果使用内置shopt启用了extglob shell 选项,则可以识别多个扩展模式匹配运算符。

So just add shopt -s extglob to the beginning your e.sh script, since it does not inherit that non-default option from your interactive session.因此,只需将shopt -s extglob添加到您的e.sh脚本的开头,因为它不会从您的交互式 session 继承该非默认选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM