Project

General

Profile

1 8706 aaronmk
#!/bin/sh -e
2 349 aaronmk
# Runs a stream command on a file
3
# Usage: self file command
4
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 349 aaronmk
diff "$file" "$temp" >/dev/null && exit # don't update file if no change
16 8735 aaronmk
case "$(uname)" in Linux) chmod --reference="$file" "$temp";; esac
17 349 aaronmk
mv "$temp" "$file"