# Passing STDIN into a script as one might a file

**URL:** <https://discourse.julialang.org/t/passing-stdin-into-a-script-as-one-might-a-file/12921>\
**Category:** General Usage\
**Created:** [August 5, 2018, 11:52pm UTC](https://discourse.julialang.org/t/passing-stdin-into-a-script-as-one-might-a-file/12921 "2018-08-05T23:52:23Z")\
**Posts on this page:** 7\
**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:** [August 5, 2018, 11:52pm UTC](https://discourse.julialang.org/t/passing-stdin-into-a-script-as-one-might-a-file/12921/1 "2018-08-05T23:52:23Z")

</div>

I know that when you run “./script.jl foo bar” String[“foo”, “bar”] is passed into the script as a one-dimentional string array. However, this is not what I’m looking for.

I’m looking to process a large text file(up to 1MB), and need STDIN to be exposed in a way that is more like a file. I tried `open(STDIN)`, however this, as I expected, isn’t proper usage (throws errors).

I’ve written a set of functions which together apply a Git filter for LaTeX documents, however at the time I started writing I didn’t know how I would be passing STDIN into a script so I merely had them read and write to files. Now that the cleaning filter is done, I figure I should make it actually work with Git instead of by opening files and writing to them.

I’m sorry if I’m sound vague. I’ve had over a week to think about this, but the fact of the matter is I’m not use to passing stuff into scripts. Normally I’m reading them from files. I read the documentation on both Strings and Streams, but have no idea how to proceed.

An example of something that Git would accept would be ` ruby -e '$stdout.puts $stdin.read.gsub(/ /, "\t")'` in a bash script.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [August 5, 2018, 11:53pm UTC](https://discourse.julialang.org/t/passing-stdin-into-a-script-as-one-might-a-file/12921/2 "2018-08-05T23:53:36Z")

</div>

STDIN is a file handle that is already opened, not a file name that you can open. You can just read from it.

---

<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:** [August 6, 2018, 5:29am UTC](https://discourse.julialang.org/t/passing-stdin-into-a-script-as-one-might-a-file/12921/3 "2018-08-06T05:29:05Z")

</div>

Indeed. I didn’t think it would work, but I tried anyway. The problem is in the expected usage, data is going to be presented as STDIN, therefore I need the Julia script to accept it. That is just how Git does things, and not something I can control. I also, for what its worth, have tried piping `cat file` to the script.

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [August 6, 2018, 6:13am UTC](https://discourse.julialang.org/t/passing-stdin-into-a-script-as-one-might-a-file/12921/4 "2018-08-06T06:13:22Z")

</div>

Julia can certainly do that. You just need to read from `STDIN` as Stefan suggested. For example:

```nohighlight
$ echo "hello world" | julia -e 'println("received: ", readline(STDIN), " on STDIN")'
received: hello world on STDIN

```

---

<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:** [August 6, 2018, 12:28pm UTC](https://discourse.julialang.org/t/passing-stdin-into-a-script-as-one-might-a-file/12921/5 "2018-08-06T12:28:33Z")

</div>

Oh, I see what he was suggesting now. I’m sorry. I miss understood. Thanks guys.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [August 6, 2018, 1:49pm UTC](https://discourse.julialang.org/t/passing-stdin-into-a-script-as-one-might-a-file/12921/6 "2018-08-06T13:49:40Z")

</div>

If you need to use STDIN as a file, many UNIX systems support reading from `/dev/fd/0`:

```julia
$ echo "Hello, world" | julia -E 'readchomp("/dev/fd/0")'
"Hello, world"

```

However, this is a feature of the operating system and not of Julia.

---

<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:** [August 6, 2018, 2:00pm UTC](https://discourse.julialang.org/t/passing-stdin-into-a-script-as-one-might-a-file/12921/7 "2018-08-06T14:00:04Z")

</div>

No, you’re original post is what I needed. It was late, I was frustrated, and before bed I posted the question instead of waiting. As a result, I misunderstood what you were suggesting.

The problem I was having was that I was assuming that I needed to. . .“tell” Julia I was going to be reading from STDIN. You were saying that was unnecessary. When I remove the “open” command, everything works as desired.

I missed the but about STDIN already being open.
