1

I am trying to run a java program (weka) from a bash script. The script takes as arguments an inputfile, an outputfile and the content of file containing the command to run the java program (environment variable $CMD). The script does not work as I wish and informs me that I use an unknown option for java. I tried to echo the command that the program sends to the shell, and the output is exactly the right command. So I assume that the echo output and the command sent to the shell are not the same.

So please tell me: What did I do wrong?

What is the difference between the output I get...

echo "java $(cat $CMD) $in > $out"

...and the command the computer gets?

java "$(cat $CMD)" $in > $out

If more information is needed, please comment!

Edit:

For those familiar with weka (or familiar with java), this is what I want to get, and what is printed to me by echo:

java -cp /usr/share/java/weka.jar weka.filters.supervised.attribute.AttributeSelection -E "weka.attributeSelection.ClassifierSubsetEval -B weka.classifiers.bayes.NaiveBayes -T" -S "weka.attributeSelection.BestFirst -D 1 -N 14" -i /home/aldorado/weka_examples/iris.arff > /home/aldorado/weka_examples/irisselected131113.txt
2
  • 1
    what is printed on echo? Commented Nov 18, 2013 at 11:14
  • so echo should print "java" following the content of the "command.txt" file which contains the information about the programm (weka) plus options for the programm. Then there is $in which contains the path of the input file for weka, and $out contains the path of the output file. Commented Nov 18, 2013 at 11:22

1 Answer 1

3

Add set -x in before the line which causes trouble.

That will make the computer print the command again as it understood it. You will see something like

 + 'java' '-classpath weka.jar name.of.the.main.Class' 'inputFile' > 'outputFile'

Note that quotes which the shell uses to tell you "this was one word / argument for me". It's very useful to notice problems with white space and quoting.

Note that it is very hard to get something like java "$(cat $CMD)" $in > $out working. I suggest to move the java into $CMD. That will allow you to say:

bash "./$CMD" $in > $out

or, if you make the file executable:

"./$CMD" "$in" > $out

Use "$1" in the file $CMD to get a property quoted reference to "$in":

cp="weka.jar"
cp="$cp;other.jar"
cp="$cp;more.jar"
cp="$cp;foo.jar"

java -classpath "$cp" name.of.the.main.Class "$1"
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! The problem is that I just started using bash. It is quite likely that I implement things in a clumsy way. I started with a script that my supervisor gave me and modified it for my needs - But as you see it did not work. I will include the java into the command.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.