|
#!/bin/bash
clear
echo
echo "################################################################"
echo "# #"
echo "# RECURSIVE SEARCH & REPLACE #"
echo "# By Thomas Guilleminot - www.guilleminot.org #"
echo "# #"
echo "################################################################"
## FUNCTION
DECLARATION ##
function ConfirmOrExit() {
while true
do
echo
echo -n "Please confirm
(y or n) : "
read CONFIRM
case $CONFIRM in
y|Y|YES|yes|Yes)
break ;;
n|N|no|NO|No)
echo
echo
Aborting as you entered $CONFIRM
echo
exit
;;
*) echo Please
enter only y or n
esac
done
echo You entered $CONFIRM. Continuing...
}
## PARAMETERS PASSED - IF NOT SET ASSIGNING DEFAULT VALUES ##
test -n "$1" && TARGET_DIRECTORY=$1
test -n "$2" && FILE_EXTENSION=$2
test -n "$3" && STRING_SEARCH=$3
test -n "$4" && STRING_REPLACE=$4
echo
echo "PARAMETERS :"
echo "------------"
echo "TARGET_DIRECTORY : "
${TARGET_DIRECTORY="/mydirectory/"};
echo "FILE_EXTENSION :
" ${FILE_EXTENSION="*.txt"};
echo "STRING_SEARCH :
" ${STRING_SEARCH="the old string"};
echo "STRING_REPLACE :
" ${STRING_REPLACE='the new string'};
## EXECUTION ##
echo
echo "STEP 1 : SEARCHING FILES..."
echo "---------------------------"
echo
find $TARGET_DIRECTORY -name $FILE_EXTENSION
| sed 's/ /\\ /g' | xargs grep "$STRING_SEARCH"
/dev/null
ConfirmOrExit
echo
echo "STEP 2 : REPLACING STRING..."
echo "----------------------------"
echo
find $TARGET_DIRECTORY -name $FILE_EXTENSION
| sed 's/ /\\ /g' | xargs sed -i -e s/"$STRING_SEARCH"/"$STRING_REPLACE"/
echo "Done."
echo
echo "STEP 3 : CHECKING FILES..."
echo "--------------------------"
echo
find $TARGET_DIRECTORY -name $FILE_EXTENSION
| sed 's/ /\\ /g' | xargs grep "$STRING_SEARCH"
/dev/null
echo "Done. Exiting..."
echo
echo
|