Vignesh
Vignesh

Reputation: 952

Unexpected token 'done' error in for loop shell scripting

I get this unexpected 'done' token error

echo -e "Enter the file stem name"
read filestem
for i in {1..22}
do
    `java -cp /export/home/alun/jpsgcs/ CMorgansToTheta $filestem_$i.INPUT.par $filestem_$i.THETA.par`
done

Upvotes: 0

Views: 262

Answers (2)

chepner
chepner

Reputation: 531185

If the Java program writes nothing to output, your for loop is equivalent (because of the backquotes) to

for i in {1..22}
do
done

which produces the error you see. It's likely you simply want to remove the backquotes to run the program 22 time:

echo -e "Enter the file stem name"
read filestem
for i in {1..22}
do
    java -cp /export/home/alun/jpsgcs/ CMorgansToTheta "${filestem}_$i.INPUT.par" "${filestem}_$i.THETA.par"
done

Upvotes: 5

anubhava
anubhava

Reputation: 785186

In your Java command line:

java -cp /export/home/alun/jpsgcs/ CMorgansToTheta $filestem_$i.INPUT.par $filestem_$i.THETA.par

You are using:

$filestem_$i

Which will be equivalent to:

${filestem_}${i}

because underscore _ is not considered a word boundary in shell and whole filestem_ will be considered variable name. Most likely you should be using:

${filestem}_${i}

Can you show me output of this script?

#!/bin/bash
set -x
echo -e "Enter the file stem name"
read filestem
for i in {1..3}
do
    echo "java -cp /export/home/alun/jpsgcs/ CMorgansToTheta ${filestem}_${i}.INPUT.par ${filestem}_${i}.THETA.par"
done

Upvotes: 1

Related Questions