When you execute commands with String.execute()
, they don't get parsed by a command shell. The quotes are passed through to the actual commands executed; in this case, grep and awk.
This is illustrated by replacing grep and awk with echo:
print " 1: " ; "echo something".execute().text.eachLine {println it } ; println ""
print " 2: " ; "echo 'something'".execute().text.eachLine {println it } ; println ""
print " 3: " ; """echo something""".execute().text.eachLine {println it } ; println ""
print " 4: " ; """echo "something" """.execute().text.eachLine {println it } ; println ""
print " 5: " ; """echo 'something'""".execute().text.eachLine {println it } ; println ""
Which results in:
1: something
2: 'something'
3: something
4: "something"
5: 'something'
A simple workaround is to build the command line as a list of strings:
["awk", 'BEGIN { print "hello" }'].execute().text
If you need finer control over the how the command is parsed, take a look at ProcessBuilder
, the Java class String.execute()
is built around.