Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 使用groovy,如何通过管道传递多个shell命令?_Bash_Shell_Groovy_Process - Fatal编程技术网

Bash 使用groovy,如何通过管道传递多个shell命令?

Bash 使用groovy,如何通过管道传递多个shell命令?,bash,shell,groovy,process,Bash,Shell,Groovy,Process,使用Groovy及其java.lang.Process支持,我如何将多个shell命令管道连接在一起 考虑这个bash命令(假设您的用户名是foo): 这将打印出用户名-一行用于与您的用户帐户相关的某些进程 使用Groovy,文档和代码说我应该能够做到这一点以获得相同的结果: def p = "ps aux".execute() | "grep ' foo'".execute() | "awk '{print $1}'".execute() p.waitFor() println p.text

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

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

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

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

def p = "ps aux".execute() | "grep ' foo'".execute() | "awk '{print $1}'".execute()
p.waitFor()
println p.text
但是,除此之外,我无法获得任何文本输出:

def p = "ps aux".execute()
p.waitFor()
println p.text
一旦我开始配管,println就不会打印任何内容


想法?

您可以这样做,让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 $1}'/].execute()
p.waitFor()
println p.text
这对我很有用:

def p = 'ps aux'.execute() | 'grep foo'.execute() | ['awk', '{ print $1 }'].execute()
p.waitFor()
println p.text
由于未知的原因,awk的参数不能仅用一个字符串发送(我不知道为什么!可能bash引用了不同的内容)。如果使用命令转储错误流,您将看到与awk脚本编译相关的错误

编辑:事实上

  • “-string-”.execute()
    委托给
    Runtime.getRuntime().exec(-string-)
  • bash的任务是处理包含带“或”的空格的参数。Runtime.exec或操作系统不知道引号
  • 正在执行
    “grep'foo'”。execute()
    执行命令grep,第一个参数是
    '
    ,第二个参数是
    foo'
    :无效。对于awk也是如此

  • 如果你想要异步,我建议你

     proc.consumeProcessOutputStream(new LineOrientedOutputStream() {
            @Override
            protected void processLine(String line) throws IOException {
               println line
            }
        }
        );
    
    这对我很有效

    def command = '''
        ps aux | grep bash | awk '{print $1}'
    '''
    def proc = ['bash', '-c', command].execute()
    proc.waitFor()
    println proc.text
    
    如果要运行多个命令,可以将其添加到命令中

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

    是的,我可以,这可能是我需要走的路,但是你知道为什么“或”方法不能像文档中定义的那样工作吗?这与awk有关。不确定是什么,但它不喜欢以这种方式运行有趣的是,slashy方法起作用了。使用双引号没有。想想看!这个答案不是错的。但它没有回答这个问题最初的问题:如何在groovy中使用进程对象之间的|运算符。通过bash只执行一个命令,是bash对进程进行管道传输,而不是groovy@tim_yates是的,我认为它与awk或嵌套的单引号有关。使用斜杠字符串为我解决了问题,所以我想我将继续使用它。不当然,关于Groovy管道的问题仍然存在:“@tim_yates的回答以不同的方式解决了我的特定问题(对此我非常感谢),这个答案解决了Groovy的
    操作符和
    进程
    对象的OP问题,所以我会奖励它。谢谢!这与上面的@tim_yates答案相同-他只是使用了一个斜杠字符串而不是一个heredoc:)@LesHazlewood对我来说,不同的是这个有效,而斜杠字符串无效。
    def command = '''
        ls -ltr
        cat secret
    '''
    def proc = ['bash', '-c', command].execute()
    proc.waitFor()
    println proc.text