Help! Yet another check element in array Question

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.

You could try

if [[ $i != *.ctl ]]

In bash it seems if you use double square brackets, that the wildcard is working. Single sq. brackets do not work - I guess test is a bit limited to the shell built-in:

$> VAR=12345.ctl
$> [ $VAR == *.ctl ] && echo found
$> [[ $VAR == *.ctl ]] && echo found
found

---------- Post updated at 09:18 AM ---------- Previous update was at 09:10 AM ----------

Oh and I didn't notice at first:

Checking $i will not help since it is just a counter for your array and will contain only a number, if I am correct.

Better check something like ${HDD_LIST_Array[$i]}

1 Like

Thanks for your reply.

I realized I needed to compare the element of the array instead of some value...which is what I was doing.

I tried this:

i=`echo ${HDD_LIST_Array[0]} | awk -F . '{print $NF}'`
if [ "$i" != "ctl" ]
  then
    echo "Control file not found!" ; exit 2;
fi

It worked.

Al.