Using groovy, how do you pipe multiple shell commands?

前端 未结 4 2263
礼貌的吻别
礼貌的吻别 2020-12-05 11:30

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

Consider this bash command (and assume your username i

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-05 12:09

    This has worked for me

    def command = '''
        ps aux | grep bash | awk '{print $1}'
    '''
    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
    

提交回复
热议问题