douyazi1129
2017-04-28 10:57
浏览 35
已采纳

无法从Golang运行Shellscript

I have to run the installable shell script from the golang. I have to provide all yes option while running the script.

I can run it from command like,

yes | install.sh

How can i give the same in golang.

I tried the following,

c := exec.Command("/bin/bash", 'path to install.sh', '| yes');
err = c.Run()

c := exec.Command("/bin/bash", 'yes | ', 'path to install.sh')
err = c.Run()

But both are not working. I need a help for this.

Thanks in advance

图片转代码服务由CSDN问答提供 功能建议

我必须从golang运行可安装的shell脚本。 我必须在运行脚本时提供所有yes选项。

我可以通过类似命令

  yes |运行它。  install.sh 
   
 
 

如何在golang中提供相同的内容。

我尝试了以下操作, \ n

  c:= exec.Command(“ / bin / bash”,'install.sh的路径','|是'); 
err = c.Run()
 
c:  = exec.Command(“ / bin / bash”,'是|','安装路径。sh')
err = c.Run()
   
 
 

但是两者都不起作用。 我需要帮助。

谢谢

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dqphg40600 2017-05-17 12:45
    已采纳

    I tried with the below code and it works fine in my case.

    cmd1 := exec.Command("/bin/bash","-c", "yes")
    cmd2 := exec.Command("path to install.sh", "2>&1 >/dev/null")
    cmd2.Stdin, _ = cmd1.StdoutPipe()
    cmd2.Stdout = os.Stdout
    _ = cmd2.Start()
    _ = cmd1.Run()
    _ = cmd2.Wait()
    
    打赏 评论

相关推荐 更多相似问题