【问题标题】:Executing long command with groovy on linux shell?在 linux shell 上使用 groovy 执行长命令?
【发布时间】:2014-03-02 22:12:08
【问题描述】:

如何使用 groovy 执行长命令? 当我在我的 linux 终端上执行这个命令时,我得到了我想要的进程的正确进程 ID。使用 groovy 我得到一个空的回报。这是我已经尝试过的:

def p = "ps aux | grep 'unit 1' | grep -v grep | awk '{print $2}'".execute()
p.waitFor()
println p.text // this is empty, but it shouldn't

groovy 的正确执行应该是什么样子的?如何在 groovy 中获取正确的进程 ID?

【问题讨论】:

  • 可能duplicate.
  • 这不是重复的,因为它包含带引号的参数。这与 stackoverflow 上的其他问题有所不同。我已经测试了其他问题的解决方案,但没有成功。

标签: linux bash shell groovy sh


【解决方案1】:

这有点重复 cmets 中的 2 个问题,(加上我目前找不到的其他一些问题),但请尝试:

def cmd = /ps aux | grep 'unit 1' | grep -v grep | awk '{print $2}'/
def out = [ '/bin/sh', '-c', cmd ].execute().text.trim()
println out

【讨论】:

猜你喜欢
  • 2010-09-14
  • 1970-01-01
  • 2020-07-31
  • 1970-01-01
  • 2017-05-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-08
相关资源
最近更新 更多