无法从Golang运行Shell脚本。

huangapple go评论82阅读模式
英文:

Unable to Run the Shellscript From Golang

问题

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

我可以在命令行中这样运行:

yes | install.sh

请问我该如何在golang中实现相同的效果?

我尝试了以下方法:

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

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

但是这两种方法都不起作用。请帮帮我。

提前感谢!

英文:

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

答案1

得分: 0

我尝试了下面的代码,在我的情况下运行良好。

cmd1 := exec.Command("/bin/bash", "-c", "yes")
cmd2 := exec.Command("install.sh的路径", "2>&1 >/dev/null")
cmd2.Stdin, _ = cmd1.StdoutPipe()
cmd2.Stdout = os.Stdout
_ = cmd2.Start()
_ = cmd1.Run()
_ = cmd2.Wait()
英文:

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()

huangapple
  • 本文由 发表于 2017年4月28日 18:57:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/43678563.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定