I/O Redirection¶
Idio defines some reader operators for I/O redirection, the
familiar >
etc.. Remember that, in Idio, whitespace is
important as >
can be part of a valid symbol.
These are implemented by changing the current input, output or error handle from or to some entity. When we reach the point of trying to invoke an external command the current input, output and error handles will be transformed into suitable file descriptors.
Note
Whilst the operators are limited to manipulating stdin, stdout
and stderr you can effect any other changes with calls to
libc/dup2
etc. directly.
Standard Redirection¶
< expr
> expr
>> expr
2> expr
2>> expr
Here, expr
can evaluate to:
a handle (fd or string)
a string to be opened for input or output
#n
as a shorthand for the string"/dev/null"
fn := "foo.txt"
;; overwrite a file
echo "hello" > fn
fh := open-file fn "a"
;; append via a file handle
echo "world" >> fh
close-handle fh
cat fn
rm fn
$ idio standard-io
hello
world
Using Handles¶
Using the redirection operators on a handle does not implicitly rewind the handle. All operators are effectively continuing from the existing file/string position.
I/O Duplication¶
<& expr
>& expr
2>& expr
Here, whatever expr
evaluates to must already exist, so:
a handle (fd or string)
a C
int
(or fixnum) representing the file descriptor to calllibc/dup2
with
fn := "foo.txt"
fh := open-output-file fn
hprintf fh "hello\n"
;; append via a file handle
echo "world" >& fh
close-handle fh
cat fn
rm fn
$ idio io-dup
hello
world
Last built at 2024-12-21T07:11:31Z+0000 from 77077af (dev) for Idio 0.3