全域替換由crontab起的shell指令碼中的某些字串

來源:互聯網
上載者:User


全域替換由crontab起的shell指令碼中的某些字串 遇到這樣一個需求:  www.2cto.com  需要替換crontab起的shell指令碼中的某個網域名稱,但是不知道crontab中到底有多少的shell指令碼包含被替換的網域名稱,難道要手工去尋找嗎?於是寫了個指令碼,並通用之. 功能:替換crontab中的shell指令碼的某個字串為另外一個,用法: 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  www.2cto.com  001#! /bin/bash002 003#####################################################004#name           : replaceCronScript.sh 005#note           : replace some crontab shell scripts's006#               : string with new string007#date           : 2012.12.27008#author         : <peterguo@vip.qq.com>009####################################################010 011 012if [ $# -eq 0 ];013then014    echo "Usage  : sh $(basename $0) src=\"string1\" dst=\"string2\""015    echo "Example: sh $(basename $0) src=clicklog.dtc.log.sz.com dst=soso1_0.dtc.log.sz.com"016    exit 1017fi018 019InnerIP=$(/sbin/ifconfig eth1 | grep "inet addr" | grep -v "127.0.0.1" | awk '{print $2}' | cut -c 6-)020CurFullScript=$(readlink /proc/$$/fd/255)021CurScriptPath=$(dirname $CurFullScript)022CurScriptName=$(basename $CurFullScript)023FullParam=$*024CurPid=$$025 026#Reslove the param, format name=value -> $name027eval $*028 029function colorEcho(){030    [ $# -eq 0 ] && echo "colorEcho Usage: colorEcho [red|green|yellow|blue|pink|dark|bright|cyan] sentence"031    [ $# -eq 0 ] && return 1032    colorFmtTail='\033[m'033    brightFmtHead='\033[1;29m'034    darkFmtHead='\033[1;30m'035    redFmtHead='\033[1;31m'036    greenFmtHead='\033[1;32m'037    yellowFmtHead='\033[1;33m'038    blueFmtHead='\033[1;34m'039    pinkFmtHead='\033[1;35m'040    cyanFmtHead='\033[1;36m'041 042    case $1 in043    "red")044        echo -e ${redFmtHead}${2}${colorFmtTail}045        ;;046    "green")047        echo -e ${greenFmtHead}${2}${colorFmtTail}048        ;;049    "yellow")050        echo -e ${yellowFmtHead}${2}${colorFmtTail}051        ;;052    "blue")053        echo -e ${blueFmtHead}${2}${colorFmtTail}054        ;;055    "pink")056        echo -e ${pinkFmtHead}${2}${colorFmtTail}057        ;;058    "dark")059        echo -e ${darkFmtHead}${2}${colorFmtTail}060        ;;       061    "bright")062        echo -e ${brightFmtHead}${2}${colorFmtTail}063        ;; 064    "cyan")065        echo -e ${cyanFmtHead}${2}${colorFmtTail}066        ;;         067    *)068        echo ${2}069    esac 070}071 072#Display the Current Environment Values073echo "[$CurPid][$CurScriptPath][$CurScriptName][$FullParam]"074#************************* main ****************************#075total=0076for file in $(crontab -l | grep "sh " | awk '{for(i=1;i<=NF;i++) print $i}' | grep ".sh$" | sed "s/;.//g")077do078    GrepRst=$(grep \"$src\" $file 2>/dev/null)079    [[ $? -eq 0 ]] && {080        echo $file081        total=$((total + 1))082    }083done084 085colorEcho yellow ">>>>>>>>>>>>>> Start to Confirm Total[$total]........\n"086 087process_files=""088for file in $(crontab -l | grep "sh " | awk '{for(i=1;i<=NF;i++) print $i}' | grep ".sh$" | sed "s/;.//g")089do090    GrepRst=$(grep \"$src\" $file 2>/dev/null)091    if [ $? -eq 0 ];092    then093        echo -e "\n\n"094        echo Found in $file095        colorEcho bright "****************************************** begin ************************************************************"096        echo $GrepRst097        colorEcho bright "******************************************* end  ************************************************************"098        colorEcho red "Replace[Enter] NoReplace[N|n]"099        read strConfirm100        if [ "${strConfirm}" == "N" ] || [ "${strConfirm}" == "n" ]101        then102            colorEcho green "Skipping file $file"103        else104            colorEcho green "sed -i \"s/$src/$dst/g\" $file"105            sed -i "s/$src/$dst/g" $file106            [[ $? -ne 0 ]] && {107                colorEcho red "Error with $file"108                exit 1109            }110            process_files="$process_files   \n$file"111        fi112    fi113done114 115colorEcho green "\nProcessed files as following ......."116echo -e $process_files 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.