Bash script and regex problem

Suppose I have the following files on my server:

foo@bar :/var/log/foo$ ls fooFile1 fooFile2 logFile logFile.1 logFile.2 

I wanted to create an archive containing all the files, but logFile. *. So I came up with a command:

foo@bar :~$ tar -czf foo.tar.gz /var/log/foo/!(logFile.*)

And everything works fine! However, when trying to create a script that 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 related to tar (I met the same problem using ls instead of tar )

I do not understand where this error came from. Can someone tell me where I am going wrong?

Meeting on Ubuntu 12.04

+7
linux bash regex
source share
1 answer

Put this line just below shebang:

 shopt -s extglob 

to enable the extended glob parameter in the script.

!(logFile.*) is an extended ball template that requires extglob be included

+7
source share

All Articles