How can i pipe arbitrary strings into a temporary file pipe for programs that read from a file?

What is the julia pipeline equivalent to bash’s process substitution?
https://unix.stackexchange.com/a/64011/43398

And/or can we use Pipe() to make a Named Pipe?
https://unix.stackexchange.com/questions/63923/pseudo-files-for-temporary-data

Or is there some other, more julian way to pipe arbitrary strings from julia into a process that expects a filename as an argument?

For example, the diff process expects its arguments to be filenames. But if i want to diff two strings, in bash I can use process substitution to do it:

$ diff --unified <(echo "hello 1") <(echo "hello 2")
--- /dev/fd/11  2020-04-16 08:59:01.000000000 -0400
+++ /dev/fd/12  2020-04-16 08:59:01.000000000 -0400
@@ -1 +1 @@
-hello 1
+hello 2

How could i call diff with temporary pipes built from strings in julia?
:slight_smile: Thanks!

1 Like

You can pipe arbitrary strings as stdin via pipeline:

julia> pipeline(`sort`, stdin=IOBuffer("cat\nbarn\napple\n"))
pipeline(`sort`, stdin<IOBuffer(data=UInt8[...], readable=true, writable=false, seekable=true, append=false, size=15, maxsize=Inf, ptr=1, mark=-1))

I don’t think there is a high-level analogue for process substitution for arbitrary files arguments, though. You could implement it based on the bash algorithm I guess.

1 Like

There really isn’t a generic way. Julia is not a shell prompt (and you seem to want Julia to match bash in it’s abilities) and worse Julia runs on Windows and Linux. So that makes it doubly hard to implement something generic.

If you are willing to tie yourself to Linux you could look at mkfifo either the command or the library call, call that twice to create two named pipes. Then you would need to spawn a 2 tasks to write to the files. Then you could pass both those named pipes to whatever command you want to run.

2 Likes

Thanks both for the answers! :slight_smile:

I ended up just creating a temporary file, writing to the temp file, and passing that, which is in a lot of ways quite similar to a temporary file pipe anyway.

    mktemp() do f, io
        out, _ = mktemp()
        write(io, str)
        close(io)
        run(`cobc -free -b -o $out $f`)
        return Libdl.dlopen(out)
    end