doutuan8887
doutuan8887
2018-11-17 11:13

管道命令并使用命令重定向golang中的输出

  • shell

Here is my code :

package main

import (
    "fmt"
    "os"
    "os/exec"
    "strconv"
    "time"
)

func main() {
    year, month, day := time.Now().Date()
    monthI := int(month)
    fmt.Println("toto")
    date := strconv.Itoa(year)+"_"+strconv.Itoa(monthI)+"_"+strconv.Itoa(day)
    nameSnapshot := "storedb@backup_"+date
    args := []string{"snapshot",nameSnapshot}
    cmd := exec.Command("zfs", args...)
    err := cmd.Run()
    if err != nil {
        os.Stderr.WriteString(err.Error())
    }
    args = []string{"send",nameSnapshot,"|","gzip",">","backup_"+date+".gz"}
    cmd = exec.Command("zfs", args...)
    err = cmd.Run()
    if err != nil {
        os.Stderr.WriteString(err.Error())
    }


}

I would like to do it in one command.

My second line the zfs send command seems not working.

How to pipe and redirect the ouput in golang with cmd.exec ?

Regards

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答