delovepr
delovepr

Reputation: 71

How to execute shell command with parameters in groovy?

I am trying to use shell command notify-send -t 2000 "Title" "Message" from groovy in next way "notify-send -t 2000 \"Title\" \"Message\"".execute() and it works perfect. But when i am trying to put exprsession in place of message it seems nothing works. here is broken code below:

def todayDate = new Date()

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

can you help me with understanding?

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

Upvotes: 4

Views: 11674

Answers (3)

Nux
Nux

Reputation: 10062

So the problem occurs when you have a space in your parameter.

So for example this will not work:

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

send_mail.sh will receive 2 parameters instead of 1.

So to avoid problems split your string by arguments. So something like this:

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

First item (0 index) is a command and then arguments follow.

Upvotes: 3

delovepr
delovepr

Reputation: 71

It seems this issue was mostly related to string formatting and misunderstanding "Message 9 May 2016" as one String(argument). For me it works in next way:

def command = new String[3]
command[0] = "notify-send"
command[1] = "Title"
command[2] = "\"Message ${todayDate}.toString()\"".toString()
def process = new ProcessBuilder(command).start()

Anyway, thanks to everyone.

Upvotes: 1

Sapna Rao
Sapna Rao

Reputation: 44

You need to declare the command as a variable beforehand and then execute the command.

You can do it like this:

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

Upvotes: 1

Related Questions