# Piping Julia function output to external command

**URL:** <https://discourse.julialang.org/t/piping-julia-function-output-to-external-command/23756>\
**Category:** New to Julia\
**Created:** [May 1, 2019, 9:56pm UTC](https://discourse.julialang.org/t/piping-julia-function-output-to-external-command/23756 "2019-05-01T21:56:26Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![nstgc](https://avatars.discourse-cdn.com/v4/letter/n/e0b2c6/32.png) [@nstgc](https://discourse.julialang.org/u/nstgc)\
**Post date:** [May 1, 2019, 9:56pm UTC](https://discourse.julialang.org/t/piping-julia-function-output-to-external-command/23756/1 "2019-05-01T21:56:26Z")

</div>

I’ve found a wealth of information on how to pipe one external command to another, but I’m having trouble with piping the output of julia functions to external commands.

As a side note, I’m not sure how I’m suppose to format this post since back ticks are used for inline code on the forums and denote Cmd types in Julia. Therefore in my sample code I’m using single quotes. Any time you see single quotes in this post, I mean backticks

For example, `run(pipeline('ls','cat'))` works as I would expect: it prints the contents of the current directory to stdout.

Now, how would I do this with `readdir()` instead, I have no clue.

I’ve tried a few things without effect (other than errors) and most of the informaiton availible on line is terribly outdates ( ie `run("Hello World" |> 'echo')` absolutely doesn’t work).

> run(pipeline(readdir(),‘cat’)  
> run(readdri() |\> ‘cat’)  
> readdir() |\> run(‘cat’)

I’ve been working on this for days now, but most of what I’ve read is out dated and I’m out of ideas.

Also, I have seen `Pipe()`, however this seems to only be useful for capturing output.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [May 1, 2019, 10:29pm UTC](https://discourse.julialang.org/t/piping-julia-function-output-to-external-command/23756/2 "2019-05-01T22:29:23Z")

</div>

> [@nstgc](#):
>
> I’ve found a wealth of information on how to pipe one external command to another, but I’m having trouble with piping the output of julia functions to external commands.

You can simply `open` a command to write to, and write whatever you want to it as if the pipe were a file. For example:

```julia
open(`cat`, "w", stdout) do f
    for s in readdir()
        println(f, s)
    end
end

```

(This is [explained in the manual](https://docs.julialang.org/en/v1.1/manual/running-external-programs/).)

---

<div class="post-metadata">

**Author:** ![mbaz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbaz/32/17295_2.png) [@mbaz](https://discourse.julialang.org/u/mbaz)\
**Post date:** [May 2, 2019, 2:39am UTC](https://discourse.julialang.org/t/piping-julia-function-output-to-external-command/23756/3 "2019-05-02T02:39:10Z")

</div>

If you want to send data to a long-running process, you can see the way I do it in Gaston.jl here: [https://github.com/mbaz/Gaston.jl/blob/master/src/gaston\_init.jl](https://github.com/mbaz/Gaston.jl/blob/master/src/gaston_init.jl) This code starts gnuplot in the background, and communication happens through the three pipes that connect to gnuplot’s STDIN, STDOUT and STDERR.

To send data to the process, you can do something like `println(gstdin, data)`.

---

<div class="post-metadata">

**Author:** ![johnh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnh/32/3615_2.png) [@johnh](https://discourse.julialang.org/u/johnh)\
**Post date:** [May 2, 2019, 10:30am UTC](https://discourse.julialang.org/t/piping-julia-function-output-to-external-command/23756/4 "2019-05-02T10:30:11Z")

</div>

Hello @mbaz Going slightly off topic, I have a small project in mind. This is to use the Raspberry Pi Sensor Hat, which has temperature, pressure, humidity sensors etc.  
For a demonstration I thought of plotting graphs of sensor data which update every N seconds.  
I guess your method of piping to STDIN would be useful for this?  
Your thoughts are welcomed please.

---

<div class="post-metadata">

**Author:** ![mbaz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbaz/32/17295_2.png) [@mbaz](https://discourse.julialang.org/u/mbaz)\
**Post date:** [May 2, 2019, 1:52pm UTC](https://discourse.julialang.org/t/piping-julia-function-output-to-external-command/23756/5 "2019-05-02T13:52:14Z")

</div>

It could be useful, but I’m unsure what you’re planning:

- If the sensor hat outputs data to its STDOUT, then you can connect to it from your Julia program and read it from there. However, if the sensor software provides an API, I’d recommend using it instead.
- For plotting the data, you’re better off using a plotting package; on a small computer like the Pi, I’d recommend a simple plotting package like Gaston.

---

<div class="post-metadata">

**Author:** ![swt30](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/swt30/32/4667_2.png) [@swt30](https://discourse.julialang.org/u/swt30)\
**Post date:** [May 7, 2019, 9:36am UTC](https://discourse.julialang.org/t/piping-julia-function-output-to-external-command/23756/6 "2019-05-07T09:36:06Z")

</div>

In case it’s helpful, you can format backticks in quoted code by putting a larger number of backticks around the code. For example:

```nohighlight
``run(pipeline(`ls`, `cat`))``

```

formats inline as `run(pipeline(`ls`, `cat`))`.

I typed the above code block as

````nohighlight
```
``run(pipeline(`ls`, `cat`))``
```

````

and I typed the above code block as

`````nohighlight
````
```
``run(pipeline(`ls`,`cat`))``
```
````

`````

and… you get the idea!
