root/bin/in_place @ 9976
1 |
#!/bin/sh -e
|
---|---|
2 |
# Runs a stream command on a file
|
3 |
# Usage: self file command
|
4 |
|
5 |
file="$1" |
6 |
shift
|
7 |
|
8 |
test "$#" -ge 1 || exit 0 # no command to run |
9 |
|
10 |
temp="$(tempfile)" |
11 |
onExit () { rm -f "$temp"; } |
12 |
trap onExit EXIT
|
13 |
|
14 |
"$@" <"$file" >"$temp" # exits on error so file not updated |
15 |
diff "$file" "$temp" >/dev/null && exit # don't update file if no change |
16 |
case "$(uname)" in Linux) chmod --reference="$file" "$temp";; esac |
17 |
mv "$temp" "$file" |