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

file – 如何正确地从groovy调用shell命令

发布时间:2020-12-14 16:24:29 所属栏目:大数据 来源:网络整理
导读:我想从我的groovy脚本执行 shell命令. 我测试了以下内容: "mkdir testdir".execute() 这只是工作正常. 现在我想制作一个文件,写一些文件,然后打开一个文本编辑器来查看文件. def execute(cmd) { def proc = cmd.execute() proc.waitFor()}execute("touch fi
我想从我的groovy脚本执行 shell命令.
我测试了以下内容:

"mkdir testdir".execute()

这只是工作正常.
现在我想制作一个文件,写一些文件,然后打开一个文本编辑器来查看文件.

def execute(cmd) {
   def proc =  cmd.execute()
   proc.waitFor()
}

execute("touch file")
execute("echo hello > file")
execute("gedit file")

现在gedit正确打开但文件中没有“hello”字符串.
这是怎么回事?!?

解决方法

你不能在行中进行重定向:

execute("echo hello > file")

因此没有任何内容写入文件.处理此问题的最简单方法可能是将所有命令包装到单个shell脚本中,然后执行此脚本.

否则,您可以从echo命令读取标准输出(不带>文件),然后在Groovy中将其写入文件.

或者你可以这样做:

execute( [ 'bash','-c','echo hello > file' ] )

哪个应该工作,因为您的执行方法将只执行the List.execute() method

(编辑:李大同)

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

    推荐文章
      热点阅读