Noob: Syntax error near unexpected token 'else'


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Noob: Syntax error near unexpected token 'else'
# 1  
Old 05-22-2011
Noob: Syntax error near unexpected token 'else'

Hi guys,

Completely new to all this and finding it soooooooo hard. Any help appreciated.

The idea is to check a list of .conf files and maintain a hash, if it does not match its hash it needs to do some more stuff, but I can't even get this first bit sorted... No idea if any of this is right. ><!

Code:
#!/bin/bash

sum1=`md5sum *.conf | awk '{print $1}'`
sleep 10
sum2=`md5sum *.conf | awk '{print $1}'`

for file in *.conf
do
if [`"sum1" -ne "$sum2"`] then
	echo "$file blah"
else
	echo "$file no blah"
fi
done

# 2  
Old 05-22-2011
Syntax errors :
Code:
if [`"sum1" -ne "$sum2"`] then
.....

should be i guess
Code:
if [[ "$sum1" -ne "$sum2" ]]; then
....

Also you might want to check md5sum -c option and/or cmp utility.
# 3  
Old 05-22-2011
Hi thanks for the help, removing the ` from there helped. Though the double brackets still gave a syntax error. I changed it back to single brackets and that worked fine. Any idea why?

Also changed this bit...

Code:
sum1=`md5sum *.conf | awk '{print $1}'`
sleep 10
sum2=`md5sum *.conf | awk '{print $1}'`

to:

Code:
sum1=$(md5sum *.conf)
sum2=$(md5sum *.conf)

Oh and changed the -ne to a "=" for string comparison... And retyped the script. (For some reason that fixed the first syntax error I got)
------------------------------------------------------

This is what I have now:

Code:
#!/bin/bash

sum1=$(md5sum *.conf)
sum2=$(md5sum *.conf)

for file in *.conf
do
if [ "$sum1" = "$sum2" ]; then
	echo "Passed"
	exit
else
	echo "Fail"
fi
done

So now I'm getting all "Fails" although I know they should be passing, using it as I have it here as well as using the -c option you mentioned and comparing it to previous output saved in a text file. May it be an issue because I'm not using actual .conf files??? I mean I just made some .txt files and pop some words in there.

With CMP, can you compare the existing file to the md5sum *.conf commands output? Ideally I want to be able to run it in the script without too many text files all about the place.

This is hard! Smilie

Last edited by Jandiedonkerman; 05-22-2011 at 07:03 AM..
# 4  
Old 05-22-2011
Try this to monitor for change in md5sum in working directory.
Code:
cd $WORKDIR
md5sum *.conf > md5 
sleep 10
md5sum -c md5 2>/dev/null  | awk -F":" '/FAILED/ { print $1 } '

This will print out the files that have changed since the initial md5sum.
You can then use that for further processing.
This User Gave Thanks to Peasant For This Post:
# 5  
Old 05-22-2011
Smilie

ILY I can finally get on with the rest of the script. Smilie Slapped that into a function and everything I've been trying to get working for the last 8 hours works.

Though honestly I have -NO- clue what
Code:
md5sum -c md5 2>/dev/null  | awk -F":" '/FAILED/ { print $1 } '"

this bit does. :P
# 6  
Old 05-22-2011
Check manual of md5sum and output redirection in unix for better understanding.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Syntax error near unexpected token

Dears, While executing the below script im getting the error at line 30. Please let me know what changes to be done to fix this. test.sh: line 30: syntax error near unexpected token `done' test.sh: line 30: ` done ' #!/bin/sh # Rev. PA1 # author: eillops # date: 26-04-2018 # #... (1 Reply)
Discussion started by: Kamesh G
1 Replies

2. UNIX for Beginners Questions & Answers

Syntax error near unexpected token 'do'

Hello all, Please i have this command i used to zip different files in differents directory, but i have an error. Note that when i run the command in one directory it works fine. /X5/WORK/BGH/INV/REG/pdf/SEND/BGH12523/1/*.fo /X5/WORK/BGH/INV/REG/pdf/SEND/BGH24523/1/*.fo... (3 Replies)
Discussion started by: gillesi
3 Replies

3. How to Post in the The UNIX and Linux Forums

Syntax error near unexpected token `('

I have 2 files like a.txt and b.txt and the content is as below cat a.txt 810750125 117780 /BSCSQAT4A/bscsqat4a/lib/jar/wclt_common.jar 1803152428 13300 /BSCSQAT4A/bscsqat4a/lib/jar/WFMSSupportTool.jar 2663502779 67049 /BSCSQAT4A/bscsqat4a/lib/jar/wma.jar 687942896 665272... (1 Reply)
Discussion started by: ranabhavish
1 Replies

4. 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

5. 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

6. Shell Programming and Scripting

Syntax error near unexpected token `}' please help

I'm going mad not being able to get this to work. im assuming its only a simple mistake but its driving me bonkers trying to find it. Please if you can help me it would save me pulling my hair out!! Thanks #!/bin/bash -xv # #Config name="TEST Server" + name='TEST Server'... (6 Replies)
Discussion started by: Fisheh
6 Replies

7. UNIX for Dummies Questions & Answers

Syntax error near unexpected token

hi! just want to seek help on this error: syntax error near unexpected token 'do this is my script # !/bin/sh # for y in 27 25 do exemmlmx -c "ZEEI;" -n XRT$y >> blah done what can be wrong? thanks! (6 Replies)
Discussion started by: engr.jay
6 Replies

8. UNIX for Advanced & Expert Users

syntax error near unexpected token '{

Hi, I am running the following script through cygwin and getting below mentioned error. ******************************************* #!/bin/sh # constants WORK_DIR="deploy" INFOFILE="deploy.info" INTROFILE="Intro.sh" CMGMT_PKG="com.kintana.cmgmt.deploy" DEPLOY_PREFIX="mitg" ... (2 Replies)
Discussion started by: MandyR
2 Replies

9. Shell Programming and Scripting

Syntax error near unexpected token `done'

Hi all, Here is a simple script that is working in one server and is giving a syntax error in other server. Can somebody help me ? #!/bin/bash # ftp files done < $file errors: I tried..with no success: if ; then (21 Replies)
Discussion started by: Lenora2009
21 Replies

10. UNIX for Advanced & Expert Users

Syntax error near unexpected token

Hi, When I run the below shell script I'm getting the error " syntax error near unexpected token `" Script: REM :: File Name : Refresh_OTL.bat REM :: Parameters : %1 - Region REM :: : %2 - Cube Type REM :: : REM :: Notes : REM ============================== set ENVIRONMENT... (2 Replies)
Discussion started by: tomailraj
2 Replies
Login or Register to Ask a Question