Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using groovy, how do you pipe multiple shell commands?

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 is foo):

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

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

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

def p = "ps aux".execute() | "grep ' foo'".execute() | "awk '{print $1}'".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.

Thoughts?

like image 543
Les Hazlewood Avatar asked Feb 03 '16 20:02

Les Hazlewood


People also ask

How do I run shell command in groovy?

Executing shell commands using Groovy is very easy. For example If you want to execute any unix/linux command using groovy that can be done using execute() method and to see the output of the executed command we can append text after it.

What is pipe bash?

A pipe in Bash takes the standard output of one process and passes it as standard input into another process. Bash scripts support positional arguments that can be passed in at the command line.


3 Answers

This works for me :

def p = 'ps aux'.execute() | 'grep foo'.execute() | ['awk', '{ print $1 }'].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.

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
like image 179
Jérémie B Avatar answered Oct 12 '22 14:10

Jérémie B


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

// 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
like image 40
tim_yates Avatar answered Oct 12 '22 13:10

tim_yates


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
like image 36
Here_2_learn Avatar answered Oct 12 '22 14:10

Here_2_learn