Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute shell script with options and parameters

Tags:

groovy

I'm using Jenkins to launch a script on a linux machine.

When I run this manually on the server, it works:

/bin/bash -c '/some/script MyProduct SomeBranch'

When I run this with groovy, it doesn't work.

I get the same error as if I didn't pass the "-c" option, so somehow the "-c" isn't working.

Here is my code:

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 )

Is this the correct way to build the command in Groovy?:

def cmd = "/bin/bash -c '/some/script "+product+" "+branch+"'"
cmd.execute()

Thanks!

Question similar to / helpful resources that I tried:

  • 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/
like image 563
Katie Avatar asked Mar 25 '15 18:03

Katie


1 Answers

Try to run the command in the following way:

def cmd = ["/bin/bash", "-c", "/some/script", product, branch]

You may also try:

def cmd = ["/some/script", product, branch]

if /some/script is executable - BTW is it placed under root (/)?

like image 95
Opal Avatar answered Oct 03 '22 01:10

Opal