【问题标题】:Execute Unix cat command in Groovy?在 Groovy 中执行 Unix cat 命令?
【发布时间】:2011-05-15 13:34:35
【问题描述】:

你好

我想从 Groovy 程序执行 cat /path/to/file1 /path/to/file2 > /path/to/file3 之类的东西。 我试过 "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(),但这也没用。

你有什么建议吗?

【问题讨论】:

    标签: unix groovy


    【解决方案1】:

    我相信您需要使用List.execute() 方法来让它在Shell 中运行,即:

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

    或者,你可以用时髦的方式来做

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

    【讨论】:

    • 太棒了,谢谢!你能解释一下为什么我们必须使用 List.execute() 方法而不是标准的 String.execute() 吗?
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2013-06-05
      • 2017-03-14
      • 2012-07-16
      • 2013-07-27
      • 2012-12-09
      • 2010-09-14
      • 1970-01-01
      • 1970-01-01
      • 2014-10-11
      相关资源
      最近更新 更多