#!/bin/bash # 2008-09-23 umask 022 [[ $# -ne 0 ]] && set -o xtrace && set -o verbose # change to - to enable verbose debugging ### SHELL OPTIONS set +o noclobber # allowed to clobber files set +o noglob # globbing on set -e # abort on first error shopt -s extglob VER="1.1" SEARCH="AskApache"; REPLACE="AskApache1"; SDIR=$PWD; ###########################################################################--=--=--=--=--=--=--=--=--=--=--# ### ### FUNCTIONS ### ###########################################################################==-==-==-==-==-==-==-==-==-==-==# #--=--=--=--=--=--=--=--=--=--=--# # script_title #==-==-==-==-==-==-==-==-==-==-==# function script_title(){ #=# TURNS ON COLORING ONLY FOR TERMS THAT CAN SUPPORT IT C="\033[";C0=;C1=;C2=;C3=;C4=;C5=;C6=;C7=;C8=;C9=; case ${TERM:-dummy} in linux*|con80*|con132*|console|xterm*|vt*|screen*|putty|Eterm|dtterm|ansi|rxvt|gnome*|*color*) C0="${C}0m";C1="${C}1;30m";C2="${C}1;32m";C3="${C}0;32m";C4="${C}1;37m";C5="${C}0;36m";C6="${C}1;35m";C7="${C}0;37m";C8="${C}30;42m";C9="${C}1;36m"; ;; esac clear echo -e "${C1} __________________________________________________________________________ " echo -e "| ${C2} ___ __ ___ __ ${C1} |" echo -e "| ${C2} / _ | ___ / /__ / _ | ___ ___ _____/ / ___ ${C1} |" echo -e "| ${C2} / __ |(_->> ${C4}${1} ${C0}\n\n"; return 0; } #--=--=--=--=--=--=--=--=--=--=--# # pi #==-==-==-==-==-==-==-==-==-==-==# function pi() { echo -e "${C6}=> ${C4}${1} ${C0}"; return 0; } #--=--=--=--=--=--=--=--=--=--=--# # ok_continue #==-==-==-==-==-==-==-==-==-==-==# function ok_continue() { local ans; echo -en "${C4} \n [ Press any key to continue ] ${C0} \n"; read -n 1 ans; return 0; } #--=--=--=--=--=--=--=--=--=--=--# # yes_no #==-==-==-==-==-==-==-==-==-==-==# function yes_no() { local a YN=65; echo -en "${C2}>>> ${C4}${1:-Q} [y/n] ${C0}"; read -n 1 a; case $a in [yY]) YN=0; ;; esac; return $YN; } #--=--=--=--=--=--=--=--=--=--=--# # p_done #==-==-==-==-==-==-==-==-==-==-==# function p_done() { sleep 1; echo -e "\n${C8} DONE ${C0} \n"; sleep 1; return 0; } #--=--=--=--=--=--=--=--=--=--=--# # get_settings #==-==-==-==-==-==-==-==-==-==-==# function get_settings(){ local a cha script_title while true; do for a in "SDIR" "SEARCH" "REPLACE"; do cha=g echo -en "\n ${C4}(Enter for Default: ${!a} )${C0}\n ${a}${C6}=> ${C0} "; read -e cha; echo; [[ ${#cha} -gt 2 ]] && eval "$a"=$cha; done yes_no "ARE THESE SETTINGS CORRECT" && echo -e "\n\n" && break done } #--=--=--=--=--=--=--=--=--=--=--# # search_and_replace #==-==-==-==-==-==-==-==-==-==-==# function search_and_replace(){ for i in $(seq 0 $((${#FOUNDFILES[@]} - 1))); do thefile=${FOUNDFILES[$i]} if [[ $1 -eq 0 ]]; then echo -e "\n\n\n___________________________________________________________________\n" echo -e "${C4}FILE: ${C3}${thefile} ${C2}($(command du -hs ${thefile}|awk '{ print $1}'))" echo -e "${C4}Type: ${C6}$(command file -b ${thefile})" echo -e "${C7}Matching Lines:${C0}" grep --color=always $SEARCH $thefile echo -e "\n___________________________________________________________________\n" while true; do yes_no "Replace occurances of ${SEARCH} with ${REPLACE}?" && echo -e "\nREPLACING...\n" && sed -i -e "s%${SEARCH}%${REPLACE}%g" $thefile && p_done && break echo -e "\nSKIPPING..\n" && p_done && break done else sed -i -e "s%${SEARCH}%${REPLACE}%g" $thefile fi done return 0; } ############################################################################################################ ### ### MAIN CODE ### ############################################################################################################ #=# CATCH SCRIPT KILLED BY USER trap 'echo error; kill -9 $$' SIGHUP SIGINT SIGTERM #=# ALLOWS SCRIPT TO WORK WHEN RUN STARTED FROM ANY DIRECTORY cd `dirname $0` #=# MAKE MAIN SCRIPT NICE AS POSSIBLE SINCE IT DOESNT DO MUCH renice 19 -p $$ &>/dev/null #=# GET SCRIPT SETTINGS get_settings #=# DISPLAY SCRIPT TITLE script_title cd $SDIR declare -a FOUNDFILES=( `grep -R -l $SEARCH . 2>/dev/null` ); declare -a FOUNDMATCHES=( ); for i in $(seq 0 $((${#FOUNDFILES[@]} - 1))); do F=${FOUNDFILES[$i]}; FOUNDMATCHES[$i]=$(grep -c $SEARCH $F); done pm "FOUND ${SEARCH} IN ${#FOUNDFILES[@]} FILES" for i in $(seq 0 $((${#FOUNDFILES[@]} - 1))); do echo -e "${C4}${FOUNDFILES[$i]} ${C6}=>${C4} ${FOUNDMATCHES[$i]} matches"; done echo -e "\n" while true; do yes_no "Replace all occurances of ${SEARCH} with ${REPLACE} without prompting?" && search_and_replace 1 && p_done && break search_and_replace 0 && p_done && break done cd $OLDPWD exit 0