>> we want to try execute a bash command from java but, I need to insert
>> in command a double quotes example:
[quoted text clipped - 7 lines]
> should do it.
> -Wayne
Echo is also a separate command, but as such it needs no special
argument escaping:
Process p = rt.exec(new String[]{"echo", "hello, world!"} );
If you use any newer than stone aged java versions (1.5 and up), you
can also make it shorter:
Process p = rt.exec("echo", "hello, world!");
Wayne - 21 Mar 2008 22:54 GMT
>>> we want to try execute a bash command from java but, I need to insert
>>> in command a double quotes example:
[quoted text clipped - 15 lines]
> can also make it shorter:
> Process p = rt.exec("echo", "hello, world!");
If you don't need to process backslash escapes, quotes, and
so on, using echo is easier. However I don't think that
your "modern stone aged" version will work, did you try it?
javac 1.6.0_05 complained when I tried, and the 1.6 API (javadocs)
don't list that as a legal version of Runtime.exec() either.
Also, just using echo as the command wouldn't permit parsing of
the argument string as the OP apparently wanted. You're right,
you don't need to parse "hello, world!" but you would need bash
or some shell to parse a more complex string including shell
variables, wildcards, etc. Interestingly the bash built-in
echo command has different semantics than /bin/echo command.
As a matter of portability printf should be used instead
anyway:
String arg = "Hello, \"World\"!";
Process p = rt.exec( new String[]{"bash", "-c",
"printf \"%s\\n\"" + arg} );
However that is uglier.
-Wayne
Wayne - 21 Mar 2008 23:00 GMT
>>> we want to try execute a bash command from java but, I need to insert
>>> in command a double quotes example:
[quoted text clipped - 15 lines]
> can also make it shorter:
> Process p = rt.exec("echo", "hello, world!");
If you don't need to process backslash escapes, quotes, and
so on, using echo is easier. However I don't think that
your "modern stone aged" version will work, did you try it?
javac 1.6.0_05 complained when I tried, and the 1.6 API (javadocs)
don't list that as a legal version of Runtime.exec() either.
Also, just using echo as the command wouldn't permit parsing of
the argument string as the OP apparently wanted. You're right,
you don't need to parse "hello, world!" but you would need bash
or some shell to parse a more complex string including shell
variables, wildcards, etc. Interestingly the bash built-in
echo command has different semantics than /bin/echo command.
As a matter of portability printf should be used instead
anyway:
String arg = "Hello, \"World\"!";
Process p = rt.exec( new String[]{"bash", "-c",
"printf \"%s\\n\" " + arg} );
However that is uglier.
-Wayne
Wayne - 21 Mar 2008 23:08 GMT
>>> we want to try execute a bash command from java but, I need to insert
>>> in command a double quotes example:
[quoted text clipped - 15 lines]
> can also make it shorter:
> Process p = rt.exec("echo", "hello, world!");
If you don't need to process backslash escapes, quotes, and
so on, using echo is easier. However I don't think that
your "modern stone aged" version will work, did you try it?
javac 1.6.0_05 complained when I tried, and the 1.6 API (javadocs)
don't list that as a legal version of Runtime.exec() either.
Also, just using echo as the command wouldn't permit parsing of
the argument string as the OP apparently wanted. You're right,
you don't need to parse "hello, world!" but you would need bash
or some shell to parse a more complex string including shell
variables, wildcards, etc. Interestingly the bash built-in
echo command has different semantics than /bin/echo command.
As a matter of portability printf should be used instead
anyway:
Process p = rt.exec( new String[]{"bash", "-c",
"printf \"%s\\n\" '" + arg + "'"} );
However that is uglier (took me three tries to get the quoting right).
-Wayne
Andreas Leitgeb - 22 Mar 2008 08:48 GMT
>> Echo is also a separate command, but as such it needs no special
>> argument escaping:
[quoted text clipped - 3 lines]
>> can also make it shorter:
>> Process p = rt.exec("echo", "hello, world!");
> However I don't think that
> your "modern stone aged" version will work, did you try it?
> javac 1.6.0_05 complained when I tried, and the 1.6 API (javadocs)
> don't list that as a legal version of Runtime.exec() either.
Damn, you're right here. Typical case of: posted too fast.
I (blindly) assumed that they did it like PrintStream's
printf with "..."-ified argument, but obviously they
omitted that. And there isn't even any other two(or more)-
String version of exec, that could have caused ambiguities.
Sorry for confusion.
> Process p = rt.exec( new String[]{"bash", "-c",
> "printf \"%s\\n\" '" + arg + "'"} );
> However that is uglier (took me three tries to get the quoting right).
It's still far away from right. Just think about arg values like:
"hello'`rm -rf /`'world"
(don't try that at home, folks :-)
The single quotes don't make it "secure" - unless you preprocess
the string and replace every "'" by this: "'\\''" and even then you
need to worry about how many backslashes you really need.