# How to tell if \`stdin\` buffer is not empty

**URL:** <https://discourse.julialang.org/t/how-to-tell-if-stdin-buffer-is-not-empty/60514>\
**Category:** General Usage\
**Created:** [May 4, 2021, 12:00pm UTC](https://discourse.julialang.org/t/how-to-tell-if-stdin-buffer-is-not-empty/60514 "2021-05-04T12:00:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jakewilliami](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jakewilliami/32/18945_2.png) [@jakewilliami](https://discourse.julialang.org/u/jakewilliami)\
**Post date:** [May 4, 2021, 12:00pm UTC](https://discourse.julialang.org/t/how-to-tell-if-stdin-buffer-is-not-empty/60514/1 "2021-05-04T12:00:10Z")

</div>

I thought that `bytesavailable` would solve this issue (based on [this](https://stackoverflow.com/a/60956065/12069968)), but I will demonstrate that it doesn’t work.  
Say I have the following script:

```julia
println(bytesavailable(stdin))

```

Now I check how many bytes there are in a dummy file:

```bash
$ wc -c foo.txt
      21 foo.txt

```

So piping this file into the Julia script show display 21, I thought—but it displays zero:

```bash
$ cat foo.txt | julia test.jl
0

```

Does anyone know how to detect if anything has been passed to the script via `stdin`? The goal is to have something like this:

```julia
bytesavailable(stdin) > 0 ? println(read(stdin, String)) : println("No stdin given")

```

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [May 4, 2021, 1:24pm UTC](https://discourse.julialang.org/t/how-to-tell-if-stdin-buffer-is-not-empty/60514/2 "2021-05-04T13:24:36Z")

</div>

how about

```julia
s = readchomp(stdin) # chomp to remove trailing \n
isempty(s) ? println("No stdin given") : println(s)

```

?

---

<div class="post-metadata">

**Author:** ![DaveMacMahon](https://avatars.discourse-cdn.com/v4/letter/d/9f8e36/32.png) [@DaveMacMahon](https://discourse.julialang.org/u/DaveMacMahon)\
**Post date:** [May 4, 2021, 4:41pm UTC](https://discourse.julialang.org/t/how-to-tell-if-stdin-buffer-is-not-empty/60514/3 "2021-05-04T16:41:21Z")

</div>

`read_chomp(stdin)` will block waiting for input if none is being piped in.

---

<div class="post-metadata">

**Author:** ![DaveMacMahon](https://avatars.discourse-cdn.com/v4/letter/d/9f8e36/32.png) [@DaveMacMahon](https://discourse.julialang.org/u/DaveMacMahon)\
**Post date:** [May 4, 2021, 4:42pm UTC](https://discourse.julialang.org/t/how-to-tell-if-stdin-buffer-is-not-empty/60514/4 "2021-05-04T16:42:44Z")

</div>

Cross-pasting from Slack…

It does seem like there should be an easier (and/or more readily apparent) way to do this. Part of the complexity seems to be that the type of `stdin` is very dependent on how julia is started:

```julia
$ cat test.jl
using InteractiveUtils
println(supertypes(typeof(stdin)))

$ julia test.jl
(Base.TTY, Base.LibuvStream, IO, Any)

$ cat /dev/null | julia test.jl
(Base.PipeEndpoint, Base.LibuvStream, IO, Any)

$ julia test.jl < /dev/null
(IOStream, IO, Any)

```

For the `IOStream` case, you could use `poll_fd` from the `FileWatching` package, but it doesn’t seem to work with `Base.LibuvStream` types (at least not on 1.6.0 for me).
