0

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!

DarthCaniac
  • 209
  • 1
  • 3
  • 8

1 Answers1

0

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?

Bram
  • 1,121
  • 6
  • 9
  • 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 09 '12 at 20:03
  • 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. – rackandboneman May 09 '12 at 22:25
  • @rackandboneman agreed, that's why I called this snippet using `bash test1.sh`. – Bram May 10 '12 at 07:01