I’m unable to use Groovy to execute a shell command that has backticks. A simplified example:
println "echo `date`".execute().text
I searched around and tried to figure out how to escape them somehow, but without luck.
In this environment, Groovy offers us the ability to execute commands with the help of the . execute () method of the String class and treat the output of this as if it were a chain, using all the language power.
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.
The backtick allows you to assign the output of a shell command to a variable. While this doesn't seem like much, it is a major building block in script programming. You must surround the entire command line command with backtick characters: # testing=`date`
A Groovy shell is a command-line application that lets you evaluate Groovy expressions, functions, define classes and run Groovy commands. The Groovy shell can be launched in Groovy projects.
What happens if you try:
println ["bash", "-c", "echo `date`"].execute().text
My guess would be that with
"echo `date`".execute()
java's Runtime#exec(String)
would be used underneath, if you were calling execute()
on a String. In which case, this simply tokenizes the string and executes the program echo
with the argument
`date`
or
$(date)
but that's shell (bash) syntax, and must be executed via bash.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With