'while read line do' cause: "syntax error near unexpected token `done'" in Linux bash script
9,231
This is a clue to the problem:
test.sh: line 98: syntax error near unexpected token `done' 'est.sh: line 98: `done < isam_subrack2.txt 2> /dev/null
That second line would normally read like this
test.sh: line 98: `done < isam_subrack2.txt 2> /dev/null'
As you can see, the trailing quote mark from the error message is at the beginning of the line. This is symptomatic of unexpected CR characters in the text file. I see that you have run the file through dos2unix
but I would still recommend you run it through something similar once more:
tr -d '\015' < src.sh > dst.sh
Related videos on Youtube
Author by
Murat
Updated on September 18, 2022Comments
-
Murat almost 2 years
I have a shell script which is running fine on Solaris platform but not working on Linux platform. The Failing code snippet is as following:
[...] while read line do insert_into_table="insert into isam_subrack_tbl (select neId, friendlyName, eqptHolderActualType from $line)" $MYSQL_HOME/bin/mysql --socket=/tmp/mysql.sock -u$MYSQL_USER -p$MYSQL_PWD --host $MYSQL_HOST -Demlplatform -e "$insert_into_table" done < isam_subrack2.txt 2> /dev/null [...]
When I run the script in debug mode:
bash-4.1$ sh -vvx test.sh [...] while read line do insert_into_table="insert into isam_subrack_tbl (select neId, friendlyName, eqptHolderActualType from $line)" $MYSQL_HOME/bin/mysql --socket=/tmp/mysql.sock -u$MYSQL_USER -p$MYSQL_PWD --host $MYSQL_HOST -Demlplatform -e "$insert_into_table" done < isam_subrack2.txt 2> /dev/null test.sh: line 98: syntax error near unexpected token `done' 'est.sh: line 98: `done < isam_subrack2.txt 2> /dev/null
I tried
dos2unix
command but it did not help.
Do you have any idea about why I am getting this error in this while loop?-
Stéphane Chazelas almost 9 yearsChances are the problem is in the
[...]
part. The Bourne and Korn shell found on Solaris allow unmatched quotes especially backticks for instance (while other shells are stricter on that). Try with only -x to see where it first stops to make sense. Above we don't see the PS4 (xtrace) output. -
Murat almost 9 yearsAgain the problem seems to be at 98th line: bash-4.1$ sh -x ./cibi_linux.sh ./cibi_linux.sh: line 98: syntax error near unexpected token
done' '/cibi_linux.sh: line 98:
done < isam_subrack2.txt 2> /dev/null -
muru almost 9 yearsWhat does
file test.sh
say? It looks like evn afterdos2unix
you haveCR
line endings. -
Murat almost 9 yearsSorry, I typed the cloned script's name. When running the test.sh:
sh -x ./test.sh ./test.sh: line 98: syntax error near unexpected token
done' '/est.sh: line 98:done < isam_subrack2.txt 2> /dev/null
-
minorcaseDev almost 9 years@Murat: check your file:
cat --show-nonprinting file
-
123 almost 9 yearsJust copy your code an paste it here shellcheck.net.
-
FelixJN almost 9 years@Murat Don't get confused with the output error pointing to line 98. It just means where the shell becomes aware of the problem, not necessarily where it is.
-
kos almost 9 yearsIt might well be that you have a missing / excessive single / double quote somewhere before the snippet. Otherwise one of
$MYSQL_HOME
$MYSQL_USER
,$MYSQL_PWD
and$MYSQL_HOST
might contain one. A good start would be to enclose all of those in souble quotes and then as suggested by User112638726 to paste the whole script to shellcheck.net to check where the actual issue is. -
roaima almost 9 yearsI do hope you can guarantee the format of your source data file. Preferably before Little Bobby Tables comes round to play.
-
-
Murat almost 9 yearsThanks @roaima. This command made the script run correctly.
-
musibs over 7 yearsThis is symptomatic of unexpected CR characters in the text file. +1