gpt4 book ai didi

shell - 如何在groovy中执行带参数的shell命令?

转载 作者:行者123 更新时间:2023-12-02 04:26:18 25 4
gpt4 key购买 nike

我正在尝试以下一种方式使用来自 groovy 的 shell 命令 notify-send -t 2000 "Title""Message" "notify-send -t 2000\"Title\"\"Message\"".execute() 它工作完美。但是当我试图用表达代替消息时,似乎没有任何作用。下面是损坏的代码:

def todayDate = new Date()

def title = 'Title'
def message = " Message ${todayDate}"
println(title + message)
"notify-send -t 2000 \"${title}\" \"${message}\"".execute()

你能帮我理解一下吗?

Connected to the target VM, address: '127.0.0.1:40305', transport: 'socket' TitleFri May 06 13:41:43 CEST 2016 groovy.lang.MissingPropertyException: No such property: execute for class: DUMMY at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:51) at org.codehaus.groovy.runtime.callsite.PogoGetPropertySite.getProperty(PogoGetPropertySite.java:49) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGroovyObjectGetProperty(AbstractCallSite.java:295) at DUMMY$_closure1.doCall(DUMMY.groovy:1) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:497) at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90) at org.codehaus.groovy.runtime.metaclass.ClosureMetaMethod.invoke(ClosureMetaMethod.java:81) at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:324) at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1208) at groovy.lang.ExpandoMetaClass.invokeMethod(ExpandoMetaClass.java:1111) at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1017) at test.notification.run(notification.groovy:28)'

最佳答案

因此,当参数中有空格时就会出现问题。

例如,这不起作用:

def command = "send_mail.sh \"Some text\""
command.execute();

send_mail.sh 将收到 2 个参数,而不是 1 个。

因此,为了避免出现问题,请按参数分割字符串。所以像这样:

def command = ["send_mail.sh", "Some text"];
command.execute();

第一项(0 索引)是一个命令,后面是参数。

关于shell - 如何在groovy中执行带参数的shell命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37068982/

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