2011-05-15 118 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