加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-14 16:23:39 所属栏目:大数据 来源:网络整理
导读:我正在使用Jenkins在 linux机器上启动脚本. 当我在服务器上手动运行它时,它可以工作: / bin / bash -c’/ some / script MyProduct SomeBranch’ 当我用groovy运行它时,它不起作用. 我得到了同样的错误,好像我没有传递“-c”选项,所以不知何故“-c”不起作
我正在使用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",branch]

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读