Warning: Work in progress! Leave feedback on Zulip or Github if you'd like this doc to be updated.

I/O Builtins in Oil

POSIX shell has overlapping and quirky constructs for doing I/O:

Oil rationalizes I/O with:

Oil also has orthogonal mechanisms for string processing:

These are discussed in more detail the strings doc.

Table of Contents
Problems With Shell
Summary of Oil features
write
Buffered vs. Unbuffered
Invariants
Problem
Array -> QSN Lines -> Array
File -> Array -> File
File -> String -> File
Related

Problems With Shell

Example:

hostname | read --all :x
write -- $x

Summary of Oil features

write

Buffered vs. Unbuffered

Invariants

Here are some design notes on making the I/O builtins orthogonal and composable. There should be clean ways to "round trip" data between the OS and Oil data structures.

Problem

# This will get messed up with newlines, and empty strings.
IFS=$'\n'
var lines1 = @(write -- @myarray)

# This will give you back an array
var lines2 = @(write --qsn -- @myarray)

Array -> QSN Lines -> Array

This is one way to make a copy of an array

write --qsn -- @myarray | read --lines --qsn :otherarray

In contrast, this doesn't work when the elements have newlines:

var myarray = %( 'bad\n' )
write -- @myarray | read --lines :otherarray

File -> Array -> File

cat input.txt | read --all-lines :myarray

# suppress the newline
write --sep '' --end '' -- @myarray > output.txt

diff input.txt output.txt  # should be equal

File -> String -> File

cat input.txt | read --all :x

# suppress the newline
write --end '' $x > output.txt

diff input.txt output.txt  # should be equal

Related


Generated on Sat Apr 30 21:44:54 EDT 2022