Globally replace certain strings in shell scripts started with crontab
Last Update:2014-05-23
Source: Internet
Author: User
Global replacement of some strings in the shell script starting with crontab meets the following requirement: www.2cto.com needs to replace a domain name in the shell script starting with crontab, but I don't know how many shell scripts in crontab contain the replaced domain name. do you need to find them manually...
Global replacement of some strings in the shell script starting with crontab meets the following requirement: www.2cto.com needs to replace a domain name in the shell script starting with crontab, but I don't know how many shell scripts in crontab contain the replaced domain name. Do I need to search for them manually? So I wrote a script and used it. function: replace a string of the shell script in crontab with another string. Usage: sh replaceCronScript. sh src = "string1" dst = "string2" Example: sh replaceCronScript. sh src = clicklog.dtc.log.sz.com dst = sosoft-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: 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 = Taobao "016 exit 1017fi018 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-> $ name027eval $ * 028 029 function 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 }$ {col OrFmtTail} 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 Environ Ment Values073echo "[$ CurPid] [$ CurScriptPath] [$ CurScriptName] [$ FullParam]" 074 #****************** * ********************************** #075 total = 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 085 colorEcho 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" 094 echo Found in $ file095 colorEcho bright "******************** * ********************* begin ***************** **************************************** * ** "096 echo $ GrepRst097 colorEcho bright "***************************** ************************* ********************************** "098 colorEcho red" replace [Enter] NoReplace [N | n] "099 read strCo Nfirm100 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 115 colorEcho green "\ nProcessed files as following ....... "116 echo-e $ process_files