大概是原 go 程式 (客戶的),exec.Command ("a_program").
然後我們是 embedded system,沒有 a_program,而是自己寫的 b_program。
go 的程式...
cmd := exec.Command("a_program") pipe, err := cmd.StdinPipe() err = cmd.Start() for k, v := range vars { _, err := fmt.Fprint(pipe, "%s%s%s\n",k," ", v) } pipe.Close() ..其中 vars 是 [string]string 的 map type
所以仿照,測試 go program:
package main import ( "fmt" "os" "os/exec" ) func main() { cmd := exec.Command("./ecc.sh") cmdin, err := cmd.StdinPipe() if err != nil { fmt.Println(err) } _, err = cmdin.Write([]byte("abc\n")) if err != nil { fmt.Println(err) } _, err = cmdin.Write([]byte("Def\n")) if err != nil { fmt.Println(err) } cmdin.Close() cmd.Stdout = os.Stdout cmd.Start() }然後 shell script (ecc.sh) 是...
#!/bin/bash while read LINE; do echo ${LINE} case ${LINE} in "abc") echo is ABC ;; "def") echo is DEF ;; *) echo unknown esac done echo AAA exit 0測試 結果:
$ go run tstdin.go abc is ABC Def unknown AAA所以.. stdin close 之後, shell script 會自己 exit while read
沒有留言:
張貼留言