Groovy 使用选项和参数执行shell脚本
我正在使用Jenkins在linux机器上启动一个脚本 当我在服务器上手动运行此时,它工作:Groovy 使用选项和参数执行shell脚本,groovy,Groovy,我正在使用Jenkins在linux机器上启动一个脚本 当我在服务器上手动运行此时,它工作: /bin/bash-c'/some/script MyProduct SomeBranch' 当我用groovy运行这个时,不起作用 如果我没有通过“-c”选项,我会得到相同的错误,因此“-c”不知何故不起作用 这是我的密码: branchName = "SomeBranch" configName = "release" println "Building for branch "+branchNam
/bin/bash-c'/some/script MyProduct SomeBranch'
当我用groovy运行这个时,不起作用
如果我没有通过“-c”选项,我会得到相同的错误,因此“-c”不知何故不起作用
这是我的密码:
branchName = "SomeBranch"
configName = "release"
println "Building for branch "+branchName+" and configuration "+configName
def chkbranch = { String product, String branch -> mkcmd( product, branch ) }
private def mkcmd ( String product, String branch ) {
// Build the command string to run
def cmd = "/bin/bash -c '/some/script "+product+" "+branch+"'"
def sout = new StringBuffer()
def serr = new StringBuffer()
// Run the command
println "running "+cmd
def proc = cmd.execute()
proc.consumeProcessOutput ( sout, serr )
proc.waitForProcessOutput ()
println "out> $sout"
println "err> $serr"
return sout
}
chkbranch ( "MyProduct", branchName )
这是在Groovy中构建命令的正确方法吗
谢谢
问题类似于我尝试的/有用的资源:
def cmd = ["/bin/bash", "-c", "/some/script", product, branch]
您也可以尝试:
def cmd = ["/some/script", product, branch]
如果
/some/script
是可执行的-顺便说一句,它是否放在根目录下(/
)?这很有效!现在它说“权限被拒绝”访问脚本中的文件,但这次它正在运行脚本!是的,脚本是在根目录下的,有什么特别的事情我需要担心吗?不是。我考虑了权限:)它必须是[“/bin/bash”、“-c”、“/some/script$product$branch”]…
-要运行的shell命令是-c
之后的唯一参数。要加倍小心,产品和分支机构是什么。如果产品是rm-rf/
,它将运行
def cmd = ["/some/script", product, branch]