2019-07-18 07:23
浏览 181


I am trying to write a file from a bash command into a file in Go. Note there are several reasons for using Go over bash here: I have some more logic such as parsing configuration files, I would like to run that code for multiple DBs in parallele and finally performing some more complex data manipulation after.

    dumpStr := fmt.Sprintf("pg_dump -U %s -h %s %s | gzip", DbUserName, DbHost, DbName)
    cmd := exec.Command("bash", "-c", dumpStr)
    cmd.Env = append(cmd.Env, "PGPASSWORD="+DbPassword)

    outfile, err := os.Create(DbName + ".gz")
    if err != nil {
    outfile = cmd.Stdout
    defer outfile.Close()
    err = cmd.Start()
    if err != nil {

However, I am getting an emtpy result. I am getting data if I am executing dumpStr from the CLI but not from that code... What am I missing?

图片转代码服务由CSDN问答提供 功能建议

我正在尝试将bash命令中的文件写入Go中的文件中。 请注意以下几种原因: 在这里使用Go over bash:我有更多逻辑,例如解析配置文件,我想并行地为多个DB运行该代码,并最终在此后执行一些更复杂的数据操作。

   dumpStr:= fmt.Sprintf(“ pg_dump -U%s -h%s%s | gzip”,DbUserName,DbHost,DbName)
 cmd:= exec.Command(“ bash”,“ -c”,  dumpStr)
 cmd.Env = append(cmd.Env,“ PGPASSWORD =” + DbPassword)
 outfile,err:= os.Create(DbName +“ .gz”)
 if err!= nil {\  n panic(err)
 outfile = cmd.Stdout 
 err = cmd.Start()
如果err!= nil {
} \  n cmd.Wait()

但是,我得到的是空结果。 如果我从CLI执行 dumpStr ,而不是从该代码执行,我正在获取数据... 我缺少什么?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

相关推荐 更多相似问题