简体   繁体   English

如何使用Groovy执行具有反引号的shell命令?

[英]How can I use Groovy to execute a shell command that has backticks?

I'm unable to use Groovy to execute a shell command that has backticks. 我无法使用Groovy执行具有反引号的shell命令。 A simplified example: 一个简化的例子:

println "echo `date`".execute().text

I searched around and tried to figure out how to escape them somehow, but without luck. 我四处寻找并试图弄清楚如何以某种方式逃脱它们,但没有运气。

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. java的Runtime#exec(String)将在下面使用,如果你在String上调用execute() In which case, this simply tokenizes the string and executes the program echo with the argument 在这种情况下,这只是简单地标记字符串并使用参数执行程序echo

`date`

or 要么

$(date)

but that's shell (bash) syntax, and must be executed via bash. 但那是shell(bash)语法,必须通过bash执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM