Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

running bash script in cygwin on windows 7 [duplicate]

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.

like image 792
peter Avatar asked Jan 30 '13 07:01

peter


People also ask

How do I run bash on Windows 7?

Reboot machine. Search for "bash" and click on it, it should open a command prompt and ask you if you want to install "Ubuntu on Windows", continue with "y" After installation it will ask to create a UNIX username and password. You are now ready to use the bash shell.

Does Cygwin use bash?

The Cygwin installation creates a Bash shell along with a UNIX environment by which you can compile and run UNIX-like programs. Using this one can even create an emulated X server on your windows box and run UNIX-like GUI software tools.


2 Answers

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.

like image 151
William Avatar answered Sep 20 '22 19:09

William


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.

like image 20
EJK Avatar answered Sep 20 '22 19:09

EJK