passing quoted string argument to java in script in msys
Question
I have been trying to run some java programs from a bash script in msys on a windows machine. Specifically, I am automating some tasks using WEKA on command line. I have some problems where a quoted string is not passed correctly to java. The actual code loops through an array of commands to run. Here is a stripped down version, i.e. a single iteration where $RUN
contains the array element string.
#!/bin/bash
export CLASSPATH=CLASSPATH:weka.jar:libsvm.jar
RUN="java weka.classifiers.functions.LibSVM -S 0 -K 2 -D 3 -G 0.0 -R 0.0 -N 0.5 -M 1000.0 -C 1000000.0 -E 0.0010 -P 0.1 -Z -W '1 2' -t trainFile.csv -T testFile.csv "
# does not work, returns => Weka exception: For input string: "'1"
$RUN > reportFile.txt
RUN="java weka.classifiers.functions.LibSVM -S 0 -K 2 -D 3 -G 0.0 -R 0.0 -N 0.5 -M 1000.0 -C 1000000.0 -E 0.0010 -P 0.1 -Z -W \"1 2\" -t trainFile.csv -T testFile.csv "
# does not work, returns => Weka exception: For input string: ""1"
$RUN > reportFile.txt
RUN="java weka.classifiers.functions.LibSVM -S 0 -K 2 -D 3 -G 0.0 -R 0.0 -N 0.5 -M 1000.0 -C 1000000.0 -E 0.0010 -P 0.1 -Z -t trainFile.csv -T testFile.csv "
# works without the string argument for -W
$RUN > reportFile.txt
It seems the the problem is the string "1 2"
is broken into two arguments. How can they be combined into one argument?
I am using Windows 7, msys
with the mintty
terminal.
Solution
Try this workaround:
RUN="weka.classifiers.functions.LibSVM -S 0 -K 2 -D 3 -G 0.0 -R 0.0 -N 0.5 -M 1000.0 -C 1000000.0 -E 0.0010 -P 0.1 -Z -W \"1 2\" -t trainFile.csv -T testFile.csv"
echo $RUN | xargs java > reportFile.txt
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow