bash 使用 groovy,您如何通过管道传输多个 shell 命令?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35187151/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 22:11:32  来源:igfitidea点击:

Using groovy, how do you pipe multiple shell commands?

bashshellgroovyprocess

提问by Les Hazlewood

Using Groovy and it's java.lang.Processsupport, how do I pipe multiple shell commands together?

使用 Groovy 及其java.lang.Process支持,如何将多个 shell 命令连接在一起?

Consider this bash command (and assume your username is foo):

考虑这个 bash 命令(并假设您的用户名是foo):

ps aux | grep ' foo' | awk '{print }'

This will print out usernames - one line for some processes related to your user account.

这将打印出用户名 - 与您的用户帐户相关的某些进程的一行。

Using Groovy, the ProcessGroovyMethodsdocumentation and code says I should be able to do this to achieve the same result:

使用 Groovy,ProcessGroovyMethods文档和代码说我应该能够做到这一点以获得相同的结果:

def p = "ps aux".execute() | "grep ' foo'".execute() | "awk '{print }'".execute()
p.waitFor()
println p.text

However, I can't get any text output for anything other than this:

但是,除此之外,我无法获得任何文本输出:

def p = "ps aux".execute()
p.waitFor()
println p.text

As soon as I start piping, the println does not print out any anything.

一旦我开始管道, println 就不会打印出任何东西。

Thoughts?

想法?

回答by Jérémie B

This works for me :

这对我有用:

def p = 'ps aux'.execute() | 'grep foo'.execute() | ['awk', '{ print  }'].execute()
p.waitFor()
println p.text

for an unknown reason, the parameters of awk can't be send with only one string (i don't know why! maybe bash is quoting something differently). If you dump with your command the error stream, you'll see error relative to the compilation of the awk script.

由于未知原因,awk 的参数不能只用一个字符串发送(我不知道为什么!也许 bash 引用了不同的东西)。如果您使用命令转储错误流,您将看到与 awk 脚本编译相关的错误。

Edit: In fact,

编辑:事实上,

  1. "-string-".execute()delegate to Runtime.getRuntime().exec(-string-)
  2. It's bash job to handle arguments containing spaces with ' or ". Runtime.exec or the OS are not aware of the quotes
  3. Executing "grep ' foo'".execute()execute the command grep, with 'as the first parameters, and foo'as the second one : it's not valid. the same for awk
  1. "-string-".execute()委托给 Runtime.getRuntime().exec(-string-)
  2. 处理包含带有 ' 或 " 空格的参数是 bash 工作。Runtime.exec 或操作系统不知道引号
  3. 执行"grep ' foo'".execute()命令grep,'作为第一个参数,foo'作为第二个:它是无效的。awk 也一样

回答by tim_yates

You can do this to just let the shell sort it out:

你可以这样做,让 shell 把它整理出来:

// slash string at the end so we don't need to escape ' or $
def p = ['/bin/bash', '-c', /ps aux | grep ' foo' | awk '{print }'/].execute()
p.waitFor()
println p.text

回答by Here_2_learn

This has worked for me

这对我有用

def command = '''
    ps aux | grep bash | awk '{print }'
'''
def proc = ['bash', '-c', command].execute()
proc.waitFor()
println proc.text

If you want to run multiple commands, you can add it in the command.

如果要运行多个命令,可以在命令中添加。

def command = '''
    ls -ltr
    cat secret
'''
def proc = ['bash', '-c', command].execute()
proc.waitFor()
println proc.text

回答by Gilad Baruchian

If you want it async I recommend

如果你想要异步,我推荐

 proc.consumeProcessOutputStream(new LineOrientedOutputStream() {
        @Override
        protected void processLine(String line) throws IOException {
           println line
        }
    }
    );