Piping To/From Commands¶
It is often useful to be able to either write data to an external command or read data from an external command. For this there are four pipe meta-commands. All create asynchronous commands.
pipe-from, will return a pipe handle.
This is another variation on an fd handle. As you might expect, you
cannot seek on a pipe handle because you can’t seek on the underlying
pipe file descriptor.
The other two,
return the name of a file that can be opened for reading or writing
respectively. This is the equivalent of Bash’s Process
>(...). Most operating systems will
/dev/fd/n-style of filename but some will use real
oph := pipe-into nslookup printf "oph is %s\n" oph hprintf oph "google.com\n" close-handle oph ;; async command last in script (wait)
$ idio pipe-into oph is #<H opw! 5:"/dev/fd/5":1:0> Server: 127.0.0.53 Address: 127.0.0.53#53 Non-authoritative answer: Name: google.com Address: 184.108.40.206 Name: google.com Address: 2a00:1450:4009:80b::200e
These examples have the asynchronous command at the end of the
script meaning Idio is likely to kill it as it exits
before the asynchronous command prints anything. We need a call to
wait to ensure it gets a chance to run to completion.
opn := named-pipe-into nslookup printf "opn is %s\n" opn oph := open-output-file opn hprintf oph "google.com\n" close-handle oph (wait)
$ idio named-pipe-into opn is /dev/fd/5 Server: 127.0.0.53 Address: 127.0.0.53#53 Non-authoritative answer: Name: google.com Address: 220.127.116.11 Name: google.com Address: 2a00:1450:4009:80b::200e
Last built at 2023-11-09T07:11:44Z+0000 from 77077af (dev) for Idio 0.3