Passing STDIN into a script as one might a file

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.

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

1 Like

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.

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

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

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

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

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

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

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.