Bash Regexp failing: unexpected token `('

Hey guys,

I'm fairly new to bash scripting, so bear with me :slight_smile:

What I want to achieve is reading a file (.php), find the phrase 'WACHTWOORD' (password) in it, and collect the password and a part of the filename in a list.
The filename is always 'settings_{name}.php' and the part I'm looking for in the file is this 'define('WACHTWOORD','password');'

I'm using 2 regexpressions and the first one seems to work just fine, but the second one gives me an error:

list.sh: line 27: syntax error in conditional expression: unexpected token `('
list.sh: line 27: syntax error near `*,\'(.'
list.sh: line 27: `		if [[ $line =~ *,\'(.*)\'* ]] ; then'

Here is my code, I hope anyone can explain why the second one fails.
It's probably worth noting the server runs bash --version 3.1.17(1)-release (x86_64-pc-linux-gnu).

Note: I have tried surrounding the regexp with both ' and *, both give the same message

#!/bin/bash

FILES="../settings_*.php"

for f in $FILES
do
        for line in `cat $f | grep "'WACHTWOORD'"`; do
                # Filter out Praktijk
                if [[ $f =~ '^../settings_(.*).php$' ]] ; then
                        praktijk="${BASH_REMATCH[1]}" 
                else
                        praktijk=$f 
                fi

                # Filter out password
                # the file contains: define('WACHTWOORD','password');
                if [[ $line =~ *,\'(.*)\'* ]] ; then
                        password="${BASH_REMATCH[1]}" 
                else
                        password=$line 
                fi

                echo $praktijk:$password
        done
done

Try quoting $f, like if [[ "$f" =~ ...

Also, for line in `cat file` is basically always wrong because for splits on whitespace, not lines... Not to mention, grep does not need cat's help to read one or more files... Try this:

while read line
do
...
done <(grep "'WACHTWOORD'" "$f")

Thanks for you answer mate, I rewrote the script to use 'while read line'.

But still, the problem persists. It is in the second regexp, not the first.

if [[ $line =~ *,\'(.*)\'* ]] ; then
     password="${BASH_REMATCH[1]}"
else
     password=$line
fi

It doesnt matter if I add double-quotes around $line or not, it still gives me:

test.sh: line 27: syntax error in conditional expression: unexpected token `('
test.sh: line 27: syntax error near `*,\'(.'
test.sh: line 27: `                if [[ $line =~ *,\'(.*)\'* ]] ; then'

Any other suggestions are welcome, cheers! :b:

I guess you need to escape the parenthesis?

if [[ $line =~ *,\'\(.*\)\'* ]] ; then

Thanks for the response!

That has crossed my mind as well, but I need to capture the data so it's actually a regular expression find rather than a literal set of parenthesis ( ).

Also, when I do escape them, I get another error:

test.sh: line 34: unexpected EOF while looking for matching `''
test.sh: line 36: unexpected argument to conditional binary operator
test.sh: line 36: syntax error: unexpected end of file

Ok, I missed that point, which version of bash do you have?

No worries mate!

bash --version 3.1.17(1)-release (x86_64-pc-linux-gnu)
:slight_smile: