go - go exec 对不同 shell 命令的不同行为

标签 go cmd interactive-shell

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

有什么区别?我怎样才能让第二个工作? (不关闭命令)

附言 当我直接打印到 os.Stdout 时,Neo4J 工作正常:

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)

最佳答案

cypher-shell 的输入不是(交互式)终端时,它期望读取整个输入并将其执行为一个脚本。 “整个输入”是指“EOF 之前的所有内容”。这是典型的 REPL 程序:例如,python行为也是如此。

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

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

但是,如果所有其他方法都失败了,您可以通过欺骗 cypher-shell 认为它的标准输入 is a terminal 来解决这个问题.这称为“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 来避免额外的 shell 进程的开销:

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

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

关于go - go exec 对不同 shell 命令的不同行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55962490/

相关文章:

go - 有什么方法可以摆脱 GORM 中的自动列蛇案例重命名?

go - 绕过 Go 中的 sql 空值问题

html - 如何在 Atom 中语法高亮 go 模板文件?

python - 如何在 python 中执行批处理文件后关闭命令提示符窗口?

go - 接受所有数字类型(int、float、..)并将它们相加的函数

windows-7 - 批量/CMD move 不适用于文件夹

image - 在命令提示符下使用 imagej 打开原始文件

linux - 在 scala shell 上使用向上箭头进行历史/部分搜索

bash - 如何将位置参数传递给交互式 bash session

Python 子进程 : interacting with a shell script