summaryrefslogtreecommitdiff
path: root/Meta/write-only-on-difference.sh
blob: a6485e79a9eefc81cc93a71878655df0cc7d1409 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/bin/sh

set -e

if [ "$#" -lt "2" ]; then
    echo "USAGE: $0 <file> <cmd...>"
    exit 1
fi

DST_FILE="$1"
shift

# Just in case:
mkdir -p -- "$(dirname -- "${DST_FILE}")"

cleanup()
{
  rm -f -- "${DST_FILE}.tmp"
}
trap cleanup 0 1 2 3 6

"$@" > "${DST_FILE}.tmp"
# If we get here, the command was successful, and we can overwrite the destination.

if ! cmp --quiet -- "${DST_FILE}.tmp" "${DST_FILE}"; then
    # File changed, need to overwrite:
    mv -f -- "${DST_FILE}.tmp" "${DST_FILE}"
fi