-
Notifications
You must be signed in to change notification settings - Fork 2
/
inplace
148 lines (122 loc) · 3.15 KB
/
inplace
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#! /bin/sh
##
## inplace - Script for 'in place' file replacing
## Copyright (c) 2006 by Michal Nazarewicz <[email protected]>
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 3 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, see <http://www.gnu.org/licenses/>.
##
## This is part of Tiny Applications Collection
## -> http://tinyapps.sourceforge.net/
##
set -e
readonly ARGV0=${0##*/}
err () {
echo "$ARGV0: $*" >&2
exit 1
}
##
## usage
##
usage () {
echo 'inplace 0.1 (c) 2006 by Michal Nazarewicz (mina86/AT/mina86.com)'
[ X"$1" != Xversion ] || exit 0
cat <<EOF
usage: $ARGV0 -h | --help | -V | --version
$ARGV0 [ -s[<suffix>] ] <command> [ <argumet> ... ] -- <file> [ ... ]
Runs <command> with given <argument>s for each <file> as a standard
input replacing it's contents with what <command> outputs to standard
output.
Options:
-h --help displays this help screen
-V --version displays version
-s<suffix> back ups each <file> to <file><suffix>
-s back ups each <file> to <file>.bak
When separating file names and command arguments the first '--' is used.
EOF
}
##
## Parse args
##
SUFFIX=
while [ $# -gt 0 ] && [ X"$1" != X"${1#-}" ]; do
case "$1" in
(-h|--help) usage full ;;
(-V|--version) usage version ;;
(-s|--suffix) SUFFIX=.bak ;;
(-s?*) SUFFIX=${1#-s} ;;
(*) err "$1: unknown option; type $ARG0 --help for help"
esac
shift
done
##
## Find first --
##
NUM=1
while [ $NUM -le $# ] && eval [ X\"\$$NUM\" -ne X-- ]; do
NUM=$(( $NUM + 1 ))
done
[ $NUM -gt $# ] || err 'no -- given'
[ $NUM -ne 1 ] || err 'no command given'
[ $NUM -ne $# ] || err 'no files given'
##
## Temporary file
##
if [ -z "$SUFFIX" ]; then
# Find temp dir
TMPFILE=.
for N in "$TMPDIR" "$TMP" "$TEMP" ~/tmp /tmp; do
if [ -n "$N" ] && [ -d "$N" ] && [ -w "$N" ]; then
TMPFILE="$N"
break
fi
done
# tempfile exists
UMASK=$(umask)
umask 077
if which mktemp >/dev/null 2>&1; then
TMPFILE=$(mktemp "$TEMPFILE/inplace-XXXXXXXX")
elif which tempfile >/dev/null 2>&1; then
TMPFILE=$(tempfile -d "$TEMPFILE" -p "inplace-")
else
err unable to create temporary file, mktemp or tempfile required
fi
trap 'rm -f -- "$TMPFILE"' 0
fi
##
## Main loop
##
N=1
for FILE in "$@"; do
# Command
if [ $N -lt $NUM ]; then
eval CMD_$N=\$FILE
N=$(( $N + 1 ))
continue
fi
# --
if [ $N -eq $NUM ]; then
set --
while [ $NUM -gt 1 ]; do
NUM=$(( $NUM - 1 ))
eval "set -- \"\$CMD_$NUM\" \"\$@\"; unset CMD_$NUM"
done
continue
fi
# File
if [ -n "$SUFFIX" ]; then
cp -fp -- "$FILE" "$FILE$SUFFIX" && "$@" <"$FILE$SUFFIX" >"$FILE"
else
"$@" <"$FILE" >"$TMPFILE" && cat <"$TMPFILE" >"$FILE"
fi
done