我如何使用 ant <exec>在linux上执行命令? [英] How can I use ant <exec> to execute commands on linux?

查看:39
本文介绍了我如何使用 ant <exec>在linux上执行命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用蚂蚁来执行如下命令:

I would like to use ant to exectue a command like below:

<exec executable="echo ptc@123 | sudo -S /app/Windchill_10.0/Apache/bin/apachectl -k stop">
</exec>

但它回复一个错误说

可执行文件和参数周围的 ' 字符不是命令的一部分.

The ' characters around the executable and arguments are not part of the command.

背景是:我想用ant来停止apache服务器,但它不是由我运行命令的同一个用户安装的.

The background is: I want to use ant to stop the apache server but it doesn't installed by the same user I run the command.

谁能帮我或给我一些线索?

Anyone could help or give me some clues?

提前致谢

推荐答案

Ant的任务使用Java的Process机制来运行命令,这个不懂特定于 shell 的语法,如管道和重定向.如果您需要使用管道,那么您必须通过说诸如

Ant's <exec> task uses Java's Process mechanism to run commands, and this does not understand shell-specific syntax like pipes and redirections. If you need to use pipes then you have to run a shell explicitly by saying something like

<exec executable="/bin/sh">
  <arg value="-c" />
  <arg value="echo ptc@123 | sudo -S /app/Windchill_10.0/Apache/bin/apachectl -k stop" />
</exec>

但在这种情况下没有必要,因为您可以只运行 sudo 命令并使用 inputstring 来提供其输入,而不是使用管道 echo:

but in this case it's not necessary, as you can run just the sudo command and use inputstring to provide its input rather than using a piped echo:

<exec executable="sudo" inputstring="ptc@123&#10;">
  <arg line="-S /app/Windchill_10.0/Apache/bin/apachectl -k stop" />
</exec>

由于 sudo -S 需要一个换行符来终止密码,我在 inputstring&#10;code>(这是在 XML 中的属性值中编码换行符的最简单方法).

Since sudo -S requires a newline character to terminate the password, I've added &#10; on the end of the inputstring (this is the simplest way to encode a newline character in an attribute value in XML).

请注意,<arg line="..."/> 在进行分词时非常简单——如果任何命令行参数可能包含空格(例如,如果您需要引用目录下的文件,例如 ${user.home}/Library/Application Support 或者如果该值是从外部 .properties 文件中读取的您无法控制)那么您必须使用带有 valuefile 属性的单独 arg 元素自己拆分参数,例如:

Note that <arg line="..." /> is pretty simple minded when it comes to word splitting - if any of the command line arguments could contain spaces (for example if you need to refer to a file under a directory such as ${user.home}/Library/Application Support or if the value is read from an external .properties file that you don't control) then you must split the arguments up yourself using separate arg elements with value or file attributes, e.g.:

<exec executable="sudo" inputstring="ptc@123&#10;">
  <arg value="-S" />
  <arg file="${windchill.install.path}/Apache/bin/apachectl" />
  <arg value="-k" />
  <arg value="stop" />
</exec>

这篇关于我如何使用 ant &lt;exec&gt;在linux上执行命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆