标签: extglob

Pattern match does not work in bash script

Using the pattern match !("file1") does not work within a bash script but will work on the command line.

For example:

ls  !("file1"|"file2")
Run Code Online (Sandbox Code Playgroud)

This will list all files in directory except file1 and file2.

When that line is executed in a script this error is displayed:

./script.sh: line 1: syntax error near unexpected token `('
./script.sh: line 1: ` ls  !("file1"|"file2") ' 
Run Code Online (Sandbox Code Playgroud)

Regardless what is used rm -v !("file1"). The same error takes place. What is going on …

linux bash glob extglob

13
推荐指数
2
解决办法
469
查看次数

负参数在参数扩展中如何在extglob中工作

问题

的行为

!(pattern-list)
Run Code Online (Sandbox Code Playgroud)

特别是在参数扩展中使用时,我的工作方式不正常

${parameter/pattern/string}
Run Code Online (Sandbox Code Playgroud)

输入

a="1 2 3 4 5 6 7 8 9 10"
Run Code Online (Sandbox Code Playgroud)

测试用例

$ printf "%s\n" "${a/!([0-9])/}"
[blank]
#expected 12 3 4 5 6 7 8 9 10

$ printf "%s\n" "${a/!(2)/}"
[blank]
#expected  2 3 4 5 6 7 8 9 10

$ printf "%s\n" "${a/!(*2*)/}"
2 3 4 5 6 7 8 9 10
#Produces the behaviour expected in previous one, not sure why though

$ printf "%s\n" "${a/!(*2*)/,}"
,2 3 4 5 6 …
Run Code Online (Sandbox Code Playgroud)

bash extglob parameter-expansion

6
推荐指数
1
解决办法
158
查看次数

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

在我的交互式 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
Run Code Online (Sandbox Code Playgroud)

但是如果我创建一个shell脚本

targets="?(pcm52|pcm61)"
ls -l config/devdesc.$targets
Run Code Online (Sandbox Code Playgroud)

运行它时出现此错误

$ 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
Run Code Online (Sandbox Code Playgroud)

为什么放入脚本时会失败?

bash glob extglob

1
推荐指数
1
解决办法
56
查看次数

标签 统计

bash ×3

extglob ×3

glob ×2

linux ×1

parameter-expansion ×1