如何将 exec.Command 的输出通过管道传输到 Golang 中的另一个命令
how to pipe the output from exec.Command into another commands in Golang
我有八个 Microsoft 访问数据库,每个数据库大约有 215 tables,我需要将这些数据库传输到 postgresql,所以我使用 mdb-tools 并导出方案,这只是一步;但是当涉及到将 tables 数据直接导出到 postgres 到 postgresql 时,我必须为每个 table:
编写这个命令
mdb-export -I postgres -q \' myaccessdatabase.mdb table-name | psql -d mypsqldatabase -U postgres -w -h localhost
所以我一直在尝试编写一个 go 命令程序来执行以下操作:
1. 首先执行命令列出table的名字。这将是下一个命令的参数。
2.然后开始for range循环执行一个导出tanle数据的命令,这个命令的输出是管道到下一个命令。
3. 这个命令是 psql 它将写入前一个命令的输出(即 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")
}
}
所以我尝试将输出通过管道传输到下一个命令的标准输入中,但无法实现它,同时我正在尝试使用 goroutin,而通道甚至无法提供将其变成最后一个命令的方法命令。
非常感谢您。
exec.Command
函数只创建命令,不执行命令。
要从 tablesname := exec.Command("mdb-tables", "-1", "myaccessdatabase.mdb")
获取输出,您需要 运行 命令并捕获其输出:
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
注意:对于数据库交互,exec
不是惯用代码。
SQL 库允许与数据库直接交互:http://golang.org/pkg/database/sql/
我有八个 Microsoft 访问数据库,每个数据库大约有 215 tables,我需要将这些数据库传输到 postgresql,所以我使用 mdb-tools 并导出方案,这只是一步;但是当涉及到将 tables 数据直接导出到 postgres 到 postgresql 时,我必须为每个 table:
编写这个命令mdb-export -I postgres -q \' myaccessdatabase.mdb table-name | psql -d mypsqldatabase -U postgres -w -h localhost
所以我一直在尝试编写一个 go 命令程序来执行以下操作: 1. 首先执行命令列出table的名字。这将是下一个命令的参数。 2.然后开始for range循环执行一个导出tanle数据的命令,这个命令的输出是管道到下一个命令。 3. 这个命令是 psql 它将写入前一个命令的输出(即 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")
}
}
所以我尝试将输出通过管道传输到下一个命令的标准输入中,但无法实现它,同时我正在尝试使用 goroutin,而通道甚至无法提供将其变成最后一个命令的方法命令。
非常感谢您。
exec.Command
函数只创建命令,不执行命令。
要从 tablesname := exec.Command("mdb-tables", "-1", "myaccessdatabase.mdb")
获取输出,您需要 运行 命令并捕获其输出:
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
注意:对于数据库交互,exec
不是惯用代码。
SQL 库允许与数据库直接交互:http://golang.org/pkg/database/sql/