[英]Bash script and regex issue
Let's imagine that on my server, i have the following files : 让我们想象一下,在我的服务器上,我有以下文件:
foo@bar:/var/log/foo$ ls
fooFile1 fooFile2 logFile logFile.1 logFile.2
I wanted to create an archive containing every file but logFile.*. 我想创建一个包含每个文件的存档,但是logFile。*。 So I came up with the command :
所以我想出了命令:
foo@bar:~$ tar -czf foo.tar.gz /var/log/foo/!(logFile.*)
And it works fine ! 它工作正常! However, when trying to create a script which does the same job :
但是,在尝试创建执行相同工作的脚本时:
#!/bin/bash
tar -czf foo.tar.gz /var/log/foo/!(logFile.*)
I came across the following error : 我遇到了以下错误:
./test.sh: line 3: syntax error near unexpected token '('
./test.sh: line 3: 'tar -czf foo.tar.gz /var/log/foo/!(logFile.*)'
The problem is not tar related (I met the same issue using ls
instead of tar
) 这个问题与tar没有关系(我使用
ls
而不是tar
遇到了同样的问题)
I don't understand where this error is coming from. 我不明白这个错误来自哪里。 Could anyone tell me where i am wrong ?
谁能告诉我哪里错了?
Met on Ubuntu 12.04 在Ubuntu 12.04上遇到过
Place this line just below shebang: 将这条线放在shebang下面:
shopt -s extglob
to enable extended glob option in your script. 在脚本中启用扩展的glob选项。
!(logFile.*)
is extended glob pattern that requires enabling extglob
!(logFile.*)
是扩展的glob模式,需要启用extglob
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.