Bash Regexp failing: unexpected token `('


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash Regexp failing: unexpected token `('
# 1  
Old 01-27-2014
Question Bash Regexp failing: unexpected token `('

Hey guys,

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

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:

Code:
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
Code:
#!/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


Last edited by Eggie01; 01-27-2014 at 11:19 AM.. Reason: Complete error message added
# 2  
Old 01-27-2014
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:

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

# 3  
Old 01-28-2014
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.
Code:
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:

Code:
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! Smilie
# 4  
Old 01-28-2014
I guess you need to escape the parenthesis?

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

# 5  
Old 01-28-2014
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:
Code:
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

# 6  
Old 01-28-2014
Ok, I missed that point, which version of bash do you have?
# 7  
Old 01-28-2014
No worries mate!

bash --version 3.1.17(1)-release (x86_64-pc-linux-gnu)
Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Syntax error near unexpected token `else'

Hello every one!! I don't know where I am going wrong but I am finding it difficult to clear this error of syntax error near unexpected token `else' I am writing a simple shell script to find a file in a directory and if found execute that else return an error to the log file ... (14 Replies)
Discussion started by: masubram
14 Replies

2. Shell Programming and Scripting

-bash: syntax error near unexpected token `('

// AIX 6.1 I am getting a syntax error below. Please advise what to be corrected. :confused: runmqsc CERN.$(echo `hostname` | cut -d'.' -f1 | tr '' '').$(echo $environment | tr '' '') <<! | egrep -i '(FROM.NC.APPLIANCE)' | sort -u |awk '{print $2}' | cut -d '(' -f2 | cut -d ')' -f1 |... (1 Reply)
Discussion started by: Daniel Gate
1 Replies

3. Shell Programming and Scripting

For loop - unexpected token `do

My requirement is to search for current date-1 .log files in /usr/sas/sas_config/Lev1/SASApp/StoredProcessServer/Logs folder and write the file name to filenames.txt When I ran the script below, I got error as syntax error near unexpected token `do I'm not sure what is wrong in my code. I... (11 Replies)
Discussion started by: Ram Kumar_BE
11 Replies

4. Shell Programming and Scripting

Trying to pass a password: bash: syntax error near unexpected token `('

howdy, so I'm make a plugin work for Nagios, and the commandline is: /usr/lib/nagios/plugins/check_mssql -H MySQLServerName -u MySqlAccountName -p MyPassword(#XXXXX -d MyDatabaseName it is barfing with: bash: syntax error near unexpected token `(' Thoughts? Do I have to wrap something... (2 Replies)
Discussion started by: rgouette
2 Replies

5. Shell Programming and Scripting

Syntax error near unexpected token

Hi all, I have a simple script that doesn't work somehow. I can't seem to be spotting the cause of the malfunction. count=$((1)) for item in `cat test1.txt` printf %s `sed -n $((count))p test2.txt` > test3.txt count=$((count+1)) do something done I get ; ./why.sh: line 3:... (14 Replies)
Discussion started by: y33t
14 Replies

6. Shell Programming and Scripting

Syntax error near unexpected token `else'

Hi, I am trying to read the session log through script. But it keeps showing me some error near. I have tried everything. Even tried converting the script using sed command to remove the hidden characters(\r).But nothing seems to be working.Below is the script : #!/bin/bash cd... (6 Replies)
Discussion started by: Aryan12345
6 Replies

7. Shell Programming and Scripting

Syntax error near unexpected token `('

What do I do here? #!/bin/bash payload=-1 AND 1=IF(21,BENCHMARK(5000000,MD5(CHAR(115,113,108,109,97,112))),0)# hash=`echo -n $payload md5sum tr -d 'n' sed 'ss-sg' md5sum tr -d 'n' sed 'ss-sg'` curl --data cs2=chronopay&cs1=$payload&cs3=$hash&transaction_type=rebill... (2 Replies)
Discussion started by: iiiiiiiiiii
2 Replies

8. Shell Programming and Scripting

syntax error near unexpected token `='

Hi all, This is a script which converts hex to bin. However am finding an error while executing syntax error near unexpected token `=' `($hexfile, $binfile) = @ARGV;' I am running using ./fil.pl <hexfile> <binfile> ################################################### # # this script... (3 Replies)
Discussion started by: jaango123
3 Replies

9. Shell Programming and Scripting

Unexpected Token Error `;;'

Hello all, Im having an Issue with my script for switch statement , can someone let me know where do i need to correct it. 7 ##******************************************************************************************************* 8 ## ********** FUNCTION USAGE *********** ... (13 Replies)
Discussion started by: raghunsi
13 Replies

10. Shell Programming and Scripting

sh syntax error unexpected token done

I'm getting the following error: line 21: syntax error near unexpected token `done` line 21: `done` and I haven't been able to figure out why. Here is my code #!/bin/sh if ; then echo 'Usage: rename getexp/replStr ' exit 0 fi arg = $1 shift while ; do (5 Replies)
Discussion started by: NullPointer
5 Replies
Login or Register to Ask a Question