Greetings,
DISCLAIMER: My shell scripting is rusty so my question may be borderline stupid. You've been warned.
I need to create a script that a) lists the content of zip files in a directory and b) sends out an `exception` report. My ZIP files contain a control file (for load check). I want to abort the whole process if the control file is missing from the ZIP file.
Typically a zip file will have its contents looking like this:
file_1231232345.ctl
filea.txt
fileb.txt
filec.txt
Here what I wrote so far:
#!/bin/bash
#get today's date in YYYYMMDD formart
DAY=`date +%Y%m%d`
#populate array of HDD zipped log(s) for today
HDD_ZIP_Array=(`ls *${DAY}*.zip`)
HDD_ZIPlen=${#HDD_ZIP_Array[*]}
#echoes message if no files to process and exits.
if [ $HDD_ZIPlen -eq 0 ]
then
echo " HDD CheckZip -- $HDD_ZIPlen files! Process will exit!" ; exit 1;
else
for name in ${HDD_ZIP_Array[@]};
do
echo " Zip File Name: $name"
#list ZIP contents with unzip, remove header and trailer,
#reverse sort to put control file name at index 0 and put file names in array().
HDD_LIST_Array=(`unzip -l $name | head -n -2|tail -n +4 | sort -r | awk '{print $4}'`)
HDD_LISTlen=${#HDD_LIST_Array[*]}
#iterate array to 1) build exception report and 2) look for control file
echo -e "Files in ZIP file: $name\n" >> report.out
for (( i = 0 ; i < ${#HDD_LIST_Array[@]} ; i++ ))
do
echo -e "${HDD_LIST_Array[$i]}" >> report.out
done
echo -e "\n Number of files in ZIP file: $HDD_LISTlen." >> report.out
#since the name of the control contains a random number, glob CTL extension
if [ $i != {*ctl} ]
then
echo "Control file not found!" ; exit 2;
fi
done
fi
It is basic but it works. Except the bottom part where I test for the CTL file extension. It is not working whether the file is here or not.
What am missing?
Thanks for your input.