curl
我的应用程序适用于从控制台( 、、、date
等等ping
)提供的所有类型的 shell 命令。现在我想用交互式 shell 命令(如 mongo shell)来覆盖这个案例,使用os/exec
.
例如,作为第一步,连接到 mongodb:
mongo --quiet --host=localhost blog
然后执行任意数量的命令,每一步都得到结果
db.getCollection('posts').find({status:'INACTIVE'})
进而
exit
我尝试了以下方法,但它允许我每个 mongo 连接只执行一个命令:
func main() {
cmd := exec.Command("sh", "-c", "mongo --quiet --host=localhost blog")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
stdin, _ := cmd.StdinPipe()
go func() {
defer stdin.Close()
io.WriteString(stdin, "db.getCollection('posts').find({status:'INACTIVE'}).itcount()")
// fails, if I'll do one more here
}()
cmd.Run()
cmd.Wait()
}
有没有办法运行多个命令,获得每个执行命令的标准输出结果?