[英]Executing shell command in groovy throws 'unexpected char' error
I am trying to execute a shell command n groovy 我正在尝试执行groovy的shell命令
def shellString = "s/\[\|]\|\s\|'\|(\|)//g"
def temp2 = "echo response| sed -e ${shellString}".execute()
It throws compilation error: 它引发编译错误:
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 33: unexpected char: '\' @ line 33, column 24.
def shellString = "s/\[\|]\|\s\|'\|(\|)//g"
^
1 error
at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:310)
at org.codehaus.groovy.control.ErrorCollector.addFatalError(ErrorCollector.java:150)
at org.codehaus.groovy.control.ErrorCollector.addError(ErrorCollector.java:120)
at org.codehaus.groovy.control.ErrorCollector.addError(ErrorCollector.java:132)
at org.codehaus.groovy.control.SourceUnit.addError(SourceUnit.java:350)
at org.codehaus.groovy.antlr.AntlrParserPlugin.transformCSTIntoAST(AntlrParserPlugin.java:139)
at org.codehaus.groovy.antlr.AntlrParserPlugin.parseCST(AntlrParserPlugin.java:110)
at org.codehaus.groovy.control.SourceUnit.parse(SourceUnit.java:234)
at org.codehaus.groovy.control.CompilationUnit$1.call(CompilationUnit.java:168)
at org.codehaus.groovy.control.CompilationUnit.applyToSourceUnits(CompilationUnit.java:943)
at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:605)
at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:581)
at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:558)
at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:298)
at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:268)
at groovy.lang.GroovyShell.parseClass(GroovyShell.java:688)
at groovy.lang.GroovyShell.parse(GroovyShell.java:700)
at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.doParse(CpsGroovyShell.java:131)
at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.reparse(CpsGroovyShell.java:125)
at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.parseScript(CpsFlowExecution.java:560)
at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.start(CpsFlowExecution.java:521)
at org.jenkinsci.plugins.workflow.job.WorkflowRun.run(WorkflowRun.java:330)
at hudson.model.ResourceController.execute(ResourceController.java:97)
at hudson.model.Executor.run(Executor.java:429)
shellString
isn't a slashed string, so not sure why a \\
would create a problem. shellString
不是斜线字符串,因此不确定为什么\\
会造成问题。 Any help is appreciated. 任何帮助表示赞赏。
This will fail on many levels. 这将在许多级别上失败。 The biggest problem you will face is the fact, that execute
is really just executing a process (not a shell command). 您将面临的最大问题是, execute
实际上只是在执行一个进程(而不是Shell命令)。 So first of all you can not use |
所以首先您不能使用|
at all. 完全没有 Next quoting arguments will not work, because execute
will just split at whitespace. 下一个引用参数将不起作用,因为execute
只会在空白处分割。 So if you want to use "shellisms" use the equivalent of sh -c "..."
instead and use execute
on a string array. 因此,如果要使用“ shellisms”,请使用等效于sh -c "..."
的字符串,并在字符串数组上使用execute
。 Eg 例如
["sh", "-c", "..."].execute()
Then you can put your ...
shell code in there with all the redirections, quotings, env-vars etc. with the proper Groovy quoting applied as mentioned in the other answer. 然后,您可以将...
外壳代码以及所有重定向,引号,env-vars等放入其中,并使用其他答案中提到的适当的Groovy引号。
And to circumvent all of that: why even bother with sed
here? 而要绕开所有这些:为什么还要在这里烦恼sed
? Just use replaceAll
on the resulting string on the groovy side of things. 只需在事物的常规方面对结果字符串使用replaceAll
。
You need to escape slash to avoid compilation errors: 您需要转义斜线以避免编译错误:
def shellString = "s/\\[\\|]\\|\\s\\|'\\|(\\|)//g"
def temp2 = "echo response| sed -e ${shellString}".execute()
println temp2.text
Output: 输出:
response| sed -e s/\[\|]\|\s\|'\|(\|)//g
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.