GO语言:与Shell进程通信

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

GO lang : Communicate with shell process

问题

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

package main

import (
	"fmt"
	"io"
	"os/exec"
)

func main() {
	cmd := exec.Command("python", "add.py")
	in, _ := cmd.StdinPipe()

	go func() {
		defer in.Close()
		io.WriteString(in, "your_input_here")
	}()

	out, _ := cmd.Output()
	fmt.Println(string(out))
}

在上面的代码中,我们使用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:

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

But how do I read from in?

答案1

得分: 7

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

package main

import (
    "bufio"
    "fmt"
    "os/exec"
)

func main() {
    // 要计算的内容
    calcs := make([]string, 2)
    calcs[0] = "3*3"
    calcs[1] = "6+6"

    // 存储结果
    results := make([]string, 2)

    cmd := exec.Command("/usr/bin/bc")

    in, err := cmd.StdinPipe()
    if err != nil {
        panic(err)
    }

    defer in.Close()

    out, err := cmd.StdoutPipe()
    if err != nil {
        panic(err)
    }

    defer out.Close()

    // 逐行读取
    bufOut := bufio.NewReader(out)

    // 启动进程
    if err = cmd.Start(); err != nil {
        panic(err)
    }

    // 将操作写入进程
    for _, calc := range calcs {
        _, err := in.Write([]byte(calc + "\n"))
        if err != nil {
            panic(err)
        }
    }

    // 从进程中读取结果
    for i := 0; i < len(results); i++ {
        result, _, err := bufOut.ReadLine()
        if err != nil {
            panic(err)
        }
        results[i] = string(result)
    }

    // 查看计算结果
    for _, result := range results {
        fmt.Println(result)
    }
}

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

英文:

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

package main
import (
&quot;bufio&quot;
&quot;fmt&quot;
&quot;os/exec&quot;
)
func main() {
// What we want to calculate
calcs := make([]string, 2)
calcs[0] = &quot;3*3&quot;
calcs[1] = &quot;6+6&quot;
// To store the results
results := make([]string, 2)
cmd := exec.Command(&quot;/usr/bin/bc&quot;)
in, err := cmd.StdinPipe()
if err != nil {
panic(err)
}
defer in.Close()
out, err := cmd.StdoutPipe()
if err != nil {
panic(err)
}
defer out.Close()
// We want to read line by line
bufOut := bufio.NewReader(out)
// Start the process
if err = cmd.Start(); err != nil {
panic(err)
}
// Write the operations to the process
for _, calc := range calcs {
_, err := in.Write([]byte(calc + &quot;\n&quot;))
if err != nil {
panic(err)
}
}
// Read the results from the process
for i := 0; i &lt; len(results); i++ {
result, _, err := bufOut.ReadLine()
if err != nil {
panic(err)
}
results[i] = string(result)
}
// See what was calculated
for _, result := range results {
fmt.Println(result)
}
}

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:

确定