设置bash来调用指定Shell脚本,dir_size.sh为我们测试用的Shell脚本。调用完成后打印Shell脚本的标准输出到控制台。
package main
import (
"fmt"
"os/exec"
)
func main(){
command := `./dir_size.sh .`
cmd := exec.Command("/bin/bash", "-c", command)
output, err := cmd.Output()
if err != nil {
fmt.Printf("Execute Shell:%s failed with error:%s", command, err.Error())
return
}
fmt.Printf("Execute Shell:%s finished with output:\n%s", command, string(output))
}