gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 17:13:19 26 4
gpt4 key购买 nike

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

跆拳道? (更新,见下文。)

这是我的测试程序:

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!'

请注意,嵌入反斜杠引用的 ' 似乎没有帮助。

最佳答案

当您使用 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()围绕着。

关于Groovy:带有嵌入式引号的字符串不会按预期执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3622540/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com