Project

General

Profile

1 8706 aaronmk
#!/bin/sh -e
2 349 aaronmk
# Runs a stream command on a file
3 13309 aaronmk
# Usage: [preserve_mtime=1] [preserve_mtime=1] self file command
4 349 aaronmk
5
file="$1"
6
shift
7
8 8707 aaronmk
test "$#" -ge 1 || exit 0 # no command to run
9
10 349 aaronmk
temp="$(tempfile)"
11 3755 aaronmk
onExit () { rm -f "$temp"; }
12
trap onExit EXIT
13 349 aaronmk
14 8706 aaronmk
"$@" <"$file" >"$temp" # exits on error so file not updated
15 13309 aaronmk
diff --brief "$file" "$temp" >/dev/null && exit # don't update file if no change
16
	# --brief: avoid scanning the entire file for large files
17 13308 aaronmk
if test "$preserve_mtime"; then touch -r "$file" "$temp"; fi
18 8735 aaronmk
case "$(uname)" in Linux) chmod --reference="$file" "$temp";; esac
19 349 aaronmk
mv "$temp" "$file"