不同 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知识!
-
502 收藏
-
502 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
139 收藏
-
204 收藏
-
325 收藏
-
477 收藏
-
486 收藏
-
439 收藏
-
357 收藏
-
352 收藏
-
101 收藏
-
440 收藏
-
212 收藏
-
143 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 508次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习