shell解析class package并复制到指定包路径
脚本如:
Shell代码
echo "###############################class 解析复制工具#####################################"
echo "##解析当前目前下所有.class文件,并复制到对应包路径;如文件存在则先备份再复制 #"
echo "##使用方法: #pp.sh <path> path:class根目录,即类都将复制到以这个为根目录的对应包下。#"
echo "##################################hzdev###############################################"
binPath=$1
if [ ! -n "$binPath" ]; then
echo 未指定class根目录,程序退出
exit 0
#binPath=pwd|sed 's/ //g'
fi
echo ============class根目录:$binPath
timeStamp=`date '+%s'`
for f in *.class
do
################ # echo ========Current class:$f
className=$(echo "$f"|sed -e's//.class//g')
className_=$(echo $className| sed 's//$.*//g')
package=$(javap $className|grep -oP '(?<=class ).*(?=/.'$className_'.* extends)'|sed -e's//.////g')
#echo "className:"$className
#echo "package:"$package
classFile="$binPath"/"$package"/"$f"
classPath="$binPath"/"$package"
# echo $classFile
#check path exists
if [ ! -d "$classPath" ];then
echo =====目录$classPath不存在,创建目录
mkdir $classPath -p
fi
#check class file exists
if [ ! -f "$classFile" ]; then
echo "复制文件$f------------>$classFile"
cp $f $classFile
else
bakFile="$classFile"."$timeStamp"
echo "=======>class文件存在,备份为 $bakFile"
echo "复制文件$f------------>$classFile"
mv $classFile $classFile.$timeStamp
cp $f $classFile
fi
echo "******************************************************************************************"
done