Moo*_*lit 5 shell groovy command file
我想从我的groovy脚本执行shell命令.我测试了以下内容:
"mkdir testdir".execute()
Run Code Online (Sandbox Code Playgroud)
这只是工作正常.现在我想制作一个文件,写一些文件,然后打开一个文本编辑器来查看文件.
def execute(cmd) {
def proc = cmd.execute()
proc.waitFor()
}
execute("touch file")
execute("echo hello > file")
execute("gedit file")
Run Code Online (Sandbox Code Playgroud)
现在gedit正确打开但文件中没有"hello"字符串.这是怎么回事?!?
你不能在行中进行重定向:
execute("echo hello > file")
Run Code Online (Sandbox Code Playgroud)
因此没有任何内容写入文件.处理此问题的最简单方法可能是将所有命令包装到单个shell脚本中,然后执行此脚本.
否则,您可以从echo
命令中读取标准输出(不带> file
),然后file
在Groovy中将其写入自己.
或者你可以这样做:
execute( [ 'bash', '-c', 'echo hello > file' ] )
Run Code Online (Sandbox Code Playgroud)
哪个应该工作,因为您的execute
方法将只执行该List.execute()
方法
归档时间: |
|
查看次数: |
5593 次 |
最近记录: |