Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have been trying to write a while loop bash script that will check the size of two directories, and echo that out to a text file in the www folder. After my while loop stubbornly refused to work, I decided to simplify it to make sure I could get aa basic "if statement" to work, but for some reason after hours of frustration, even with the most basic of if statements, it will not work. Any idea what I am missing?

echo "hello World"
#
a=1
b=2
echo "A: $a"
echo "B: $b"
if [ "$a" -eq "$b" ];
then
echo "equal"
else
echo "Not equal"
fi

Every time I run that, I get this error:

test.sh: line 12: syntax error near unexpected token `fi'

test.sh: line 12: `fi'

If I put "exit" or "done" at the end of the file, it tells me unexpected end of file.

Thanks so much!

share|improve this question
1  
Works for me as entered above. –  Bittrance May 9 '12 at 19:52

1 Answer 1

up vote 0 down vote accepted

I copied pasted your script and ran it through bash and got the expected output:

$ bash test1.sh
hello World
A: 1
B: 2
Not equal
$ cat test1.sh     
echo "hello World"
#
a=1
b=2
echo "A: $a"
echo "B: $b"
if [ "$a" -eq "$b" ];
then
echo "equal"
else
echo "Not equal"
fi

Perhaps you've got an extra linefeed after the fi or something? Can you check with cat -v or simply create a new script and copy/paste the above yourself?

share|improve this answer
    
Thank so much guys! I just realized that my encoding in Notepad++ had somehow gotten changed to "Windows/Dos" instead of "UNIX". I went and used the menu to switch it back, and now it works like a charm! –  DarthCaniac May 9 '12 at 20:03
    
<soapbox>Mind if I point out that it is good practice to always prefix shell scripts with the appropriate hashbang line, "#!/bin/sh" if it is really within the confines of POSIX shell syntax or "#!/bin/bash" if the script is assumed bash specific. Some environments DO get it wrong in unexpected ways if you don't. For example, some linux systems will use busybox, dash, ash... by default for a noninteractive script (eg cron driven), which usually ends up in a script that was written with bash specific features to almost work in the most confusing way. </soapbox> –  rackandboneman May 9 '12 at 22:25
    
@rackandboneman agreed, that's why I called this snippet using bash test1.sh. –  Bram May 10 '12 at 7:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.