4.3.2. runsed
The shell script runsed was developed to make
changes to an input file permanently. In other words, it is used in
cases when you would want the input file and the output file to be the
same. Like testsed, it creates a temporary file,
but then it takes the next step: copying the file over the original.
#! /bin/sh
for x
do
echo "editing $x: \c"
if test "$x" = sedscr; then
echo "not editing sedscript!"
elif test -s $x; then
sed -f sedscr $x > /tmp/$x$$
if test -s /tmp/$x$$
then
if cmp -s $x /tmp/$x$$
then
echo "file not changed: \c"
else
mv $x $x.bak # save original, just in case
cp /tmp/$x$$ $x
fi
echo "done"
else
echo "Sed produced an empty file\c"
echo " - check your sedscript."
fi
rm -f /tmp/$x$$
else
echo "original file is empty."
fi
done
echo "all done"
To use runsed, create a sed script named
sedscr in the directory where you want to make the
edits. Supply the name or names of the files to edit on the command
line. Shell metacharacters can be used to specify a set of files.
$ runsed ch0?
runsed simply invokes sed -f
sedscr on the named files, one at a time, and redirects the
output to a temporary file. runsed then tests this
temporary file to make sure that output was produced before copying it
over the original.
The muscle of this shell script (line 9) is essentially the same as
testsed. The additional lines are intended to test
for unsuccessful runsfor instance, when no output is produced. It
compares the two files to see if changes were actually made or to see
if an empty output file was produced before overwriting the original.
However, runsed does not protect you from imperfect
editing scripts. You should use testsed first to
verify your changes before actually making them permanent with
runsed.