doukuanghuan7582 2015-09-19 14:17
浏览 385
已采纳

如何将exec.Command的输出传递到Golang中的另一个命令

I have eight Microsoft access databases each one has around 215 tables and I needed to transfered those databases into postgresql so i used mdb-tools and exported the schemes which just one step ;but when it come to exporting tables data into postgres directly into postgresql i have to write this command for every single table:

mdb-export -I postgres -q \' myaccessdatabase.mdb table-name | psql -d mypsqldatabase -U postgres -w -h localhost 

so I have been trying to write a go command program to do as follows: 1. firstly excute a command to list tables name. which will be the arg of the next command. 2. then start for range looop to excute a command that export tanle data and the output of this command is pipe into the next command. 3. this command is psql which will write the output from the previous command ( which is sql insert statment)

package main

import (
    "bufio"
    "log"
    "os"
    "os/exec"
)
func main() {
    // command to Collect tables name and list it to the next command 
          tablesname := exec.Command("mdb-tables", "-1", "myaccessdatabase.mdb")

    // Run the command on each table name and get the output pipe/feed into the psql shell 
    for _, table := range tablesname {
        ls := exec.Command("mdb-export", "-I", "postgres", "-q", "\'", "myaccessdatabase.mdb", table)

        // | psql -d mydatabase -U postgres -w -h localhost command which will write each line from the output of previouse command's 
        visible := exec.Command("psql", "-d", "mypsqldatabase", "-U", "postgres", "-w", "-h", "localhost")



    }
}

So i have tried to pipe the output into the stdin of the next command and couldn't implement it , meanwhile I am trying with goroutin and channels just been unable to even come with a way to make this into the last command.

Thank you very much in advance.

  • 写回答

1条回答 默认 最新

  • duandong2562 2015-09-19 14:48
    关注

    The exec.Command function only creates the command, it doesn't execute it.

    To get the output from tablesname := exec.Command("mdb-tables", "-1", "myaccessdatabase.mdb"), you need to run the command and capture its output:

    tablesname := exec.Command("mdb-tables", "-1", "myaccessdatabase.mdb")
    //capture the output pipe of the command
    outputStream := bufio.NewScanner(tablesname.StdoutPipe())
    tablesname.Start()  //Runs the command in the background
    
    for outputStream.Scan() {   
    //Default scanner is a line-by-line scan, so this will advance to the next line
        ls := exec.Command("mdb-export", "-I", "postgres", "-q", "\'", "myaccessdatabase.mdb", outputStream.Text())
        ls.Run()  //Blocks until command finishes execution
    
        visible := exec.Command("psql", "-d", "mypsqldatabase", "-U", "postgres", "-w", "-h", "localhost")
        visible.Run()
    }
    
    tablesname.Wait()  //Cleanup 
    

    BEWARE: For database interactions, exec isn't idiomatic code.

    The SQL library allows direct interaction with the database: http://golang.org/pkg/database/sql/

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 想用@vueuse 把项目动态改成深色主题,localStorge里面的vueuse-color-scheme一开始就给我改成了dark,不知道什么原因(相关搜索:背景颜色)
  • ¥15 flask实现搜索框访问数据库
  • ¥15 mrk3399刷完安卓11后投屏调试只能显示一个设备
  • ¥20 白日门传奇少一个启动区服和启动服务器的快捷键,东西都是全的 , 他们说套一个出来就行了 但我就是弄不好,谁看看,
  • ¥100 如何用js写一个游戏云存档
  • ¥15 ansys fluent计算闪退
  • ¥15 有关wireshark抓包的问题
  • ¥15 需要写计算过程,不要写代码,求解答,数据都在图上
  • ¥15 向数据表用newid方式插入GUID问题
  • ¥15 multisim电路设计