Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I got this error after execute shell script as below.

$ ./script_test.sh
./script_test.sh: line 3: syntax error near unexpected token `$'do\r''
'/script_test.sh: line 3: `for ((j=0; j<"$N"; j++)) ; do

Script below

#!/bin/sh

# This script will launch 10 simulations for each value of the PARAM1_LIST.
# Simulations results will be stored in folders named "myTest-i-j", where i
# is the effective value of the PARAM1_LIST for each simulation and j
# is the 1 to 10 repetition for each i.

N=10
PARAM1_LIST=(1 2 5 10)

for (( j = 0 ; j < $N; j++ )); do
for i in ${PARAM1_LIST[@]}; do
java -jar N3Sim.jar myConfigFile.cfg myTest-${i}-${j} $i
done
done
share|improve this question
    
Hard to tell due to the formatting above, but it looks like you may need #!/bin/sh instead of !/bin/sh ? –  Kent Jul 24 '13 at 8:26
    
The OP seems to have written a full shebang, but markup ate the hash. –  Hennes Jul 24 '13 at 8:30
    
What error exactly? –  Journeyman Geek Jul 24 '13 at 8:32
    
You can add a set -x below the shebang line to get more debug information. –  Hennes Jul 24 '13 at 8:37
    
Also, I notice and error near $'do\r. That \r is a newline. Maybe that line got written or copied from a place with other line endings than which your interpreter expects. –  Hennes Jul 24 '13 at 8:40
show 1 more comment

2 Answers

up vote 0 down vote accepted

Your script contains DOS newlines CR LF instead of Unix ones LF. $'\r' is a bash escape sequence for the CR control character which should not be normally contained in Unix text files. It was at the end of line 3 which is now the line 11 as you probably added the shebang sequence and comments later.

Solution is to convert the newlines:

dos2unix script_test.sh

Also please note that the script is using bash construct of array and the shebang should be #!/bin/bash.

share|improve this answer
    
Thanks for the answer. I just need to convert the script to unix format using cygwin. Everything I put in hash-tag just a simple explanation to what I'm trying to do. –  leepyron Jul 25 '13 at 7:29
    
@leepyron I do not completely understand your comment. You can convert your script file using the dos2unix utility (as shown in the answer) which is part of Cygwin. If for any reason you cannot install the dos2unix package, you can use for example sed 's/\r//' script_test.sh >script_test-converted.sh or tr -d '\r' <script_test.sh >script_test-converted.sh. See Alternatives to unix2dos conversion. –  pabouk Jul 25 '13 at 15:10
    
@leepyron continuation: By "hash-tag" you mean the shebang sequence #!/bin/bash? It is a path to the script interpreter which will read and interpret your script file when you execute it. For example Korn Shell script would begin with #!/bin/ksh. –  pabouk Jul 25 '13 at 15:13
    
@leepyron As I and Glenn wrote earlier: In your case change the #!/bin/sh at the file beginning to #!/bin/bash because your script is a Bash script. –  pabouk Jul 25 '13 at 15:35
    
Hi @pabouk, I'm not sure what you're talking about. I'm still new with all this thing. My purpose only to create a script to automate simulations for a list of values of a parameter and repeat each simulation N times. I'm using an open source software with .cfg file. Right now, the script is working flawlessly after I convert it using dos2unix command in cygwin. Thanks for your attention. This is the guide that I follow: n3cat.upc.edu/tools/UserGuide/UserGuide.html –  leepyron Jul 26 '13 at 4:57
add comment

You are using bash features but using a sh interpreter. Change the shebang line to #!/bin/bash

To decomstrate:

$ /bin/sh
$ i=1
$ ((i++))
/bin/sh: i++: not found
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.