Linux – Cygwin did not run the script correctly

Cygwin did not run the script correctly… here is a solution to the problem.

Cygwin did not run the script correctly

Hello, I am trying to run this script in cygwin but it doesn’t work. Although when running under linux, it works fine.

Error:

$ ./script2.txt >> count.txt
./script2.txt: line 3: syntax error near unexpected token `|'
'/script2.txt: line 3: `    | grep PlayerCount \

Original script:

#!/usr/bin/bash
wget --output-document=- http://runescape.com/title.ws 2>/dev/null \
    | grep PlayerCount \
    | head -1l \
    | sed 's/^[^>]*>//' \
    | sed "s/currently.*$/$(date '+%r %b %d %Y')/"

What was someone asking for:

$ od -a script2.txt
0000000   #   !   /   u   s   r   /   b   i   n   /   b   a   s   h  cr
0000020  nl   w   g   e   t  sp   -   -   o   u   t   p   u   t   -   d
0000040   o   c   u   m   e   n   t   =   -  sp   h   t   t   p   :   /
0000060   /   r   u   n   e   s   c   a   p   e   .   c   o   m   /   t
0000100   i   t   l   e   .   w   s  sp   2   >   /   d   e   v   /   n
0000120   u   l   l  sp   \  cr  nl  sp  sp  sp  sp   |  sp   g   r   e
0000140   p  sp   P   l   a   y   e   r   C   o   u   n   t  sp   \  cr
0000160  nl  sp  sp  sp  sp   |  sp   h   e   a   d  sp   -   1   l  sp
0000200   \  cr  nl  sp  sp  sp  sp   |  sp   s   e   d  sp   '   s   /
0000220   ^   [   ^   >   ]   *   >   /   /   '  sp   \  cr  nl  sp  sp
0000240  sp  sp   |  sp   s   e   d  sp   "   s   /   c   u   r   r   e
0000260   n   t   l   y   .   *   $   /   $   (   d   a   t   e  sp   '
0000300   +   %   r  sp   %   b  sp   %   d  sp   %   Y   '   )   /   "
0000320

Solution

You need that your file does not have a Windows-style end-of-line line.

dos2unix script2.txt script2.txt

Alternatively, use your editor to set line endings. For example, in vim, you would use :set ff=unix

Related Problems and Solutions