chiark / gitweb /
Version 1.2.6.
[misc] / inplace.in
CommitLineData
b2ffb9b7 1#! @BASH@
3770fe80 2
3set -e
4usage () { echo >&2 "$0 FILE ... -- PROG ARGS"; exit 1; }
5case "$#:$1" in
6 0:* | 1:-h | 1:--help)
7 echo "$0 FILE ... -- PROG ARGS"
8 echo " For each FILE, run PROG ARGS with stdin from FILE. If all is"
9 echo " good, replace each FILE with the corresponding output."
10 echo " Clobbers FILE.old and FILE.new."
11 exit 0
12 ;;
13esac
14declare -a file
15while :; do
16 [ $# -eq 0 ] && usage
17 f=$1; shift
18 [ "$f" = -- ] && break
19 file=("${file[@]}" "$f")
20done
21[ $# -eq 0 ] && usage
22
23cleanup_a () {
24 for f in "${file[@]}"; do
25 rm -f "$f.new" "$f.old"
26 done
27}
28trap cleanup_a EXIT INT QUIT TERM
29
30for f in "${file[@]}"; do "$@" <"$f" >"$f.new"; done
31for f in "${file[@]}"; do cp "$f" "$f.old"; done
32
33cleanup_b () {
34 for f in "${file[@]}"; do
35 rm -f "$f.new"
36 mv "$f.old" "$f"
37 done
38}
39trap cleanup_b EXIT INT QUIT TERM
40
41for f in "${file[@]}"; do mv "$f.new" "$f"; done
42trap '' EXIT INT QUIT TERM
43for f in "${file[@]}"; do rm -f "$f.old"; done
44exit 0