#!/bin/sh
# Check whether all the files in ZIP package are valid file type.
# If not, removed the invalid files. Finally, ZIP left files to ZIP package.
#
# Usage : checkfile.sh FILE_NAME DIR
#
# Author : Ray Chen
# Date : 2007-09-27
echo
echo ===================================
echo = Usage : checkfile.sh FILE_NAME DIR
echo =
echo ===================================
echo
usage()
{
echo "Usage : checkfile.sh FILE_NAME DIR"
exit;
}
if [ $# -ne 2 ]; then
usage;
fi
FILE_NAME=$1
DIR=$2
# define a file type list. When added new support file
# type, don't forget to update type_num.
file_type_list=(ver rpt dsp cul gpr bat ada xml txt)
type_num=9
if [ "$FILE_NAME" = "" ]; then
echo "File name can't be null"
usage;
fi
if [ -d "$DIR" ]
then
echo "The checked directory is $DIR"
else
echo "$DIR doesn't exist"
usage;
fi
echo
echo ===================================
echo = Step 1 : Unzip $FILE_NAME.zip
echo =
echo ===================================
echo
cd "$DIR"
#ZIP=`echo *`
#ctp_zip=`echo $ZIP | tr A-Z a-z`
# unzip
unzip -o $FILE_NAME.zip
if [ $? -eq 0 ]
then
echo "Unzip completely"
# Remove unnecessary files
rm -f $FILE_NAME.zip $FILE_NAME.ZIP
rm -f $ctp_zip.ZIP $ctp_zip.zip
else
echo "Unzip error"
exit;
fi
echo
echo ====================================
echo = Step 2 : Check $FILE_NAME.zip
echo =
echo ====================================
echo
# Check file type
check_type()
{
local_name=$1
local_type=$2
# change upcase to lowcase
check_type=`echo "$local_type" | tr A-Z a-z`
count=0
while [ $check_type != ${file_type_list[$count]} ]
do
let "count += 1"
if [ $count -eq $type_num ]; then
rm -f $local_name
echo "Removed $local_name file"
break;
fi
done
}
# loop to check
for file in *
do
file_name=`basename $file`
# get the file type(3 char)
file_type=${file_name: -3}
check_type $file_name $file_type
done
if [ $? -eq 0 ]
then
echo "Check completely"
else
echo "Check error"
exit;
fi
echo
echo ====================================
echo = Step 3 : zip $FILE_NAME.zip
echo =
echo ====================================
echo
zip $FILE_NAME.zip *
if [ $? -eq 0 ]
then
echo "Zip completely"
else
echo "Zip error"
fi
|