15

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.

1
  • 1
    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. Commented Jan 30, 2013 at 7:33

2 Answers 2

43

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.

Sign up to request clarification or add additional context in comments.

6 Comments

export SHELLOPTS did the trick for me! but occasionally I still got "invalid option name" - why is that?
Instead of exporting SHELLOPTS (which has side effects, though extremely unlikely to matter), you may also define SHELLOPTS as a Windows environment variable and set it to "igncr". Every BASH instance will inherit this option besides setting its own defaults.
@William - Nice! I like your solution. It is far easier than my approach of converting each script.
+1 for explaining WHY "this comment is required".
Not working for me...
|
13

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.

1 Comment

you can download it here: sourceforge.net/projects/dos2unix -> unzip it, you should now have a file dos2unix.exe somewhere -> open a new cygwin terminal ->cd to your script -> run <path_to_your_unzipped_dos2unix.exe_file> myScript.sh -> remember to replace all "\" in your path with "/"

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.