最近开发了一款应用,其显示页面有12个模板,并且适配了横竖屏,即对应了2*12个布局文件,现在有个需求就是修改这些模板中ImageView的缩放类型,于是写了一个shell,用于批量修改布局中相同的属性,有需要的小伙伴可以参考,shell脚本如下:
#!/bin/bash
proper=$1
oldvalue=$2
newvalue=$3
folder=$4
if [ ! ${folder} ] || [ ! -d $folder ] || [ ! ${proper} ] || [ ! ${oldvalue} ] || [ ! ${newvalue} ];then
echo "usage:$0 proper oldValue newValue path"
exit
fi
echo "proper = "$proper
echo "oldvalue = "$oldvalue
echo "newvalue = "$newvalue
files=`grep -r $proper --include=*.xml ${folder} | awk -F":" '{print $1}'`
sortfiles=()
for file in $files
do
file_exist=false
for sortf in ${sortfiles[@]}
do
#echo "sortf = "$sortf
#echo "file = "$file
if [ $file = $sortf ];then
file_exist=true
break
fi
done
#echo "file_exist = "$file_exist
if [ $file_exist = false ];then
index=${#sortfiles[@]}
#echo "index = "$index
sortfiles[$index]=$file
#echo $file
fi
#echo $file
done
for sfile in ${sortfiles[@]}
do
echo "modify "$sfile
sed -i "s#${proper}\s*=\s*\"${oldvalue}\"#${proper}=\"${newvalue}\"#g" $sfile
#dos2unix $sfile
done
展示下效果,这里以修改ImageView
的scaleType
为例,将scaleType
从centerInside
修改为fitXY
:
master@ubuntu-server:app$ ./updateXmlProper.sh "android:scaleType" "centerInside" "fitXY"
usage:./updateXmlProper.sh proper oldValue newValue path
master@ubuntu-server:app$ ./updateXmlProper.sh "android:scaleType" "centerInside" "fitXY" res/
proper = android:scaleType
oldvalue = centerInside
newvalue = fitXY
modify res/layout/info_display_layout_six.xml
modify res/layout/info_display_layout_seven.xml
modify res/layout/info_display_layout_four.xml
modify res/layout/info_display_layout_ten.xml
modify res/layout/info_display_layout_eight.xml
modify res/layout/info_display_layout_nine.xml
modify res/layout/info_display_layout_eleven.xml
modify res/layout/info_display_layout_one.xml
modify res/layout/info_display_layout_two.xml
modify res/layout/info_display_layout_twelve.xml
modify res/layout/info_display_layout_five.xml
modify res/layout/info_display_layout_three.xml
modify res/layout-land/info_display_layout_six.xml
modify res/layout-land/info_display_layout_seven.xml
modify res/layout-land/info_display_layout_four.xml
modify res/layout-land/info_display_layout_ten.xml
modify res/layout-land/info_display_layout_eight.xml
modify res/layout-land/info_display_layout_nine.xml
modify res/layout-land/info_display_layout_eleven.xml
modify res/layout-land/info_display_layout_one.xml
modify res/layout-land/info_display_layout_two.xml
modify res/layout-land/info_display_layout_twelve.xml
modify res/layout-land/info_display_layout_five.xml
modify res/layout-land/info_display_layout_three.xml