1#! /bin/bash 2# Run given command application and update the contents of a given file. 3# Will not change the file if its contents has not changed. 4[[ $# -gt 1 ]] || { echo "Usage: ${0##*/} FILE COMMAND" >&2; exit 1; } 5set -u 6declare -r outfile="$1" 7shift 8if [[ ! -f $outfile ]]; then 9 $@ >$outfile 10 exit 11fi 12 13declare -r newout=${outfile}.new 14$@ >$newout 15rc=$? 16if cmp -s $newout $outfile; then 17 rm $newout 18else 19 mv -f $newout $outfile 20fi 21exit $rc 22