GO语言:与Shell进程通信

huangapple go评论125阅读模式
英文:

GO lang : Communicate with shell process

问题

你可以使用io.WriteString函数将输入写入到in中,然后通过in.Close()关闭输入流。这样,你就可以执行shell脚本并读取结果了。以下是一个示例代码:

  1. package main
  2. import (
  3. "fmt"
  4. "io"
  5. "os/exec"
  6. )
  7. func main() {
  8. cmd := exec.Command("python", "add.py")
  9. in, _ := cmd.StdinPipe()
  10. go func() {
  11. defer in.Close()
  12. io.WriteString(in, "your_input_here")
  13. }()
  14. out, _ := cmd.Output()
  15. fmt.Println(string(out))
  16. }

在上面的代码中,我们使用io.WriteString将输入写入到in中,并通过in.Close()关闭输入流。然后,我们使用cmd.Output()获取脚本的输出结果,并将其打印出来。你可以将"your_input_here"替换为你想要提供给脚本的输入。

英文:

I want to execute a shell script from Go.
The shell script takes standard input and echoes the result.

I want to supply this input from GO and use the result.

What I am doing is:

  1. cmd := exec.Command("python","add.py")
  2. in, _ := cmd.StdinPipe()

But how do I read from in?

答案1

得分: 7

以下是一段写入进程并从中读取的代码:

  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "os/exec"
  6. )
  7. func main() {
  8. // 要计算的内容
  9. calcs := make([]string, 2)
  10. calcs[0] = "3*3"
  11. calcs[1] = "6+6"
  12. // 存储结果
  13. results := make([]string, 2)
  14. cmd := exec.Command("/usr/bin/bc")
  15. in, err := cmd.StdinPipe()
  16. if err != nil {
  17. panic(err)
  18. }
  19. defer in.Close()
  20. out, err := cmd.StdoutPipe()
  21. if err != nil {
  22. panic(err)
  23. }
  24. defer out.Close()
  25. // 逐行读取
  26. bufOut := bufio.NewReader(out)
  27. // 启动进程
  28. if err = cmd.Start(); err != nil {
  29. panic(err)
  30. }
  31. // 将操作写入进程
  32. for _, calc := range calcs {
  33. _, err := in.Write([]byte(calc + "\n"))
  34. if err != nil {
  35. panic(err)
  36. }
  37. }
  38. // 从进程中读取结果
  39. for i := 0; i < len(results); i++ {
  40. result, _, err := bufOut.ReadLine()
  41. if err != nil {
  42. panic(err)
  43. }
  44. results[i] = string(result)
  45. }
  46. // 查看计算结果
  47. for _, result := range results {
  48. fmt.Println(result)
  49. }
  50. }

你可能希望在不同的 goroutine 中进行读写操作。

英文:

Here is some code writing to a process, and reading from it:

  1. package main
  2. import (
  3. &quot;bufio&quot;
  4. &quot;fmt&quot;
  5. &quot;os/exec&quot;
  6. )
  7. func main() {
  8. // What we want to calculate
  9. calcs := make([]string, 2)
  10. calcs[0] = &quot;3*3&quot;
  11. calcs[1] = &quot;6+6&quot;
  12. // To store the results
  13. results := make([]string, 2)
  14. cmd := exec.Command(&quot;/usr/bin/bc&quot;)
  15. in, err := cmd.StdinPipe()
  16. if err != nil {
  17. panic(err)
  18. }
  19. defer in.Close()
  20. out, err := cmd.StdoutPipe()
  21. if err != nil {
  22. panic(err)
  23. }
  24. defer out.Close()
  25. // We want to read line by line
  26. bufOut := bufio.NewReader(out)
  27. // Start the process
  28. if err = cmd.Start(); err != nil {
  29. panic(err)
  30. }
  31. // Write the operations to the process
  32. for _, calc := range calcs {
  33. _, err := in.Write([]byte(calc + &quot;\n&quot;))
  34. if err != nil {
  35. panic(err)
  36. }
  37. }
  38. // Read the results from the process
  39. for i := 0; i &lt; len(results); i++ {
  40. result, _, err := bufOut.ReadLine()
  41. if err != nil {
  42. panic(err)
  43. }
  44. results[i] = string(result)
  45. }
  46. // See what was calculated
  47. for _, result := range results {
  48. fmt.Println(result)
  49. }
  50. }

You might want to read/write from/to the process in different goroutines.

huangapple
  • 本文由 发表于 2014年3月7日 15:44:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/22244341.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定