如何使用 ant <exec> 在 linux 上执行命令?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20883212/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 01:44:40  来源:igfitidea点击:

How can I use ant <exec> to execute commands on linux?

javalinuxant

提问by user3153661

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

我想使用 ant 来执行如下命令:

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

But it replies an error say

但它回复一个错误说

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

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

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.

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

Anyone could help or give me some clues?

任何人都可以帮助或给我一些线索?

Thanks in advance

提前致谢

回答by Ian Roberts

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

Ant 的<exec>任务使用 Java 的Process机制来运行命令,它不理解特定于 shell 的语法,如管道和重定向。如果你需要使用管道,那么你必须通过说类似的话来明确地运行一个 shell

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

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

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

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

Since sudo -Srequires 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).

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

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 Supportor if the value is read from an external .propertiesfile that you don't control) then you must split the arguments up yourself using separate argelements with valueor fileattributes, e.g.:

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

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