登录
首页 >  Golang >  Go问答

不同 shell 命令对 go exec 的影响

来源:stackoverflow

时间:2024-02-28 23:51:24 231浏览 收藏

学习知识要善于思考,思考,再思考!今天golang学习网小编就给大家带来《不同 shell 命令对 go exec 的影响》,以下内容主要包含等知识点,如果你正在学习或准备学习Golang,就都不要错过本文啦~让我们一起来看看吧,能帮助到你就更好了!

问题内容

我尝试对控制台 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 的行为也是如此。

所以你的 cypher 代码直到你 stdin.close() 才开始执行。您的 cmd.stdout = os.stdout 示例似乎可以工作,因为当您的 go 程序退出时,stdin 会隐式关闭,并且只有然后 cypher-shell 才会执行您的代码并打印到 stdout,该 stdout 仍然连接到您的终端。

您可能应该以不同的方式构建您的流程。例如,您不能为每个查询运行一个新的 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 值。

好了,本文到此结束,带大家了解了《不同 shell 命令对 go exec 的影响》,希望本文对你有所帮助!关注golang学习网公众号,给大家分享更多Golang知识!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>