1) : directly execute a string statement, executing a string
A string can be executed in the standard java way:
def command = """executable arg1 arg2 arg3"""// Create the String
def proc = command.execute() // Call *execute* on the string
proc.waitFor() // Wait for the command to finish
// 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
Gotchas:Take care if you wish to pass a quoted argument that contains white space– it will be split into multiple arguments, e.g.:
"""executable "first with space" second""".execute()
will invoke executable with the following arguments:
arg1 = “first
arg2 = with
arg3 = space”
arg4 = second
In such a case, you may prefer to useone of the array or list of String variations, e.g.:
["executable", "first with space", "second"].execute()
2)Processing shell command with piping in groovy script, Using pipeline functions in groovy scripts
def proc = “ffmpeg -i /tmp/sample.m4a -f ffmetadata 2>&1 | grep Duration”.execute()
is not working.the ‘2>1&1’ bit is shell functionality, and Groovy processes don’t invoke the shell, they just start a program. If you really need it, you should do something like:
def proc1 = [‘/bin/bash’, ‘-c’, ‘/usr/local/bin/ffmpeg -i /tmp/sample.m4a -fffmetadata 2>&1’].execute()
or similar – do some experimenting. That is, you need to call the shell with the command you want executed as argument.
3): using ant builder’s exec task
Ant has an exec task and it be accessed from the AntBuilder object
def ant = new AntBuilder() // create an antbuilder
ant.exec(outputproperty:"cmdOut",
errorproperty: "cmdErr",
resultproperty:"cmdExit",
failonerror: "true",
executable: '/opt/myExecutable') {
arg(line:"""*"first with space"* second""")
}
println "return code: ${ant.project.properties.cmdExit}"
println "stderr: ${ant.project.properties.cmdErr}"
println "stdout: ${ ant.project.properties.cmdOut}"
Similar Posts:
- Linux Error: missing argument to `-exec’ [How to Solve]
- linux find: missing argument to `-exec’
- Python calls shell script: oserror: [errno 8] exec format err
- Linux shell script execution error: bad substitution [How to Solve]
- [Solved] Java Call Error: java.lang.IllegalArgumentException: wrong number of arguments
- Solutions to the problem of too many arguments in Linux Bash
- Difference between normalize space (.) and normalize space (text ()) in XPath
- [Solved] OCI runtime exec failed……executable file not found in $PATH”: unknown
- [Solved] appium Error: A new session could not be created
- [Solved] warning MSB8004: Output Directory does not end with a trailing slash. This build instance will add the slash as it is required to allow proper evaluation of the Output Directory.