英文:
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()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论