Véase la documentación de String sobre execute()
:
Executes the given string as a command line process. For more control over the process mechanism in JDK 1.5 you can use java.lang.ProcessBuilder.
Returns: the Process which has just started for this command line string
Sigue un ejemplo de uso:
generaciondecodigos@nereida:~/src/groovy/processes$ cat -n retcode.groovy 1 #!/usr/bin/env groovy 2 sarg = args.join(' ') 3 def command = """ 4 ls $sarg 5 """// Create the String 6 def proc = command.execute() 7 proc.waitFor() 8 9 println "return code: ${proc.exitValue()}" 10 println "stderr: ${proc.err.text}" 11 println "stdout: ${proc.in.text}"Veamos los resultados de dos ejecuciones:
generaciondecodigos@nereida:~/src/groovy/processes$ ./retcode.groovy -l *.groovy return code: 0 stderr: stdout: -rwxr-xr-x 1 generaciondecodigos generaciondecodigos 177 2009-11-18 11:00 cat2.groovy -rwxr-xr-x 1 generaciondecodigos generaciondecodigos 172 2009-11-18 11:14 cat.groovy -rwxr-xr-x 1 generaciondecodigos generaciondecodigos 351 2009-11-18 10:52 grooshEx2.groovy -rwxr-xr-x 1 generaciondecodigos generaciondecodigos 145 2009-11-18 10:36 grooshEx.groovy -rwxr-xr-x 1 generaciondecodigos generaciondecodigos 274 2010-03-01 11:19 retcode.groovy generaciondecodigos@nereida:~/src/groovy/processes$ ./retcode.groovy doesnotexist return code: 2 stderr: ls: no se puede acceder a doesnotexist: No existe el fichero ó directorio stdout:
Take care if you wish to pass a quoted argument that contains white space – it will be split into multiple arguments, e.g.:
generaciondecodigos@nereida:~/src/groovy/processes$ ls -l 'a file' -rw-r--r-- 1 generaciondecodigos generaciondecodigos 3 2010-03-01 11:23 a file generaciondecodigos@nereida:~/src/groovy/processes$ ./retcode.groovy 'a file' return code: 2 stderr: ls: no se puede acceder a a: No existe el fichero ó directorio ls: no se puede acceder a file: No existe el fichero ó directorio stdout:
The call"""executable "first with space" second""".execute()
invokesexecutable
with the following arguments:
* arg1 = "first * arg2 = with * arg3 = space" * arg4 = second
In such a case, you may prefer to use one of the array or list of String variations, e.g.:
["executable", "first with space", "second"].execute()
Veamos un ejemplo:
~/Lgroovy/files$ ls -l 'a file' -rw-r--r-- 1 casianorodriguezleon staff 28 16 abr 06:10 a file ~/Lgroovy/files$ groovysh Groovy Shell (1.6.5, JVM: 1.6.0_17) Type 'help' or '\h' for help. ----------------------------------------------------------------------------------------------------------------------------------------------------- groovy:000> r = ['ls', 'a file'].execute() ===> java.lang.UNIXProcess@50a69b6b groovy:000> r.waitFor() ===> 0 groovy:000> r.exitValue() ===> 0 groovy:000> r.err.text ===> groovy:000> r.in.text ===> a file
Como método de una lista execute
nos permite introducir Expresiones shell
en los argumentos:
groovy:000> p=['bash', '-c', 'ls -l|head -4'].execute() ===> java.lang.UNIXProcess@27af8502 groovy:000> p.waitFor() ===> 0 groovy:000> p.in.text ===> total 28 -rw-r--r-- 1 lhp lhp 0 2010-04-21 10:51 cat -n -rw-r--r-- 1 lhp lhp 422 2010-04-21 11:03 feedProcess.groovy -rw-r--r-- 1 lhp lhp 140 2010-04-21 11:11 input.txt