# Control flow of shell command Pipe()

**URL:** <https://discourse.julialang.org/t/control-flow-of-shell-command-pipe/48915>\
**Category:** General Usage\
**Created:** [October 23, 2020, 10:26pm UTC](https://discourse.julialang.org/t/control-flow-of-shell-command-pipe/48915 "2020-10-23T22:26:48Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Donut\_Meepo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/donut_meepo/32/9085_2.png) [@Donut\_Meepo](https://discourse.julialang.org/u/Donut_Meepo)\
**Post date:** [October 23, 2020, 10:26pm UTC](https://discourse.julialang.org/t/control-flow-of-shell-command-pipe/48915/1 "2020-10-23T22:26:48Z")

</div>

Hi, I am trying to process a large amount of shell command output that I want to do it line by line. The following code is what I have right now. The while loop will not terminate. I could do that by checking `process_running(p)`. But I think then the last piece of content in the Pipe() needs to be taken care outside the while loop? Are there better way to do the job? Thank you for helping.

```nohighlight
p = Pipe()
proc = run(pipeline(cmd, stderr=p), wait=false)
# filter out lines start with Warning
while !eof(p) #does not stop
    line = readline(p) |> String
    if ~startswith(line, "Warning")
        println(line)
    end
end
close(p.in)
close(p)
```

---

<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:** [October 23, 2020, 11:24pm UTC](https://discourse.julialang.org/t/control-flow-of-shell-command-pipe/48915/2 "2020-10-23T23:24:46Z")

</div>

You need to connect the pipe to the process being executed. Assuming you want to read the process’ `stderr`:

```julia
p = Pipe()
proc = run(pipeline(cmd, stderr = p), wait=false)
close(p.in)
Base.start_reading(p.out)
while ....

```

---

<div class="post-metadata">

**Author:** ![Donut\_Meepo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/donut_meepo/32/9085_2.png) [@Donut\_Meepo](https://discourse.julialang.org/u/Donut_Meepo)\
**Post date:** [October 23, 2020, 11:37pm UTC](https://discourse.julialang.org/t/control-flow-of-shell-command-pipe/48915/3 "2020-10-23T23:37:12Z")

</div>

Thank you. Sorry I forgot to type `=p`. I corrected the post question. Your code works! Could you explain a bit what `close(p.in)` do? Will that block proc to put content into p?

---

<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:** [October 23, 2020, 11:47pm UTC](https://discourse.julialang.org/t/control-flow-of-shell-command-pipe/48915/4 "2020-10-23T23:47:16Z")

</div>

I’m not sure why it’s needed, since it’s not documented. I just picked up this knowledge along the way. See [https://github.com/JuliaLang/julia/issues/11824](https://github.com/JuliaLang/julia/issues/11824) and [Improve documentation for process interaction using pipes · Issue #24810 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/24810).

---

<div class="post-metadata">

**Author:** ![Donut\_Meepo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/donut_meepo/32/9085_2.png) [@Donut\_Meepo](https://discourse.julialang.org/u/Donut_Meepo)\
**Post date:** [October 23, 2020, 11:52pm UTC](https://discourse.julialang.org/t/control-flow-of-shell-command-pipe/48915/5 "2020-10-23T23:52:17Z")

</div>

Thank you very much. Now it is able to evaluate eof(p). Do you happen to know that if the Pipe p reaches `eof`, does it also mean the proc is ended? So I don’t have to do `wait(proc)`? (In my case, process\_exited(proc) returns true)

---

<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:** [October 24, 2020, 3:04pm UTC](https://discourse.julialang.org/t/control-flow-of-shell-command-pipe/48915/6 "2020-10-24T15:04:39Z")

</div>

I’m not sure, but I think `process_exited()` is the most reliable way to find if the process is running or not.
