全局替換由crontab起的shell腳本中的某些字符串

遇到這樣一個需求: shell

須要替換crontab起的shell腳本中的某個域名,可是不知道crontab中到底有多少的shell腳本包含被替換的域名,難道要手工去查找麼?因而寫了個腳本,並通用之 bash

功能:替換crontab中的shell腳本的某個字符串爲另一個,用法: spa

Usage  : sh replaceCronScript.sh src="string1" dst="string2"
Example: sh replaceCronScript.sh src=clicklog.dtc.log.sz.com dst=soso1_0.dtc.log.sz.com
code

#! /bin/bash

#####################################################
#name           : replaceCronScript.sh  
#note           : replace some crontab shell scripts's 
#               : string with new string 
#date           : 2012.12.27
#author         : <peterguo@vip.qq.com>
####################################################


if [ $# -eq 0 ];
then
    echo "Usage  : sh $(basename $0) src=\"string1\" dst=\"string2\""
    echo "Example: sh $(basename $0) src=clicklog.dtc.log.sz.com dst=soso1_0.dtc.log.sz.com"
    exit 1
fi

InnerIP=$(/sbin/ifconfig eth1 | grep "inet addr" | grep -v "127.0.0.1" | awk '{print $2}' | cut -c 6-)
CurFullScript=$(readlink /proc/$$/fd/255)
CurScriptPath=$(dirname $CurFullScript)
CurScriptName=$(basename $CurFullScript)
FullParam=$*
CurPid=$$

#Reslove the param, format name=value -> $name
eval $*

function colorEcho(){
    [ $# -eq 0 ] && echo "colorEcho Usage: colorEcho [red|green|yellow|blue|pink|dark|bright|cyan] sentence"
    [ $# -eq 0 ] && return 1
    colorFmtTail='\033[m'
    brightFmtHead='\033[1;29m'
    darkFmtHead='\033[1;30m'
    redFmtHead='\033[1;31m'
    greenFmtHead='\033[1;32m'
    yellowFmtHead='\033[1;33m'
    blueFmtHead='\033[1;34m'
    pinkFmtHead='\033[1;35m'
    cyanFmtHead='\033[1;36m'

    case $1 in
    "red")
        echo -e ${redFmtHead}${2}${colorFmtTail}
        ;;
    "green")
        echo -e ${greenFmtHead}${2}${colorFmtTail}
        ;;
    "yellow")
        echo -e ${yellowFmtHead}${2}${colorFmtTail}
        ;;
    "blue")
        echo -e ${blueFmtHead}${2}${colorFmtTail}
        ;;
    "pink")
        echo -e ${pinkFmtHead}${2}${colorFmtTail}
        ;;
    "dark")
        echo -e ${darkFmtHead}${2}${colorFmtTail}
        ;;        
    "bright")
        echo -e ${brightFmtHead}${2}${colorFmtTail}
        ;;  
    "cyan")
        echo -e ${cyanFmtHead}${2}${colorFmtTail}
        ;;          
    *)
        echo ${2}
    esac  
}

#Display the Current Environment Values
echo "[$CurPid][$CurScriptPath][$CurScriptName][$FullParam]"
#************************* main ****************************#
total=0
for file in $(crontab -l | grep "sh " | awk '{for(i=1;i<=NF;i++) print $i}' | grep ".sh$" | sed "s/;.//g")
do
    GrepRst=$(grep \"$src\" $file 2>/dev/null)
    [[ $? -eq 0 ]] && {
        echo $file
        total=$((total + 1))
    }
done

colorEcho yellow ">>>>>>>>>>>>>> Start to Confirm Total[$total]........\n"

process_files=""
for file in $(crontab -l | grep "sh " | awk '{for(i=1;i<=NF;i++) print $i}' | grep ".sh$" | sed "s/;.//g")
do
    GrepRst=$(grep \"$src\" $file 2>/dev/null)
    if [ $? -eq 0 ];
    then
        echo -e "\n\n"
        echo Found in $file
        colorEcho bright "****************************************** begin ************************************************************"
        echo $GrepRst
        colorEcho bright "******************************************* end  ************************************************************"
        colorEcho red "Replace[Enter] NoReplace[N|n]"
        read strConfirm
        if [ "${strConfirm}" == "N" ] || [ "${strConfirm}" == "n" ]
        then
            colorEcho green "Skipping file $file"
        else 
            colorEcho green "sed -i \"s/$src/$dst/g\" $file"
            sed -i "s/$src/$dst/g" $file
            [[ $? -ne 0 ]] && {
                colorEcho red "Error with $file"
                exit 1
            }
            process_files="$process_files	\n$file"
        fi
    fi
done

colorEcho green "\nProcessed files as following ......."
echo -e $process_files
相關文章
相關標籤/搜索