EXEC args(value)带有来自Ant脚本的linux上的引号

5 ant

bash shell:

./mimic_cmd "startDaemon()"
Run Code Online (Sandbox Code Playgroud)

对应的Ant代码:

 <exec failonerror="true" executable="/bin/mimic_cmd">
    <arg value='"startDaemon()"' />
 </exec>
Run Code Online (Sandbox Code Playgroud)
  1. Ant代码是否完全代表bash shell中的上述命令?根据调试信息,它看起来像:
 [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.
Run Code Online (Sandbox Code Playgroud)

但是,当Bash shell命令返回0时,Ant代码返回并退出代码1.

切换vmlauncher没有帮助,路径都是正确的.

相同的Ant代码在Windows上使用生成的调试输出:

 [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.
Run Code Online (Sandbox Code Playgroud)

vla*_*adr 7

你能告诉我们什么mimic_cmd是?(它是一个ELF可执行文件,它是一个脚本 - 如果是,它的内容是什么?)

不需要也不想要你的ANT XML属性中的双引号(顺便说一句,因为它是格式良好的XML,你应该把它们写成&quot;没有",但是这个讨论没有任何改变),除非你的可执行文件需要它们.以下任一(100%等效)shell命令行的相应ANT代码:

   ./mimic_cmd "startDaemon()"
   ./mimic_cmd 'startDaemon()'
   ./mimic_cmd startDaemon\(\)
   ./mimic_cmd startDaemon"()"
   ./mimic_cmd startDaemon'()'
Run Code Online (Sandbox Code Playgroud)

......实际上是:

   <exec failonerror="true" executable="/bin/mimic_cmd"> 
      <arg value="startDaemon()" /> 
   </exec> 
Run Code Online (Sandbox Code Playgroud)

......或者,为说明目的:

   <!-- 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> 
Run Code Online (Sandbox Code Playgroud)

为什么会如此呢?足以说明,在您的特定情况下,您必须使用双引号的唯一时间是最终通过*nix shell发布命令(交互式或作为另一个脚本的一部分或通过编程以exec编程方式sh -c) ,只是为了让那个贝壳不要认为圆形的parens ()有特殊意义.当shell反过来产生mimic_cmd它时,它已经剥离了双引号(并替换了反斜杠转义序列等 - 看看*nix shell如何解析它的命令行)ANT不通过shell运行你的命令而是直接执行它,所以在这种情况下mimic_cmd发现自己手上有一堆双引号,它显然不知道如何处理.

您基本上必须将其视为替换所有形式的shell引用并通过XML转义和转换为<arg/>标记来转义.

Windows' CMD.EXE特殊的,在unline*nix shell中,它执行最小的解析(通常不关心程序参数中的双引号),让程序通过引用来弄清楚你的意思.(这实际上是Windows的一个硬性限制,CreateProcess没有这个概念argv[],让它lpCommandLine按照它认为合适的方式对每个程序进行解释;有些会为你删除引号,但这种行为极不一致,例如echo "bla",在CMD.EXE提示上看看内部组件CMD.EXE对引用的看法.然而,在你的情况下,圆形的parens ()没有任何意义,CMD.EXE因此即使在命令提示符下键入命令也不需要它们.至于ANT,在Windows上*nix平台,它spwans mimic_cmd通过CreateProcessCMD.EXE那么你真的不希望引用任何东西.