Project

General

Profile

1
#!/bin/sh -e
2
# Runs a stream command on a file
3
# Usage: [preserve_mtime=1] [preserve_mtime=1] 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 --brief "$file" "$temp" >/dev/null && exit # don't update file if no change
16
	# --brief: avoid scanning the entire file for large files
17
if test "$preserve_mtime"; then touch -r "$file" "$temp"; fi
18
case "$(uname)" in Linux) chmod --reference="$file" "$temp";; esac
19
mv "$temp" "$file"
(35-35/86)