#!/bin/bash

#  "subst", a script that substitutes one pattern for another in a file,


ARGS=3         # Script requires 3 arguments.
E_BADARGS=65   # Wrong number of arguments passed to script.

if [ $# -ne "$ARGS" ]
# Test number of arguments to script
then
  echo "Usage: `basename $0` old-pattern new-pattern filename"
  exit $E_BADARGS
fi

old_pattern=$1
new_pattern=$2

if [ -f "$3" ]
then
    file_name=$3
else
    echo "File \"$3\" does not exist."
    exit $E_BADARGS
fi

#  DO THE SUBSTITUTION

# -----------------------------------------------
sed -e "s/$old_pattern/$new_pattern/g" $file_name
# -----------------------------------------------

#  's' is, of course, the substitute command in sed,
#+ and /pattern/ invokes address matching.
#  The "g", or global flag causes substitution for *every*
#+ occurence of $old_pattern on each line, not just the first.

