xref: /aosp_15_r20/build/soong/scripts/update_out (revision 333d2b3687b3a337dbcca9d65000bca186795e39)
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