2

我正在尝试对控制台 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)
4

1 回答 1

1

当输入cypher-shell不是(交互式)终端,它希望读取整个输入并将其作为单个脚本执行。“整个输入”是指“直到 EOF 之前的所有内容”。这对于 REPL 程序来说很典型:例如,它的python行为也是如此。

因此,您的 Cypher 代码甚至不会开始执行,直到您stdin.Close(). 您的cmd.Stdout = os.Stdout示例似乎有效,因为stdin当您的 Go 程序退出时隐式关闭,然后才执行cypher-shell的代码并打印到仍然连接到您的终端的标准输出。

您可能应该以不同的方式构建您的流程。例如,您不能cypher-shell为每个查询运行一个新的吗?

但是,如果所有其他方法都失败了,您可以通过误cypher-shell以为它的 stdin是一个终端来解决这个问题。这称为“pty”,您可以在 Go 中使用github.com/kr/pty. 问题是,这也会cypher-shell提示打印并回显您的输入,如果您希望以编程方式处理输出,则必须检测并丢弃这些输入。

cmd := exec.Command("sh", "-c", "cypher-shell -u neo4j -p 121314 --format plain")
f, _ := pty.Start(cmd)
stdoutScanner := bufio.NewScanner(f)
cmd.Start()

// Give it some time to start, then read and discard the startup banner.
time.Sleep(2 * time.Second)
f.Read(make([]byte, 4096))

go func() {
    for stdoutScanner.Scan() {
        println(stdoutScanner.Text())
    }
}()

io.WriteString(f, "match (n) return count(n);\n")
time.Sleep(2 * time.Second)

io.WriteString(f, "match (n) return count(n) + 123;\n")
time.Sleep(2 * time.Second)

旁白1:在您的示例中,您不需要sh -c,因为您没有使用外壳的任何功能。您可以通过直接运行来避免额外的 shell 进程的开销cypher-shell

cmd := exec.Command("cypher-shell", "-u", "neo4j", "-p", "121314", "--format", "plain")

旁白 2:不要丢弃error生产代码中的返回值。

于 2019-05-03T20:51:12.713 回答