使用选项和参数执行shell脚本

Kat*_*tie 4 groovy

我正在使用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 )
Run Code Online (Sandbox Code Playgroud)

这是在Groovy中构建命令的正确方法吗?:

def cmd = "/bin/bash -c '/some/script "+product+" "+branch+"'"
cmd.execute()
Run Code Online (Sandbox Code Playgroud)

谢谢!

我试过类似/有用资源的问题:

Opa*_*pal 5

尝试以下列方式运行该命令:

def cmd = ["/bin/bash", "-c", "/some/script", product, branch]
Run Code Online (Sandbox Code Playgroud)

您也可以尝试:

def cmd = ["/some/script", product, branch]
Run Code Online (Sandbox Code Playgroud)

if /some/script是可执行的 - BTW是否置于root(/)下?