我正在尝试对控制台 go 应用程序使用不同的 shell 命令,并且由于某种原因,以下交互式 shell 的行为不同。
此代码打印 mongoDB 查询的结果:
cmd := exec.Command("sh", "-c", "mongo --quiet --host=localhost blog")
stdout, _ := cmd.StdoutPipe()
stdin, _ := cmd.StdinPipe()
stdoutScanner := bufio.NewScanner(stdout)
go func() {
for stdoutScanner.Scan() {
println(stdoutScanner.Text())
}
}()
cmd.Start()
io.WriteString(stdin, "db.getCollection('posts').find({status:'ACTIVE'}).itcount()\n")
//can't finish command, need to reuse it for other queries
//stdin.Close()
//cmd.Wait()
time.Sleep(2 * time.Second)
但是 Neo4J shell 的相同代码不会打印任何内容:
cmd := exec.Command("sh", "-c", "cypher-shell -u neo4j -p 121314 --format plain")
stdout, _ := cmd.StdoutPipe()
stdin, _ := cmd.StdinPipe()
stdoutScanner := bufio.NewScanner(stdout)
go func() {
for stdoutScanner.Scan() {
println(stdoutScanner.Text())
}
}()
cmd.Start()
io.WriteString(stdin, "match (n) return count(n);\n")
//can't finish the command, need to reuse it for other queries
//stdin.Close()
//cmd.Wait()
time.Sleep(2 * time.Second)
有什么不同?我怎样才能使第二个工作?(不关闭命令)
当我直接打印到以下位置时,PS Neo4J 工作正常os.Stdout
:
cmd := exec.Command("sh", "-c", "cypher-shell -u neo4j -p 121314 --format plain")
cmd.Stdout = os.Stdout
stdin, _ := cmd.StdinPipe()
cmd.Start()
io.WriteString(stdin, "match (n) return count(n);\n")
//stdin.Close()
//cmd.Wait()
time.Sleep(2 * time.Second)