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

YSH I/O Builtins

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

YSH rationalizes I/O with:

YSH also has orthogonal mechanisms for string processing:

These are discussed in more detail the strings doc.

Table of Contents
Problems With Shell
Summary of YSH 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 YSH 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 YSH 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 Wed, 06 Dec 2023 01:04:01 -0500