# Communication with a command line program

**URL:** https://discourse.julialang.org/t/communication-with-a-command-line-program/4448
**Category:** General Usage
**Created:** [June 25, 2017, 4:25pm UTC](https://discourse.julialang.org/t/communication-with-a-command-line-program/4448 "2017-06-25T16:25:06Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![purplishrock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/purplishrock/32/13451_2.png) [@purplishrock](https://discourse.julialang.org/u/purplishrock)
#### Post date: [June 25, 2017, 4:25pm UTC](https://discourse.julialang.org/t/communication-with-a-command-line-program/4448/1 "2017-06-25T16:25:06Z")

</div>

Hi

I’m trying to set-up communication with an interactive program.  
I’ll use bc for my example.

I quickly found that this works:

```julia
julia> (p, process)=open(`bc`, "w", STDOUT)
(Pipe(open => closed, 0 bytes waiting),Process(`bc`, ProcessRunning))

julia> write(p, "2*3\n")
6
4

```

However, what I can’t figure out is what to substitute for STDOUT.

```julia
open(cmds::Base.AbstractCmd, mode::AbstractString, other::Union{Base.FileRedirect,IO,RawFD})

```

I thought the most obvious thing was to use an IOBuffer so that the process would simple write into the buffer and then of course I could read from it, but that is not type compatible.

```julia
julia> s=IOBuffer(100)
IOBuffer(data=UInt8[...], readable=true, writable=true, seekable=true, append=false, size=0, maxsize=100, ptr=1, mark=-1)

julia> (p, process)=open(`bc`, "w", s)
ERROR: MethodError: no method matching uvtype(::Base.AbstractIOBuffer{Array{UInt8,1}})
Closest candidates are:
  uvtype(::RawFD) at process.jl:158
  uvtype(::Base.DevNullStream) at process.jl:151
  uvtype(::Base.Filesystem.File) at filesystem.jl:70

```

ok, no problem, I just need to turn by IOBuffer into a File. And that’s where i get stuck.

Any hints ?

Thank you!

---

<div class="post-metadata">

### Author: ![anon94023334](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@anon94023334](https://discourse.julialang.org/u/anon94023334)
#### Post date: [June 25, 2017, 7:55pm UTC](https://discourse.julialang.org/t/communication-with-a-command-line-program/4448/3 "2017-06-25T19:55:30Z")

</div>

If your objective is to write to a file, just use a plain old file:

```julia
julia> f = open("/tmp/foo","w")
IOStream(<file /tmp/foo>)

julia> (p, process)=open(`bc`, "w", f)
(Pipe(RawFD(22) open => RawFD(-1) closed, 0 bytes waiting), Process(`bc`, ProcessRunning))

julia> write(p, "2*3\n")
4

julia> close(f)

phasebang:~$ cat /tmp/foo
6

```

---

<div class="post-metadata">

### Author: ![purplishrock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/purplishrock/32/13451_2.png) [@purplishrock](https://discourse.julialang.org/u/purplishrock)
#### Post date: [June 26, 2017, 5:25am UTC](https://discourse.julialang.org/t/communication-with-a-command-line-program/4448/4 "2017-06-26T05:25:56Z")

</div>

No, the objective is to communicate back and forth with the program. So I don’t want to just dump the output to a file- although your example is useful- but to keep the link open.

So what I want to do is open a string buffer or iobuffer as an iostream so i can continually read back from it.  
so in python this just uses the Popen function with stdin and stdout assigned to Subprocess.PIPE. I then get file objects I can read from and write to.

edit: i guess i could open a file as the output, leave it open, and then open another, read-only stream to that file to get output from it… That seems very kludgey.

---

<div class="post-metadata">

### Author: ![purplishrock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/purplishrock/32/13451_2.png) [@purplishrock](https://discourse.julialang.org/u/purplishrock)
#### Post date: [June 26, 2017, 5:34am UTC](https://discourse.julialang.org/t/communication-with-a-command-line-program/4448/5 "2017-06-26T05:34:02Z")

</div>

LOL.  
I knew it would be easy, and that Julia would have something built in…

```julia
julia> (sout, sin, p) = readandwrite(`bc`)
(Pipe(closed => open, 0 bytes waiting),Pipe(open => open, 0 bytes waiting),Process(`bc`, ProcessRunning))

julia> write(sin, "2*3\n")
4

julia> readline(sout)
"6\n"

julia> write(sin, "2*10\n")
5

julia> readline(sout)
"20\n"

julia> 

```
