Groovy:具有嵌入式引号的字符串不会按预期执行

时间:2010-09-01 22:21:27

标签: groovy

这很奇怪:使用groovy字符串来保存一些命令行以供执行,我发现有时如果字符串中有引号字符,则执行只是默默地失败。

WTF? (更新,见下文。)

这是我的测试程序:

print " 1: " ; "grep nameserver /etc/resolv.conf".execute().text.eachLine {println it }          ; println ""
print " 2: " ; "grep 'nameserver' /etc/resolv.conf".execute().text.eachLine {println it }        ; println ""
print " 3: " ; """grep nameserver /etc/resolv.conf""".execute().text.eachLine {println it }      ; println ""
print " 4: " ; """grep "nameserver" /etc/resolv.conf""".execute().text.eachLine {println it }    ; println ""
print " 5: " ; """grep 'nameserver' /etc/resolv.conf""".execute().text.eachLine {println it }    ; println ""
print " 6: " ; "awk /nameserver/ /etc/resolv.conf".execute().text.eachLine{ println it }         ; println ""
print " 7: " ; "awk '/nameserver/' /etc/resolv.conf".execute().text.eachLine{ println it }       ; println ""
print " 8: " ; """awk "/nameserver/" /etc/resolv.conf""".execute().text.eachLine{ println it }   ; println ""
print " 9: " ; """awk '/nameserver/' /etc/resolv.conf""".execute().text.eachLine{ println it }   ; println ""
print "10: " ; """awk "/nameserver/{print \$2}" /etc/resolv.conf""".execute().text.eachLine{ println it }   ; println ""
return

结果如下:

$ groovy weird.groovy 
 1: nameserver 10.3.0.101
nameserver 10.4.1.237
nameserver 10.2.1.34

 2: 
 3: nameserver 10.3.0.101
nameserver 10.4.1.237
nameserver 10.2.1.34

 4: 
 5: 
 6: nameserver 10.3.0.101
nameserver 10.4.1.237
nameserver 10.2.1.34

 7: 
 8: nameserver 10.3.0.101
nameserver 10.4.1.237
nameserver 10.2.1.34
domain lnx.copansys.com
search lnx.copansys.com tst.copansys.com copansys.com

 9: 
10: 

更新

这是另一个例子:

"bash -c echo Hello".execute().text.eachLine{ println it }
"bash -c echo 'Hello'".execute().text.eachLine{ println it }
"bash -c echo \'Hello\'".execute().text.eachLine{ println it }

结果:

groovy> "bash -c echo Hello".execute().text.eachLine{ println it } 
groovy> "bash -c echo 'Hello'".execute().text.eachLine{ println it } 
groovy> "bash -c echo \'Hello\'".execute().text.eachLine{ println it } 

'Hello!'

观察嵌入反斜杠引用'似乎没有帮助。

2 个答案:

答案 0 :(得分:11)

当您使用String.execute()执行命令时,它们不会被命令shell解析。引号传递给执行的实际命令;在这种情况下,grep和awk。

通过用echo:

替换grep和awk来说明这一点
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 ""

结果是:

 1: something

 2: 'something'

 3: something

 4: "something"

 5: 'something'

一个简单的解决方法是将命令行构建为字符串列表:

["awk", 'BEGIN { print "hello" }'].execute().text

如果您需要更好地控制解析命令的方式,请查看ProcessBuilder,Java类String.execute()是围绕它构建的。

答案 1 :(得分:0)

http://docs.groovy-lang.org/latest/html/documentation/index.html#all-strings
看来你所期望的python样式引用即"'"')和"""""""")在groovy中不存在,所以'或{{字符串文字中的1}}必须写为"\'