douao3636 2016-10-08 07:58
浏览 31
已采纳

Golang-执行命令

Prompt as in golang to execute such command:

/bin/bash script.sh < text.txt

I execute a script with parameters so:

package main

import (
    "fmt"
    "os/exec"
    "log"
    "os"
)

func main() {
    argstr := []string{"script.sh", "arg1", "arg2"}
    out, err := exec.Command("/bin/bash", argstr...).Output()
    if err != nil {
        log.Fatal(err)
        os.Exit(1)
    }
    fmt.Println(string(out))
}

And here is how to transfer an output from the text file?

  • 写回答

2条回答 默认 最新

  • douhui3760 2016-10-08 08:00
    关注

    The command you should execute is:

    /bin/bash -c 'script.sh < text.txt'
    

    So

    argstr := []string{"-c", "script.sh < text.txt"}
    

    Bash will interpret input redirection and will do the job.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?