一般不建议使用这种默认方式调用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.")
}