2011-05-15 121 views
3

在Groovy中执行Unix cat命令?

我想执行类似猫/路径/到/文件1 /路径/到/文件2> /路径/从一个Groovy程序/文件3。我尝试了“cat/path/to/file1/path/to/file2>/path/to/file3”.execute(),但是这样做不起作用。

经过一番搜索,我读了关于sh -c。所以我尝试了 “sh -c cat/path/to/file1/path/to/file2>/path/to/file3”.execute(),但那也不行。

你有什么建议吗?

回答

4

我相信你需要使用List.execute()的方法得到shell中运行,即:

[ 'sh', '-c', 'cat file1 file2 > file3' ].execute() 

或者,你可以做到这一点的常规方式

new File('file3').with { f -> 
    [ 'file1', 'file2' ].each { f << new File(it).asWritable() } 
} 
+0

工作正常。谢谢! – Nomr 2011-05-28 11:44:49

+1

辉煌,谢谢!你能否解释为什么我们必须使用List.execute()方法而不是标准的String.execute()? – th3morg 2015-01-30 14:34:34

1

也许你需要提供可执行文件的完整路径? "/bin/sh -c '/bin/cat file1 file2 >file3'".execute()

+0

我试了一下/ bin/sh但它也不起作用。 – Nomr 2011-05-15 14:17:03