全局替換由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/bash
002
003
#####################################################
004
#name : replaceCronScript.sh
005
#note : replace some crontab shell scripts's
006
# : string with new string
007
#date : 2012.12.27
008
#author : <
[email protected]>
009
####################################################
010
011
012
if [ $# -eq 0 ];
013
then
014
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 1
017
fi
018
019
InnerIP=$(/sbin/ifconfig eth1 | grep "inet addr" | grep -v "127.0.0.1" | awk '{print $2}' | cut -c 6-)
020
CurFullScript=$(readlink /proc/$$/fd/255)
021
CurScriptPath=$(dirname $CurFullScript)
022
CurScriptName=$(basename $CurFullScript)
023
FullParam=$*
024
CurPid=$$
025
026
#Reslove the param, format name=value -> $name
027
eval $*
028
029
function colorEcho(){
030
[ $# -eq 0 ] && echo "colorEcho Usage: colorEcho [red|green|yellow|blue|pink|dark|bright|cyan] sentence"
031
[ $# -eq 0 ] && return 1
032
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 in
043
"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 Values
073
echo "[$CurPid][$CurScriptPath][$CurScriptName][$FullParam]"
074
#************************* main ****************************#
075
total=0
076
for file in $(crontab -l | grep "sh " | awk '{for(i=1;i<=NF;i++) print $i}' | grep ".sh$" | sed "s/;.//g")
077
do
078
GrepRst=$(grep \"$src\" $file 2>/dev/null)
079
[[ $? -eq 0 ]] && {
080
echo $file
081
total=$((total + 1))
082
}
083
done
084
085
colorEcho yellow ">>>>>>>>>>>>>> Start to Confirm Total[$total]........\n"
086
087
process_files=""
088
for file in $(crontab -l | grep "sh " | awk '{for(i=1;i<=NF;i++) print $i}' | grep ".sh$" | sed "s/;.//g")
089
do
090
GrepRst=$(grep \"$src\" $file 2>/dev/null)
091
if [ $? -eq 0 ];
092
then
093
echo -e "\n\n"
094
echo Found in $file
095
colorEcho bright "****************************************** begin ************************************************************"
096
echo $GrepRst
097
colorEcho bright "******************************************* end ************************************************************"
098
colorEcho red "Replace[Enter] NoReplace[N|n]"
099
read strConfirm
100
if [ "${strConfirm}" == "N" ] || [ "${strConfirm}" == "n" ]
101
then
102
colorEcho green "Skipping file $file"
103
else
104
colorEcho green "sed -i \"s/$src/$dst/g\" $file"
105
sed -i "s/$src/$dst/g" $file
106
[[ $? -ne 0 ]] && {
107
colorEcho red "Error with $file"
108
exit 1
109
}
110
process_files="$process_files \n$file"
111
fi
112
fi
113
done
114
115
colorEcho green "\nProcessed files as following ......."
116
echo -e $process_files