Appending to or Replacing Command Output

A redirection can append output after existing bytes or open an ordinary file for replacement, depending on the specified operator and shell.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Trace two runs
  2. Choose the report's contract
  3. Compare two runs with the same generated line
  4. Decide what incomplete output means

Trace two runs

If each run emits one line, append mode can keep both lines. Replacement mode can leave only the second output. The file open mode determines that retained state.

Choose the report's contract

A current summary normally needs one completed generation. A log normally retains several events. To avoid seeing an incomplete replacement, compare publishing after a complete write.[1]

Compare two runs with the same generated line

A command always prints one record X. If output is opened with truncation on each run, the destination ends with one X. If both runs append, it contains two. Choose based on whether the destination is a replacement snapshot or a history stream. See repeatable file exports.

Initial file: A
Truncating redirection then print X: X
Appending redirection then print X: A followed by X
Repeat append: a second X appears

Decide what incomplete output means

A failed truncating run can leave a partial replacement. A failed append can leave an incomplete record. Test recovery at the consumer boundary and stage generated snapshots before replacing them when required.

References

  1. The Open Group, POSIX Shell Command Language ↩