# overwrite: replace file with command output. (DEE 2015-06-03) # Based on exercise 5-22, The Unix Programming Environment. if test "$#" -le 1 then echo 'usage: overwrite file cmd [args]' 1>&2; exit 2 fi file="$1"; shift new=/tmp/NEW.$$; old=/tmp/OLD.$$ > "$new"; > "$old" chmod 600 "$new" "$old" # Protect data privacy. if eval "$@" > "$new" then if test -e "$file" then cp "$file" "$old" fi trap '' 1 2 15 cp "$new" "$file" else echo "overwrite: $1 failed, $file unchanged" 1>&2; exit 3 fi rm -f $new # Leave $old as backup.