#!/bin/sh set -eu die() { echo "$@" 1>&2 exit 1 } recv_hole() { local n read n truncate --size "+$n" "$1" } recv_data() { local n read n dd of="$1" conv=notrunc oflag=append bs=1 count="$n" status=noxfer 2>&1 | grep -Ev ' records (in|out)$' || true } output="$1" touch "$output" while read what do case "$what" in DATA) recv_data "$output" ;; HOLE) recv_hole "$output" ;; *) die "Unknown instruction: $what" ;; esac done