shell替换程序里的代码

需求:
扩展名类型: .php .html .shtml .inc
将http://market.pk.com/files/union/ad_union_5.js替换为http://market.pk.com/files/union/union_5.js
http://market.pk.com/files/advertisement/27/ad_27.js替换http://market.pk.com/files/upload/27/27.js

http://market.pk.com/files/union/ad_union_(*).js
替换为:http://market.pk.com/files/union/union_(*).js
http://market.pk.com/files/advertisement/(*)/ad_(*).js
替换为:http://market.pk.com/files/upload/(*)/(*).js


脚本1:
#!/bin/bash

ROOT_DIR='/root/temp/wow'
KEYWORD='ad_union'
KEYWORD2='advertisement'

#for i in $(find $ROOT_DIR -name "*.*" -exec grep "$KEYWORD" {} \;)
for i in $(find $ROOT_DIR -name "*.*")
do
ifile=$(grep "$KEYWORD" $i)
if [ "$?" -eq "0" ]
then
FILE_NUMBER=$(echo $ifile | awk -F"\"" '{print $2}' | awk -F"/" '{print $NF}' | awk -F"." '{print $1}' | awk -F"_" '{print $NF}')
#sed -i "s#ad\_union\_[0-9][0-9]#union\_$FILE_NUMBER#g" $i
sed -i "s#ad\_union\_$FILE_NUMBER#union\_$FILE_NUMBER#g" $i
fi

ifile=$(grep "$KEYWORD2" $i)
if [ "$?" -eq "0" ]
then
FILE_NUMBER2=$(echo $ifile | awk -F"\"" '{print $2}' | awk -F"/" '{print $NF}' | awk -F"." '{print $1}' | awk -F"_" '{print $NF}')
sed -i "s#advertisement\/$FILE_NUMBER2\/ad\_$FILE_NUMBER2#upload\/$FILE_NUMBER2\/$FILE_NUMBER2#g" $i
fi
done

 

 


脚本2:
#!/bin/bash

EXT_ARRAY=("*.php" "*.inc" "*.html" "*.shtml")
EXT_ARRAY_LEN=${#EXT_ARRAY[@]}
ROOT_DIR='/root/temp/wow'
KEYWORD='ad_union'
KEYWORD2='advertisement'

#i=0
#while [ $i -lt $EXT_ARRAY_LEN ]
#do
# find $ROOT_DIR -name "${EXT_ARRAY[$i]}" | while read f
# do
# sed -i 's#union\/ad_union_5\.js#union\/union_5\.js#g' $f
# sed -i 's#advertisement\/27\/ad_27\.js#upload\/27\/27\.js#g' $f
# done
# let i++
#done

#for i in $(find $ROOT_DIR -name "*.*" -exec grep "$KEYWORD" {} \;)
for i in $(find $ROOT_DIR -name "*.*")
do
ifile=$(grep "$KEYWORD" $i)
if [ "$?" -eq "0" ]
then
FILE_NUMBER=$(echo $ifile | awk -F"/" '{print $NF}' | awk -F"." '{print $1}' | awk -F"_" '{print $NF}')
#sed -i "s#ad\_union\_[0-9][0-9]#union\_$FILE_NUMBER#g" $i
sed -i "s#ad\_union\_$FILE_NUMBER#union\_$FILE_NUMBER#g" $i
fi

ifile=$(grep "$KEYWORD2" $i)
if [ "$?" -eq "0" ]
then
FILE_NUMBER2=$(echo $ifile | awk -F"/" '{print $NF}' | awk -F"." '{print $1}' | awk -F"_" '{print $NF}')
sed -i "s#advertisement\/$FILE_NUMBER2\/ad\_$FILE_NUMBER2#upload\/$FILE_NUMBER2\/$FILE_NUMBER2#g" $i
fi
done

这两个脚本分别执行替换程序里的代码内容。

 

你可能感兴趣的:(shell,职场,休闲)