#!/bin/sh set -e Die(){ e=$? trap - EXIT rm -f $TMP exit ${1:-$e} }; trap Die INT HUP EXIT usage(){ printf >&2 '%s\n' 'usage: Y- FILE [CMD...]'\ $'\033[31mWARNING!\033[0m overwrites FILE with [ nil | CMD ] f(Y(f)) # Y(Y)-> Y(Y(Y)) TMP=$(mktemp)||{ typeset e=$? echo >&2 "Y-: mktemp error." Die $e } f="$1"; shift 1 cat "$f">$TMP exec 3<$TMP #new pos:0 fd rm -f $TMP #see /proc/$$/fd/3 case "$Y" in ''|u) ;; *) echo >&2 'Y-: (unexpected $Y)' Die 1 ;; esac Y=3$Y Y "$@"<&3 >"$f" Die