13
votes

I am trying to run the below bash script in cygwin on windows 7

REPEATTIMES="$1"

if [ $# = 0 ]; then

    echo "Usage: fetch topN repeatTimes"
    exit 1
fi

for (( i=1; i<=$REPEATTIMES; i++ ))
do
    echo "ITERATION: $i"
    echo "GENERATING"

    log=thelogs/log 

    bin/nutch generate crawl/segment -topN 10 > $log
    batchId=`sed -n 's|.*batch id: \(.*\)|\1|p' < $log`

    echo "batch id: $batchId "

    # rename log file by appending the batch id
    log2=$log$batchId
    mv $log $log2
    log=$log2

    echo "FETCHING"
    bin/nutch fetch crawl/segments/$batchId >> $log

    echo "PARSING"
    bin/nutch parse crawl/segments/$batchId >> $log


    echo "UPDATING DB"
    bin/nutch updatedb crawl/crawldb crawl/segments/$batchId >> $log

    echo "Done "

done

But when i run it i get the error :

line 11 :syntax error near unexpected token '$'\r'

line 11 :'for (( i=1; i<= REPEATTIMES; i++ ))

The script works fine on a ubuntu server. But i need to run it now on a windows machine.

2
have you tried dos2unix on the file? If you edited it with Notepad or something and got CRLF characters in there that can screw things up. Cygwin will expect LF characters only as line breaks if I remember correctly.scanny

2 Answers

43
votes

If you can't fix all your scripts, you should be able to modify the EOL behavior in Cygwin by setting an option to ignore CRs:

set -o igncr

If you add this to your .bash_profile, it will be globally set by default when you login:

export SHELLOPTS
set -o igncr

You can also do this per script internally by putting this line just after the #! line:

(set -o igncr) 2>/dev/null && set -o igncr; # this comment is required

You need the comment to ignore the CR in that line which is read before the option takes effect.

13
votes

The latest version of Cygwin seems to only support files in Unix format (i.e. with \n for newlines as opposed to the DOS/Windows \r\n newline).

To fix this, run the /bin/dos2unix.exe utility, giving your script as the argument to the command:

e.g. /bin/dos2unix.exe myScript.sh

This will convert it to Unix format and you then should be able to run it.