1. 程式人生 > >go語言exec包呼叫shell命令

go語言exec包呼叫shell命令

工程中需要用到ffmpeg,想直接用exec包呼叫shell命令。本來以為很簡單,結果折騰了一下午,最後查到了解決方案。

假如之前執行報錯的語句為:

cmd := exec.Command("echo", "'helloworld!'")
out, err := cmd.Output()

那麼改為:
c := "echo hello world"
cmd := exec.Command("sh", "-c", c)
out, err := cmd.Output()

即可!

感謝萬能的stack overflow

reference:

https://stackoverflow.com/questions/20437336/how-to-execute-system-command-in-golang-with-unknown-arguments/20438245#20438245?newreg=80e5ccfd4f7f447c9acc931f5509af2b