Cygwin Syntax Trouble

0

I'm on windows 7 using Cygwin.

My script and text file are located in the same directory.

#!/bin/bash
while read name; do
echo "Name read from file - $name"
done < /home/Matt/servers.txt

I get this error and I don't know why because this is correct while loop syntax..?

u0146121@U0146121-TPD-A ~/Matt
$ ./script.sh
./script.sh: line 4: syntax error near unexpected token `done'
./script.sh: line 4: `done < /home/Matt/servers.txt'

Can anybody tell me what I'm doing wrong? I think it's because I'm on windows and using Cygwin.

mkrouse

Posted 2013-07-01T15:27:06.350

Reputation: 71

Do you mean it worked on Unix? – golimar – 2013-07-01T15:35:26.333

@golimar no never tested it – mkrouse – 2013-07-01T15:42:23.710

why don't you do a for loop? IMHO it would be much more readable (sorry it does not answer the question) – pataluc – 2013-07-01T16:03:18.850

@pataluc: Because while read... is often the recommended method. – user1686 – 2013-07-01T16:10:02.007

Could you post the file you are reading from? Also try adding ; after the echo command and the done, just in case. – terdon – 2013-07-01T16:31:02.060

1Check the script for DOS-style line endings by printing it with cat -v /path/to/script and looking for "^M" at the end of lines." This doesn't really look like the errors I'd expect from this, but with Cygwin it's best to check. – Gordon Davisson – 2013-07-02T07:28:19.627

I used this command to flip my script from the windows style endings. sed -i 's/\r$//' script – mkrouse – 2013-07-02T14:47:24.933

Answers

0

This script has CR LF line endings. This is more visible with od.

$ od -c script
0000000   #   !   /   b   i   n   /   b   a   s   h  \r  \n   w   h   i
0000020   l   e       r   e   a   d       n   a   m   e   ;       d   o
0000040  \r  \n   e   c   h   o       "   N   a   m   e       r   e   a
0000060   d       f   r   o   m       f   i   l   e       -       $   n
0000100   a   m   e   "  \r  \n   d   o   n   e       <       /   h   o
0000120   m   e   /   M   a   t   t   /   s   e   r   v   e   r   s   .
0000140   t   x   t  \r  \n
0000145

As you can see, I have \r (carriage return) and \n (line feed) characters at the end of each line where you should only have \n characters. This is a result of a compatibility issue between Windows and *nix systems. Bash has difficulty dealing with the \r characters.

You can fix your script by using a utility like dos2unix or by running the following sed command:

sed -i 's/\r$//' script

mkrouse

Posted 2013-07-01T15:27:06.350

Reputation: 71

0

The previous answer is correct in that Windows will place \r\n but *nix systems will only accept \n. However there is an extremely easy way to solve this on a Windows machine.

In a text editor such as Textpad, choose "Save As", and under the filename you can choose "Line Ending". Here, save it as "UNIX", and the file will be properly encoded with \r and your scrips will work just fine.

Olfactory Cone

Posted 2013-07-01T15:27:06.350

Reputation: 1