#!/bin/sh # s a n d r # (Search and Replace) # Written by Derek J. Punaro on 4/23/99 # Last modified: 8/5/99 # Usage: sandr [-r] ########################################################################### # Variables ########################################################################### SCRIPTNAME="sandr" # This is the name of the script STARTDIR="" # Starting directory OLDPTRN="" # The pattern we want to replace NEWPTRN="" # The pattern we want in it's place RECURSIVE=0 # By default, do not search recursively PROGDIR="" # The directory where the script sits MASK="" ########################################################################### # Function declarations ########################################################################### startprogram() { echo "Enter starting directory: \c" read STARTDIR echo "Enter pattern to find: \c" read OLDPTRN echo "Enter pattern to replace it with: \c" read NEWPTRN PROGDIR=`pwd` cd $STARTDIR } replace() { for FILE in `ls -p1` do if [ -f $FILE ] then if [ $FILE != "change.sh" ] then grep $OLDPTRN $FILE > /dev/null if [ $? -eq 0 ] then sed "s%${OLDPTRN}%${NEWPTRN}%g" $FILE > ${FILE}.new #mv ${FILE}.new $FILE fi fi elif [ -d $FILE ] then if [ $RECURSIVE = 1 ] then cd $FILE ${PROGDIR}/$SCRIPTNAME $OLDPTRN $NEWPTRN $PROGDIR fi fi done } ########################################################################### # Begin program ########################################################################### # Check to see if this is the initial running of the script if [ "x$3" = "x" ] then while getopts r OPT do case "$OPT" in r) RECURSIVE=1 shift 1 ;; esac done startprogram else RECURSIVE=1 OLDPTRN=$1 NEWPTRN=$2 PROGDIR=$3 fi replace