groovy execute with parameters containing spaces

15,865

Solution 1

The trick was to use a list:

println(['ls', '/tmp/folder with spaces'].execute().text)

Solution 2

Sorry man, none of the tricks above worked for me. This piece of horrible code is the only thing that went thru:

    def command = 'bash ~my_app/bin/job-runner.sh -n " MyJob today_date=20130202 " ' 
    File file = new File("hello.sh")
    file.delete()       
    file << ("#!/bin/bash\n")
    file << (command)
    def proc = "bash hello.sh".execute()                 // Call *execute* on the file

Solution 3

One weird trick for people who need regular quotes processing, pipes etc: use bash -c

['bash','-c',
'''
docker container ls --format="{{.ID}}" | xargs -n1 docker container inspect --format='{{.ID}} {{.State.StartedAt}}' | sort -k2,1
'''].execute().text
Share:
15,865
Johan Lübcke
Author by

Johan Lübcke

Java developer longing for python...

Updated on July 10, 2022

Comments

  • Johan Lübcke
    Johan Lübcke almost 2 years

    How do I provide arguments containing spaces to the execute method of strings in groovy? Just adding spaces like one would in a shell does not help:

    println 'ls "/tmp/folder with spaces"'.execute().text
    

    This would give three broken arguments to the ls call.