[
https://issues.apache.org/jira/browse/EXEC-83?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13906213#comment-13906213
]
Bernd Eckenfels commented on EXEC-83:
-------------------------------------
@sebb: hmm... there is no need to quote arguments with spaces as long as you
use String[] variant of exec and not use -c. The question of course is if and
why the shell should use -c (IMHO only needed if you want to use shell
expansion or internal commands)
{pre}
$ cat pa.sh
#!/bin/sh
echo Arg1=$1 Arg2=$2 Arg3=$3 count=$# args=["$*"]
$ java -cp . test.Fork
ProcessBuilder("/bin/sh", "./pa.sh", "a", "b b", "c") // best
Arg1=a Arg2=b b Arg3=c count=3 Args=[a b b c]
ProcessBuilder("/bin/sh", "-c", "./pa.sh a \"b b\" c") // good
Arg1=a Arg2=b b Arg3=c count=3 Args=[a b b c]
ProcessBuilder("/bin/sh", "-c", "./pa.sh", "a", "b b", "c") // wrong
Arg1= Arg2= Arg3= count=0 Args=[]
$ cat test/Fork.java
package test;
import java.io.IOException;
public class Fork {
public static void main(String[] args) throws IOException,
InterruptedException {
System.out.println("ProcessBuilder(\"/bin/sh\", \"./pa.sh\", \"a\", \"b
b\", \"c\") // best");
ProcessBuilder pb = new ProcessBuilder("/bin/sh", "./pa.sh", "a", "b
b", "c");
pb.inheritIO(); pb.start().waitFor();
System.out.println("ProcessBuilder(\"/bin/sh\", \"-c\", \"./pa.sh a
\\\"b b\\\" c\") // good");
pb = new ProcessBuilder("/bin/sh", "-c", "./pa.sh a \"b b\" c");
pb.inheritIO(); pb.start().waitFor();
System.out.println("ProcessBuilder(\"/bin/sh\", \"-c\", \"./pa.sh\",
\"a\", \"b b\", \"c\") //wrong");
pb = new ProcessBuilder("/bin/sh", "-c", "./pa.sh", "a", "b b", "c");
pb.inheritIO(); pb.start().waitFor();
} }
{pre}
> Arguments with spaces lead to quotes in arguments in bash
> ---------------------------------------------------------
>
> Key: EXEC-83
> URL: https://issues.apache.org/jira/browse/EXEC-83
> Project: Commons Exec
> Issue Type: Bug
> Affects Versions: 1.2
> Environment: Java 7 & OSX
> Reporter: Kevin Brown
> Priority: Minor
>
> If I run my script directly from a bash prompt I see a different behavior
> than if I launch with commons-exec. This may be the nature of the beast
> (java Runtime-exec interacting with bash), but leads me to have to rewrite
> scripts to remove extraneous quotes.
> Here is my script:
> $ cat ./foo.sh
> #!/bin/bash
> echo "$@"
> case I: commons-exec with no spaces ==> arguments do NOT have quotes when in
> bash script
> cmdLine=[/opt/bmam/bin/avidcommand/write_xmp_remote.sh, 70059021,
> RAW-one_two_three, http://foo.com]
> output (of $@):
> 70059021 RAW-one_two_three http://foo.com
> case II: commons-exec with spaces ==> arguments have quotes when in bash
> script
> cmdLine=[/opt/bin/foo.sh, 70058269, "AIR-one two three", http://foo.com]
> output (of $@):
> 70058269 "AIR-one two three" http://foo.com
> case III: execute directly from bash ==> arguments do NOT have quotes when in
> bash script
> $./foo.sh 70058891 "one two three" http://foo.com
> output (of $@):
> 70058891 one two three http://foo.com
--
This message was sent by Atlassian JIRA
(v6.1.5#6160)