1

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?

  • 1
    Chances 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. – Stéphane Chazelas Aug 24 '15 at 7:06
  • 2
    What does file test.sh say? It looks like evn after dos2unix you have CR line endings. – muru Aug 24 '15 at 7:22
  • 1
    @Murat, just like Stephane mentioned, the problem seems to be in the [...]. Hope the file for example with vim and enable the syntax check. You should be able to see if something is not terminated. I have my share of cases where the script complain at line 150 and the problem as on a misquote in a completed different line. – BitsOfNix Aug 24 '15 at 7:25
  • 2
    @Murat: check your file: cat --show-nonprinting file – Cyrus Aug 24 '15 at 7:44
  • 4
    @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. – Fiximan Aug 24 '15 at 8:15
2

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
  • Thanks @roaima. This command made the script run correctly. – Murat Aug 24 '15 at 12:10
  • This is symptomatic of unexpected CR characters in the text file. +1 – Somnath Musib Jan 22 '17 at 6:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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