| Sasha Smundak | 82b6e84 | 2021-11-17 17:07:44 -0800 | [diff] [blame] | 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; } | 
|  | 5 | set -u | 
|  | 6 | declare -r outfile="$1" | 
|  | 7 | shift | 
|  | 8 | if [[ ! -f $outfile ]]; then | 
|  | 9 | $@ >$outfile | 
|  | 10 | exit | 
|  | 11 | fi | 
|  | 12 |  | 
|  | 13 | declare -r newout=${outfile}.new | 
|  | 14 | $@ >$newout | 
|  | 15 | rc=$? | 
|  | 16 | if cmp -s $newout $outfile; then | 
|  | 17 | rm $newout | 
|  | 18 | else | 
|  | 19 | mv -f $newout $outfile | 
|  | 20 | fi | 
|  | 21 | exit $rc |