Bash script with multiple string arguments

Go To


I'm having problem separating two string variables as arguments for a bash script. My bash script takes two arguments like so:



However, DATA1 and DATA2 are not what I intended, because bash is reading my strings wrongly. DATA2 is only the first part of the argument I wanted to put in, because I'm having trouble making a complicated(relatively) string into a single arguemnt.

I call this script from a Java app with variables like this:

String m1 = "some data";
String m2 = "some more"+mystring+"even more data"+anotherstring;  m1 m2

Only the first part of m2 is passed to DATA1 in the bash script. I've tried wrapping the whole thing in single quotes and double quotes I can't get it to accept the whole of m2 as the second argument.

Any help in creating two string arguments is appreciated.

2012-04-04 03:40
by NoName
I think the problem is more likely to be on the Java end than on the Bash end. How are you launching the script - ruakh 2012-04-04 03:43
You are going to have to show the EXACT code you use to invoke the script from Java (Runtime.exec()?, ProcessBuilder?), including the code that sets up the arguments. Otherwise we're just guessing - Jim Garrison 2012-04-04 03:55
first part of m2 is passed to DATA1? do you mean first part of m2 is passed to DATA2 - Alvin 2012-04-04 07:06


It works for me, see:

Runtime.getRuntime().exec(new String[]{"/bin/sh", "/tmp/", m1,m2});

Your code calls:Runtime.getRuntime().exec(String)

It calls method exec(command,null,null) (see source code in exec(String command, String[] envp, File dir) .

Method exec(String command, String[] envp, File dir) parse command by StringTokenizer. So text "some data" or "\"some data\"" is split to some data (or "some data ")

2012-04-04 06:55
by Andrzej Jozwik