LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Bash script problem: syntax error near unexpected token `do (https://www.linuxquestions.org/questions/programming-9/bash-script-problem-syntax-error-near-unexpected-token-%60do-897377/)

rmaier9 08-15-2011 05:52 AM

Bash script problem: syntax error near unexpected token `do
 
Hello,

I am having a problem running a bash script, and I get the following problem:

Code:

: command not found
'eatFlux: line 4: syntax error near unexpected token `do
'eatFlux: line 4: `do

The code is as follows:

Code:

#!/usr/bin/bash

for a in 10 20 30 40 50 60 70 80 90

do

        for b in 5 15 25 35 45 55 65 75 85

        do

                cp ./a$a/b$b/BoundaryHeatFlux.txt ./

                /home/CFD_Filmgruppe/HeatTransferAndMarangoni_Simulations/Studienarbeit_Robert_Maier/TestCases/TestCase_2D/k_as_function_of_a_B/Shell/heatFluxGather $a $b

                rm BoundaryHeatFlux.txt

        done

done


I have a bunch of test cases that I ran in various folders. Each folder has a .txt file with some data I would like to collect and write into one .txt file. The read and write part is executed by the 'heatFluxGather' code. The bash just retrieves the data. Any recommendations as to how to fix or improve the script would be nice too.

Thanks,

Robert

tommylovell 08-15-2011 07:49 AM

I changed your script like this (/usr/bin/bash to /bin/bash and preceded your cp, heatFluxGather and rm with echo),
Code:

[root@athlon ~]# cat heatFlux
#!/bin/bash

for a in 10 20 30 40 50 60 70 80 90

do

        for b in 5 15 25 35 45 55 65 75 85

        do

                echo cp ./a$a/b$b/BoundaryHeatFlux.txt ./

                echo /home/CFD_Filmgruppe/HeatTransferAndMarangoni_Simulations/Studienarbeit_Robert_Maier/TestCases/TestCase_2D/k_as_function_of_a_B/Shell/heatFluxGather $a $b

                echo rm BoundaryHeatFlux.txt

        done

done
[root@athlon ~]#

Then I ran heatFlux,
Code:

[root@athlon ~]# ./heatFlux | head
cp ./a10/b5/BoundaryHeatFlux.txt ./
/home/CFD_Filmgruppe/HeatTransferAndMarangoni_Simulations/Studienarbeit_Robert_Maier/TestCases/TestCase_2D/k_as_function_of_a_B/Shell/heatFluxGather 10 5
rm BoundaryHeatFlux.txt
cp ./a10/b15/BoundaryHeatFlux.txt ./
/home/CFD_Filmgruppe/HeatTransferAndMarangoni_Simulations/Studienarbeit_Robert_Maier/TestCases/TestCase_2D/k_as_function_of_a_B/Shell/heatFluxGather 10 15
rm BoundaryHeatFlux.txt
cp ./a10/b25/BoundaryHeatFlux.txt ./
/home/CFD_Filmgruppe/HeatTransferAndMarangoni_Simulations/Studienarbeit_Robert_Maier/TestCases/TestCase_2D/k_as_function_of_a_B/Shell/heatFluxGather 10 25
rm BoundaryHeatFlux.txt
cp ./a10/b35/BoundaryHeatFlux.txt ./
[root@athlon ~]#


I suspect you are having a problem with lineend characters. The 'file' command should tell you what they are (if that command is available on the Unix you are on).
Code:

[root@athlon ~]# file heatFlux
heatFlux: Bourne-Again shell script, ASCII text executable

Code:

[root@athlon ~]# unix2dos heatFlux
unix2dos: converting file heatFlux to DOS format ...
[root@athlon ~]# file heatFlux
heatFlux: Bourne-Again shell script, ASCII text executable, with CRLF line terminators

Code:

[root@athlon ~]# dos2unix heatFlux
dos2unix: converting file heatFlux to Unix format ...

[root@athlon ~]# file heatFlux
heatFlux: Bourne-Again shell script, ASCII text executable

[root@athlon ~]# unix2mac heatFlux
unix2dos: converting file heatFlux to Mac format ...

[root@athlon ~]# file heatFlux
heatFlux: Bourne-Again shell script, ASCII text executable, with CR line terminators
[root@athlon ~]#

To see what is actually in the file, type hexdump -C -n 64 heatFlux (if you have hexdump).
Code:

[root@athlon ~]# hexdump -C -n 64 heatFlux
00000000  23 21 2f 62 69 6e 2f 62  61 73 68 0a 0a 66 6f 72  |#!/bin/bash..for|
00000010  20 61 20 69 6e 20 31 30  20 32 30 20 33 30 20 34  | a in 10 20 30 4|
00000020  30 20 35 30 20 36 30 20  37 30 20 38 30 20 39 30  |0 50 60 70 80 90|
00000030  0a 0a 64 6f 0a 0a 09 66  6f 72 20 62 20 69 6e 20  |..do...for b in |
00000040

If you don't have hexdump, try od -c heatFlux | head
Code:

[root@athlon ~]# od -c heatFlux | head
0000000  #  !  /  b  i  n  /  b  a  s  h  \n  \n  f  o  r
0000020      a      i  n      1  0      2  0      3  0      4
0000040  0      5  0      6  0      7  0      8  0      9  0
0000060  \n  \n  d  o  \n  \n  \t  f  o  r      b      i  n   
0000100  5      1  5      2  5      3  5      4  5      5  5
0000120      6  5      7  5      8  5  \n  \n  \t  d  o  \n  \n
0000140  \t  \t  e  c  h  o      c  p      .  /  a  $  a  /
0000160  b  $  b  /  B  o  u  n  d  a  r  y  H  e  a  t
0000200  F  l  u  x  .  t  x  t      .  /  \n  \n  \t  \t  e
0000220  c  h  o      /  h  o  m  e  /  C  F  D  _  F  i
[root@athlon ~]#

See my lineend is '0a' or '\n'. You sometimes get this sort of problem when you do a binary ftp of an ascii file from one system to another, like from MS to Linux or Unix, or Mac to Linux or Unix. See http://stackoverflow.com/questions/2...ad-interpreter

If that's the case you can try dos2unix or mac2unix to fix the script file, although they not be available on your flavor of Unix. If bad lineend's are not the case then I can't help.

In terms of a suggested style, make your variables upper case ("for a in 10 20..." would be "for A in 10 20..."; and "cp ./a$a/b$b/..." and "...heatFluxGather $a $b" would be "cp ./a$A/b$B/..." and "...heatFluxGather $A $B".

I'd lose most of the extra blank lines you have inserted (probably for readability).

And I think you want an explicit path to the file in your remove command. (If you have a script with references to a particularly long path or one that is used multiple times, it's common to make it a variable that gets used in multiple places - just don't call it PATH.)

ps. you should mention what Unix you are running. My response is for Linux, but in general I think some of it should apply.

Hope this helps.

rmaier9 08-15-2011 09:15 AM

Thanks, tommylovell. This has been bothering me for a while now. Based on your recommendation, I just opened a new file and rewrote the script with some of your advice, and it worked great. Thanks a lot.

Cheers.

praju1 08-21-2014 05:20 AM

Thanks, tommylovell. In my case, I was trying to execute the script file(written on Windows system) on linux system. So tried with command as :
Code:

$ dos2unix abc.sh
dos2unix: converting file abc.sh to UNIX format ...



All times are GMT -5. The time now is 11:36 PM.