Thursday, 14 February 2013

bash unexpected end of file problem

bash unexpected end of file problem

Other day I was working for my research in Ubuntu and I needed to automate a process. I decide to write a bash script but my while loop was not working at all. Any condition and loop I wrote, it gave me a unexpected end of file error. I try to google for more than an hour but nothing was helpful. Even though my syntax seems to be correct but the terminal was giving me same error again and again. I was not able to figure out what was causing the error but, finally I was able to solve it.

There are several reasons for throwing this error when working with the bash script and mostly its due to syntax error. But, sometimes even  though every syntax is correct, you may still get this error and those times are really frustrating. It took me more than 2 hours to figure out the problem.

I figure our the cause of this problem. It is due to the line ending used by various OS. I figure out that when we save our file in gedit (ubuntu), it has 3 line ending options as,
1. Unix/linux
2. Mac
3. Windows.

By default, windows is selected as line ending, and if its the case then how much you write the correct syntax, bash will not run and give you the unexpected end of file error.

Just use the line ending as unix/linux and your bash script should work well this time. I hope this simple information is helpful.

Thank you and have a nice day...