Go语言调用Shell与可执行文件(调用Shell脚本)

5年以前  |  阅读数:1928 次  |  编程语言:Golang 
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))
}

Copyright© 2013-2020

All Rights Reserved 京ICP备2023019179号-8