shell commands/scripts in Script Console

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

shell commands/scripts in Script Console

Brian J. Murrell
The Script Console is a very nice/neat feature.  I've been using the
following to execute single commands:

def command = "ps axf"
def proc = command.execute()
proc.waitFor()

// Obtain status and output
println "return code: ${ proc.exitValue()}"
println "stderr: ${proc.err.text}"
println "stdout: ${proc.in.text}" // *out* from the external program is *in* for groovy

Which works well for single commands, but I have been unable to conjure
up the magic quoting/escaping to do multiple commands in a shell.  One
might think something along the lines of:

def command = "/bin/bash -c \"echo test\""

might work, but alas it does not:

return code: 2
stderr: test": -c: line 0: unexpected EOF while looking for matching `"'
test": -c: line 1: syntax error: unexpected end of file

stdout:

Obviously there is some magic ju-ju I need to properly quote all of
that.

Anyone have any ideas or experiences?

Cheers,
b.


signature.asc (205 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: shell commands/scripts in Script Console

Alan Harder-2
I do this:

println new ProcessBuilder('sh','-c','script stuff
here').redirectErrorStream(true).start().text



Brian J. Murrell wrote:

> The Script Console is a very nice/neat feature.  I've been using the
> following to execute single commands:
>
> def command = "ps axf"
> def proc = command.execute()
> proc.waitFor()
>
> // Obtain status and output
> println "return code: ${ proc.exitValue()}"
> println "stderr: ${proc.err.text}"
> println "stdout: ${proc.in.text}" // *out* from the external program is *in* for groovy
>
> Which works well for single commands, but I have been unable to conjure
> up the magic quoting/escaping to do multiple commands in a shell.  One
> might think something along the lines of:
>
> def command = "/bin/bash -c \"echo test\""
>
> might work, but alas it does not:
>
> return code: 2
> stderr: test": -c: line 0: unexpected EOF while looking for matching `"'
> test": -c: line 1: syntax error: unexpected end of file
>
> stdout:
>
> Obviously there is some magic ju-ju I need to properly quote all of
> that.
>
> Anyone have any ideas or experiences?
>
> Cheers,
> b.
>
>  

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: shell commands/scripts in Script Console

Brian J. Murrell
On Tue, 2010-09-28 at 13:56 -0700, Alan Harder wrote:
> I do this:
>
> println new ProcessBuilder('sh','-c','script stuff
> here').redirectErrorStream(true).start().text

This is working wonderfully for me.  Thanx much!

b.


signature.asc (205 bytes) Download Attachment
Loading...