Usually shell scripts are not callable from language execs.  Instead of 
running "./child.sh", you should probably run "/bin/bash", "./child.sh".  

On Thursday, August 29, 2013 at 1:33:57 PM UTC-7, Daryl Williams wrote:
>
> Hi Kyle, Thanks for your reply, and your right, I was just using bash as 
> an example. What I am using to try and learn is a simple bash script that 
> is run by my Go program.
>
> *Here is the bash script:*
>
> #!/bin/bash
>
> read line
> echo "child says: Who's there?"
> read line
> echo "child says: Canoe who?"
> read line
> echo "child says: Groan..."
>
> *And here is the Go code:* (exitOnErrorFunction not shown)
>
> cmd := exec.Command("./child.sh")
>
> cmdStdin, err := cmd.StdinPipe()
> exitOnError("cmd.StdinPipe()", err)
>
> cmdStdout, err := cmd.StdoutPipe()
> exitOnError("cmd.StdoutPipe()", err)
>
> _, err := cmdStdin.Write([]byte("Knock Knock.\n"))
> exitOnError("cmdStdin.Write()", err)
> //cmdStdin.Close()
>
> bytes, err := ioutil.ReadAll(cmdStdout)
> exitOnError("ioutil.ReadAll", err)
>
> _, err = cmdStdin.Write([]byte("Canoe.\n"))
> exitOnError("cmdStdin.Write()", err)
>
> bytes, err = ioutil.ReadAll(cmdStdout)
> exitOnError("ioutil.ReadAll", err)
>
> _, err = cmdStdin.Write([]byte("Canoe help me figure this out?\n"))
> exitOnError("cmdStdin.Write()", err)
>
> bytes, err := ioutil.ReadAll(cmdStdout)
> exitOnError("ioutil.ReadAll", err)
>
> ---
>
> The above code hangs because the child appears to be blocking on the read. 
> If I un-comment the cmdStdin.Close() then command does not hang, but the 
> parent is unable to send any further data to the child.
>
> If can provide any further information, please let me know.
>
> Thanks,
>
> Daryl
>
>
>
> On Thursday, August 29, 2013 11:54:40 AM UTC-7, Daryl Williams wrote:
>>
>> Hi Folks,
>>
>> I am still trying to get a grip on the correct usage of Cmd.StdinPipe. I 
>> have looked at several examples, which work well as far as they go, but I 
>> haven't been able to find any examples of multiple writes to Cmd.StdinPipe. 
>> Perhaps if I explain conceptually what I am trying to do, some of the more 
>> experienced hands here can put me onto the right track.
>>
>> Basically I need to exec a new process and be able to have a back and 
>> forth exchange, presumably over stdin and stdout. For example, I'd like to 
>> start up a bash shell and have it run /bin/ls, get that output and then run 
>> /usr/bin/grep and write it the data to be searched and then read the output 
>> of that command.
>>
>> So to keep it simple, can anyone tell me if this is possible? The 
>> examples I've seen only work if you close the Cmd.StdinPipe after writing 
>> to it, otherwise the child process seems to block on the read.
>>
>> Any help would be greatly appreciated.
>>
>> Thanks,
>>
>> Daryl
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to