b2ffb9b7 |
1 | #! @BASH@ |
3770fe80 |
2 | |
3 | set -e |
4 | usage () { echo >&2 "$0 FILE ... -- PROG ARGS"; exit 1; } |
5 | case "$#:$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 | ;; |
13 | esac |
14 | declare -a file |
15 | while :; do |
16 | [ $# -eq 0 ] && usage |
17 | f=$1; shift |
18 | [ "$f" = -- ] && break |
19 | file=("${file[@]}" "$f") |
20 | done |
21 | [ $# -eq 0 ] && usage |
22 | |
23 | cleanup_a () { |
24 | for f in "${file[@]}"; do |
25 | rm -f "$f.new" "$f.old" |
26 | done |
27 | } |
28 | trap cleanup_a EXIT INT QUIT TERM |
29 | |
30 | for f in "${file[@]}"; do "$@" <"$f" >"$f.new"; done |
31 | for f in "${file[@]}"; do cp "$f" "$f.old"; done |
32 | |
33 | cleanup_b () { |
34 | for f in "${file[@]}"; do |
35 | rm -f "$f.new" |
36 | mv "$f.old" "$f" |
37 | done |
38 | } |
39 | trap cleanup_b EXIT INT QUIT TERM |
40 | |
41 | for f in "${file[@]}"; do mv "$f.new" "$f"; done |
42 | trap '' EXIT INT QUIT TERM |
43 | for f in "${file[@]}"; do rm -f "$f.old"; done |
44 | exit 0 |