EXEC args (value) with quotes on linux from Ant script

ant
17,474

Can you tell us what mimic_cmd is? (Is it an ELF executable, is it a script -- and if so, what is its contents?)

You don't need nor want the double-quotes inside your ANT XML attributes (incidentally, for it to be well-formed XML you should have written them as " not ", but that changes nothing with respect to this discussion) unless your executable expects them. The corresponding ANT code for either of the following (100% equivalent) shell command lines:

   ./mimic_cmd "startDaemon()"
   ./mimic_cmd 'startDaemon()'
   ./mimic_cmd startDaemon\(\)
   ./mimic_cmd startDaemon"()"
   ./mimic_cmd startDaemon'()'

...actually is:

   <exec failonerror="true" executable="/bin/mimic_cmd"> 
      <arg value="startDaemon()" /> 
   </exec> 

...or, for illustrative purposes:

   <!-- spawn a shell with your original command line -->
   <exec failonerror="true" executable="/bin/sh"> 
      <arg value="-c" /> 
      <arg value="/bin/mimic_cmd &quot;startDaemon()&quot;" /> 
   </exec> 

Why that is so is longwinded to explain; suffices to say that, in your specific case, the only time when you'd have to use double quotes would be when ultimately issuing the command via a *nix shell (either interactively or as part of another script or programatically via the execing of sh -c), and only in order for that shell not to think that the round parens () have special meaning. By the time the shell would in turn spawn mimic_cmd it would have already stripped the double quotes (and substituted backslash-escaped sequences etc. -- see how a *nix shell parses its command line) ANT does not run your command via the shell but rather executes it directly, so in this case mimic_cmd finds itself with a bunch of double quotes on its hand which it apparently doesn't know how to handle.

You essentially have to think of it as replacing all forms of shell quoting and escaping with XML escaping and breaing down into <arg/> tags.

Windows' CMD.EXE is special in the sense that, unline *nix shells, it does minimal parsing (and generally does not care about double quotes in program arguments), leaving it up to the program to figure out what you meant by quoting. (This is actually a hard limitation of Windows' CreateProcess which does not have the notion of argv[], leaving it up to each program to intepret lpCommandLine in whichever way it sees fit; some will get rid of the quotes for you, but that behaviour is extremely inconsistent, e.g. issue echo "bla" on the CMD.EXE prompt to see what CMD.EXE's builtins think about quoting.) Again, in your case the round parens () have no meaning for CMD.EXE so you don't need them even when typing the command at a command prompt. As for ANT, on Windows as on *nix platforms, it spwans mimic_cmd via CreateProcess not CMD.EXE so you don't really want to quote anything.

Share:
17,474
Admin
Author by

Admin

Updated on June 23, 2022

Comments

  • Admin
    Admin almost 2 years

    bash shell:

    ./mimic_cmd "startDaemon()"
    

    Corresponding Ant code:

     <exec failonerror="true" executable="/bin/mimic_cmd">
        <arg value='"startDaemon()"' />
     </exec>
    
    1. Does the Ant code exactly represent the above command at the bash shell? Based on the debug info, it looks like it:
     [exec] Executing '/bin/mimic_cmd' with arguments:
     [exec] '"startDaemon()"'
     [exec] 
     [exec] The ' characters around the executable and arguments are
     [exec] not part of the command.
     Execute:Java13CommandLauncher: Executing '/bin/mimic_cmd' with arguments:
     '"startDaemon()"'
     The ' characters around the executable and arguments are not part of the command.
    

    However, the Ant code returns and exit code of 1 while the Bash shell command returns 0.

    Toggling vmlauncher doesn't help, and paths are all correct.

    The same Ant code works on windows with the resulting debug output:

     [exec] Executing 'C:\bin\mimic_cmd' with arguments:
     [exec] '"startDaemon()"'
     [exec] 
     [exec] The ' characters around the executable and arguments are
     [exec] not part of the command.
     Execute:Java13CommandLauncher: Executing 'C:\bin\mimic_cmd' with arguments:
     '"startDaemon()"'
     The ' characters around the executable and arguments are not part of the command.
    
  • Admin
    Admin about 14 years
    mimic_cmd is a shell executable for a cross platform installation solution. I don't work on the in-house team that develops it, so honestly I can't say whether the quotes are always needed, but some commands appear to need them or must have a space before the parenthesis? 'mimic_cmd StartDaemon()' does work in ANT and on the command line (no quotes, no space) 'mimic_cmd createNewWorkspace(blah)' without quotes does not work at all; '"mimic_cmd createNewWorkspace(blah)"' works on Windows, but not linux, and 'mimic_cmd createNewWorkSpace (blah)' works on both... (space before parenthesis)
  • vladr
    vladr about 14 years
    Hmm... careful with those round parens without quotes on *nix, they are probably doing something behind your back you don't expect... does typing /bin/mimic_cmd "createNewWorkspace(blah)" work on a *nix command line? If so, <exec failonerror="true" executable="/bin/mimic_cmd"><arg value="createNewWorkspace(blah)" /></exec> should work just as well in ANT. If blah is itself a string it'd get more interesting: /bin/mimic_cmd "createNewWorkspace(\"blah\")" or /bin/mimic_cmd 'createNewWorkspace("blah")' in a shell or, in ANT, ...<arg value="createNewWorkspace(&quot;blah&quot;)" />...