gpt4 book ai didi

groovy - 使用 groovy 脚本输出作为另一个 groovy 脚本的输入

转载 作者:行者123 更新时间:2023-12-01 05:30:30 28 4
gpt4 key购买 nike

我会提前道歉,我是 groovy 的新手。我的问题是我有 3 个执行不同功能的 groovy 脚本,我需要从我的主 groovy 脚本中调用它们,使用脚本 1 的输出作为脚本 2 的输入和脚本 2 的输出作为脚本 3 的输入。

我试过以下代码:

script = new GroovyShell(binding)
script.run(new File("script1.groovy"), "--p", "$var" ) | script.run(new File("script2.groovy"), "<", "$var" )

当我运行上面的代码时,第一个脚本成功运行,但第二个脚本根本没有运行。

脚本 1 使用 "--p", "$var" 将 int 作为参数。代码。这在主脚本中成功运行: script.run(new File("script1.groovy"), "--p", "$var" ) - 脚本 1 的输出是一个 xml 文件。

当我跑 script.run(new File("script2.groovy"), "<", "$var" )在主 groovy 脚本中,没有任何 react ,系统挂起。

我可以使用 groovy script2.groovy < input_file 从命令行运行脚本 2它工作正常。

任何帮助将不胜感激。

最佳答案

您不能通过 <作为脚本的参数,当您从命令行运行时,重定向由 Shell 处理...

将 Scripts 的输出重定向到其他脚本是出了名的困难,并且基本上依赖于您更改 System.out在每个脚本的持续时间内(并希望 JVM 中没有其他内容打印和弄乱您的数据)

最好使用如下的 java 进程:

鉴于这 3 个脚本:

脚本1.groovy

// For each argument
args.each {
// Wrap it in xml and write it out
println "<woo>$it</woo>"
}

linelength.groovy
// read input
System.in.eachLine { line ->
// Write out the number of chars in each line
println line.length()
}

漂亮.groovy
// For each line print out a nice report
int index = 1
System.in.eachLine { line ->
println "Line $index contains $line chars (including the <woo></woo> bit)"
index++
}

然后我们可以写这样的东西来让一个新的 groovy 进程依次运行,并将输出通过管道相互连接(使用重载 or operator on Process):
def s1 = 'groovy script1.groovy arg1 andarg2'.execute()
def s2 = 'groovy linelength.groovy'.execute()
def s3 = 'groovy pretty.groovy'.execute()

// pipe the output of process1 to process2, and the output
// of process2 to process3
s1 | s2 | s3

s3.waitForProcessOutput( System.out, System.err )

打印出来:
Line 1 contains 15 chars (including the <woo></woo> bit)
Line 2 contains 18 chars (including the <woo></woo> bit)

关于groovy - 使用 groovy 脚本输出作为另一个 groovy 脚本的输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11437763/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com