LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-28-2010, 06:25 PM   #1
kristo5747
Member
 
Registered: Jul 2010
Location: Earth
Distribution: Ubuntu 11.04 (Natty Narwhal)
Posts: 31

Rep: Reputation: 0
Smile BASH SCRIPTING: 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:

Code:
file_1231232345.ctl
filea.txt
fileb.txt
filec.txt
Here what I wrote so far:

Code:
#!/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.

Al.

Last edited by kristo5747; 07-28-2010 at 06:27 PM.
 
Old 07-28-2010, 07:24 PM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
case "$i" in 
  *ctl ) echo "yes";;
esac
 
Old 07-29-2010, 09:47 AM   #3
kristo5747
Member
 
Registered: Jul 2010
Location: Earth
Distribution: Ubuntu 11.04 (Natty Narwhal)
Posts: 31

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by ghostdog74 View Post
Code:
case "$i" in 
  *ctl ) echo "yes";;
esac
Thanks for your reply but it did not work.

Actually, I figured that I need to check the array element itself, instead of some value...which is what I was doing.

Here's what works:

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

Al.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash Scripting - Accessing Array Indices Jus144tice Programming 7 01-10-2015 03:39 PM
Incrementing an array element in Shell scripting ohijames Linux - Newbie 2 06-23-2010 05:06 PM
get index of an element in array in bash mangatmodi Programming 4 11-20-2009 07:45 AM
[perl] copying an array element into another array s0l1dsnak3123 Programming 2 05-17-2008 01:47 AM
Bash Shell Scripting - using ls into array aliasofmike Programming 5 11-05-2007 03:00 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:25 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration