开发者

Ant script passing arguments to Batch file

开发者 https://www.devze.com 2023-01-28 21:22 出处:网络
I have an Ant script where I need to call a batch script as follows: <exec dir=\"${basedir}\\work_internal\\${platform}\" executable=\"cmd.exe\">

I have an Ant script where I need to call a batch script as follows:

<exec dir="${basedir}\work_internal\${platform}" executable="cmd.exe">
          <arg line ="/c example.bat 'C:\work_internal\${platform}' 'revn=120 SPECIAL_OBJS='a b''" />

I need to pass the arguments to example.bat, first argument is a directory and second argument is 'revn=120 SPECIAL_OBJS='a b'', with SPECIAL_OBJS='a b' where 'a b' must be in q开发者_运维问答uotes. But when it calls to Bat script, it discards the quotes around 'a b' so in the second argument it is interpreted as revn=120 SPECIAL_OBJS= a b.

How can make it read like revn=120 SPECIAL_OBJS="a b"?


The single quotes don't pair up how you want them to, but you aught to be able to embed single quotes using the &quot; entity - something like:

<arg line=" ... &quot;revn=120 SPECIAL_OBJS='a b'&quot;" />

For me ant -verbose for the above gives the below:

 [exec] Executing 'cmd.exe' with arguments:
 [exec] '/c'
 [exec] 'example.bat'
 [exec] 'C:\work_internal\${platform}'
 [exec] 'revn=120 SPECIAL_OBJS='a z''

In your posted xml the quote pairs (v--v) are here:

<arg line="/c example.bat
     v----------------------------v v----------------------v   vv
     'C:\work_internal\${platform}' 'revn=120 SPECIAL_OBJS='a b''" />

which doesn't look like what you intend, and the line is broken up incorrectly.

Another way to pass arguments to the batch script is using separate arg value= elements:

<exec dir="." executable="cmd.exe">
    <arg value="/c" />
    <arg value="example.bat" />
    <arg value="C:\work_internal\${platform}" />
    <arg value="revn=120 SPECIAL_OBJS='a b'" />
</exec>

rather than passing everything as a single line to the shell. That sidesteps the (shell) tokenization logic that is breaking the line up differently to how you wish.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号