使用选项和参数执行 shell 脚本
Execute shell script with options and parameters
我正在使用 Jenkins 在 linux 机器上启动脚本。
当我在服务器上运行这个手动时,它有效 :
/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+"'"
cmd.execute()
谢谢!
与我尝试过的有用资源类似的问题:
- groovy execute with parameters containing spaces
- Groovy executing shell commands
- http://www.joergm.com/2010/09/executing-shell-commands-in-groovy/
- https://fbflex.wordpress.com/2011/11/30/replacing-the-groovy-execute-method-with-one-that-prints-output-while-the-process-is-running/
尝试按以下方式运行命令:
def cmd = ["/bin/bash", "-c", "/some/script", product, branch]
您也可以试试:
def cmd = ["/some/script", product, branch]
如果 /some/script
是可执行的 - 顺便说一句,它是否放在根目录下 (/
)?
我正在使用 Jenkins 在 linux 机器上启动脚本。
当我在服务器上运行这个手动时,它有效 :
/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+"'"
cmd.execute()
谢谢!
与我尝试过的有用资源类似的问题:
- groovy execute with parameters containing spaces
- Groovy executing shell commands
- http://www.joergm.com/2010/09/executing-shell-commands-in-groovy/
- https://fbflex.wordpress.com/2011/11/30/replacing-the-groovy-execute-method-with-one-that-prints-output-while-the-process-is-running/
尝试按以下方式运行命令:
def cmd = ["/bin/bash", "-c", "/some/script", product, branch]
您也可以试试:
def cmd = ["/some/script", product, branch]
如果 /some/script
是可执行的 - 顺便说一句,它是否放在根目录下 (/
)?