Groovy:如何在Groovy中执行复杂的shell命令?
我希望能够执行嵌套的shell命令。比如,Groovy:如何在Groovy中执行复杂的shell命令?,shell,groovy,Shell,Groovy,我希望能够执行嵌套的shell命令。比如, final String cmd = 'for i in pom.xml projects.xml; do find . -name $i | while read fname; do echo $fname; done;done' 我尝试了以下语法,但无法运行 def result=cmd.execute() def result=['sh','-c',cmd].execute() def result=('sh-c代表pom.xml projec
final String cmd = 'for i in pom.xml projects.xml; do find . -name $i | while read fname; do echo $fname; done;done'
我尝试了以下语法,但无法运行
def result=cmd.execute()代码>
def result=['sh','-c',cmd].execute()代码>
def result=('sh-c代表pom.xml projects.xml中的i;do find.-name$i |而read fname;do echo$fname;done;done')。执行()
def cmd = [
'bash',
'-c',
'''for i in pom.xml projects.xml
|do
| find . -name $i | while read fname
| do
| echo $fname
| done
|done'''.stripMargin() ]
println cmd.execute().text
(我已经格式化了命令文本,因此在这里看起来更好,您可以将其全部保留在一行中)
我还相信,你的指挥权可以由以下人员取代:
find . -name pom.xml -o -name projects.xml -print
或者,在Groovy中:
def files = []
new File( '.' ).traverse() {
if( it.name in [ 'pom.xml', 'projects.xml' ] ) {
files << it
}
}
println files
def文件=[]
新文件('.')。遍历(){
if(在['pom.xml','projects.xml']中的it.name){
文件感谢所有的帮助。这是一个很棒的社区。在传递了环境和工作目录信息后,我能够让它工作
def wdir = new File( "./", module ).getAbsoluteFile() ;
def env = System.getenv();
def envlist = [];
env.each() { k,v -> envlist.push( "$k=$v" ) }
final String cmd = 'for i in pom.xml projects.xml; do find . -name $i | while read fname; do echo $fname; done;done'
proc = ["bash", "-c", cmd].execute(envlist , wdir);
为什么不编写一个shell脚本并执行它,或者只编写一些groovy来执行相同的功能呢?如果我可以在groovy中执行它,那会容易得多。我可以在groovy中执行它,但上面只是一个示例,因为我可以使用更复杂的命令;在groovy中实现它需要更长的时间。如果o在Groovy中调用shell命令的简单方法然后我将不得不创建一个单独的shell脚本,但现在我必须跟踪另一个脚本文件?