一般不建議使用這種默認方式調用Shell腳本:
cmd := exec.Command("my_shell.sh")
。因為這種方式實際的執行結果和命令行執行#sh my_shell.sh
一樣,如果你的Shell腳本不滿足sh的規范,就會調用失敗。
package main
import (
"fmt"
"os/exec"
)
func main(){
cmd := exec.Command("touch", "test_file")
err := cmd.Run()
if err != nil {
fmt.Println("Execute Command failed:" + err.Error())
return
}
fmt.Println("Execute Command finished.")
}