commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Kevin Brown (JIRA)" <j...@apache.org>
Subject [jira] [Comment Edited] (EXEC-83) Arguments with spaces lead to quotes in arguments in bash
Date Thu, 20 Feb 2014 19:45:27 GMT

    [ https://issues.apache.org/jira/browse/EXEC-83?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13907388#comment-13907388
] 

Kevin Brown edited comment on EXEC-83 at 2/20/14 7:45 PM:
----------------------------------------------------------

[~b.eckenfels] [~sebb@apache.org]

Thanks for your replies.    For now, I'm including some java code that recreates my original
issue.   I'll modify this code to use the ProcessBuilder/bash example above shortly.

Java:

{code:title=Foo.java|borderStyle=solid}
@Test
    public void testExecutor() throws IOException {

        String script="/Users/kevin/foo.sh";
        String arg1="100";
        String arg2="kevin was here";
        String arg3="three";

        CommandLine commandLine = new CommandLine(script);
        commandLine.addArgument(arg1, false);
        commandLine.addArgument(arg2, true);
        commandLine.addArgument(arg3, false);

        DefaultExecutor executor = new DefaultExecutor();
        executor.setExitValues(null); //check for exit values ourselves

        System.out.println("cmdLine="+ commandLine.toString());
        int exitValue = executor.execute(commandLine);
        System.out.println("exitValue="+ exitValue);
    }
{code}

#foo.sh being called from Java
mac:~ kevin$ cat ./foo.sh 
#!/bin/bash
echo "The number of arguments is: $#" >> ~/log.txt
a=${@}
count=0
for var in "$@"
do
    echo "The length of argument '$var' is: ${#var}" >> ~/log.txt
    (( count++ ))
done


#RUN JUnit test from IDE HERE -- look at results

mac:~ kevin$ cat ~/log.txt
The number of arguments is: 3
The length of argument '100' is: 3
The length of argument '"kevin was here"' is: 16
The length of argument 'three' is: 5

#Compare with running from command line
mac:~ kevin$ rm ~/log.txt
mac:~ kevin$ /Users/kevin/foo.sh 100 "kevin was here" three
mac:~ kevin$ cat ~/log.txt
The number of arguments is: 3
The length of argument '100' is: 3
The length of argument 'kevin was here' is: 14
The length of argument 'three' is: 5

Notice that the 2nd argument is quoted in the first run and not in the second








was (Author: kbrowncnn):
[~b.eckenfels] [~sebb@apache.org]

Thanks for your replies.    For now, I'm including some java code that recreates my original
issue.   I'll modify this code to use the ProcessBuilder/bash example above shortly.

Java:

@Test
    public void testExecutor() throws IOException {

        String script="/Users/kevin/foo.sh";
        String arg1="100";
        String arg2="kevin was here";
        String arg3="three";

        CommandLine commandLine = new CommandLine(script);
        commandLine.addArgument(arg1, false);
        commandLine.addArgument(arg2, true);
        commandLine.addArgument(arg3, false);

        DefaultExecutor executor = new DefaultExecutor();
        executor.setExitValues(null); //check for exit values ourselves

        System.out.println("cmdLine="+ commandLine.toString());
        int exitValue = executor.execute(commandLine);
        System.out.println("exitValue="+ exitValue);
    }

#foo.sh being called from Java
mac:~ kevin$ cat ./foo.sh 
#!/bin/bash
echo "The number of arguments is: $#" >> ~/log.txt
a=${@}
count=0
for var in "$@"
do
    echo "The length of argument '$var' is: ${#var}" >> ~/log.txt
    (( count++ ))
done


#RUN JUnit test from IDE HERE -- look at results

mac:~ kevin$ cat ~/log.txt
The number of arguments is: 3
The length of argument '100' is: 3
The length of argument '"kevin was here"' is: 16
The length of argument 'three' is: 5

#Compare with running from command line
mac:~ kevin$ rm ~/log.txt
mac:~ kevin$ /Users/kevin/foo.sh 100 "kevin was here" three
mac:~ kevin$ cat ~/log.txt
The number of arguments is: 3
The length of argument '100' is: 3
The length of argument 'kevin was here' is: 14
The length of argument 'three' is: 5

Notice that the 2nd argument is quoted in the first run and not in the second







> 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)

Mime
View raw message