KSH script not looping through array


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting KSH script not looping through array
# 1  
Old 07-18-2008
KSH script not looping through array

Hi All,

I'm trying to get a script to loop through an array. The array is basically a list of .zip files. I'd like the script to loop through and unzip the zip files contained in the zip file list. When I run the script, it unzip the first zip file correctly, and then stops Any thoughts? Here's the script:

Code:
#!/usr/bin/ksh 
clear
# setting up the date to be something a bit more pretty
DATE=`date +%m/%d/%y-%H:%M`
# This date variable is used for directory creation
DATE2=`date +%m%d%y%H%M%S`
# Start a new search in the log files
print "" > logs/recovery.log
print "New Search Started on ${DATE}" >> logs/recovery.log
# User Prompting for email, start date and end date
print " Welcome to the MRR User Recovery Interface"
print ""
print ""
print " Please enter the email address to search for:"
read user
print ""
print " Enter start date to search:"
read sdate
print ""
print "Enter end date of search:"
read edate
print ""
print "I will now search for the email from $user"
print "starting at $sdate and ending at $edate."
print ""
print ""
print "${DATE} Search context email $user" >> logs/recovery.log
print "${DATE} Starting date $sdate" >> logs/recovery.log
print "${DATE} Ending date $edate" >> logs/recovery.log
# Eventually there will be some stuff here to sort the zip files by the sdate and edate and then copy them off to a work directory.
 
# grab a list of the zip files. 
print "Now Getting a file list..."
# The following needs to be changed when putting into production. These file paths
# will *NOT* work. Make sure they are correct! You've been warned.
cd /home/chris/MRR/test/
ls *.zip > /home/chris/MRR/ziplist
print "${DATE} ziplist created" >> /home/chris/MRR/scripts/logs/recovery.log
# This is just for testing purposes, remove or comment out the following line
# cd ../scripts/
# cat ziplist
# Unzipping the files
# To do this, we need to feed the file into an array otherwise ksh will choke.
cat /home/chris/MRR/ziplist|sed 's/.\{4\}$//' >> /home/chris/MRR/dirlist
set -A dirlist $(< /home/chris/MRR/dirlist)
set -A zipfile $(< /home/chris/MRR/ziplist)
for i in $zipfile
do
unzip -qq -d /home/chris/MRR/test/"$dirlist" /home/chris/MRR/test/"$zipfile" >> /home/chris/MRR/scripts/logs/recovery.log 
print "name of files in zipfile:" ${zipfile[*]}
done
#

# 2  
Old 07-18-2008
Shell arrays are ugly and smelly and best avoided (IMO). Why not do something simpler like
Code:
for zip in *.zip
do
    dir=`echo $zip | sed 's/.\{4\}$//'`
    unzip -qq -d $dir $zip >> logfile
done

which wiill avoid writing and reading temporary files in hardwired locations.
# 3  
Old 07-18-2008
Quote:
Originally Posted by spirtle
Shell arrays are ugly and smelly and best avoided (IMO). Why not do something simpler like
Code:
for zip in *.zip
do
    dir=`echo $zip | sed 's/.\{4\}$//'`
    unzip -qq -d $dir $zip >> logfile
done

which wiill avoid writing and reading temporary files in hardwired locations.
Thanks Spirtle! That worked like a charm! Much appreciated!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to pass and read an array in ksh shell script function.?

I'm able to read & print an array in varaible called "filelist" I need to pass this array variable to a function called verify() and then read and loop through the passed array inside the function. Unfortunately it does not print the entire array from inside the funstion's loop. #/bin/ksh... (5 Replies)
Discussion started by: mohtashims
5 Replies

2. Programming

Looping an array of 2d arrays in C

Le sigh... Hopefully this will be the last time I have to ask for help on this topic. For a while now I've been working with a 1d array that holds 2d arrays. For reference you can view here. Now I'm just trying to loop through the elements with the following: #include <stdio.h> void... (3 Replies)
Discussion started by: Azrael
3 Replies

3. UNIX for Dummies Questions & Answers

Looping through the contents of array for output name

Hi all, I am trying to loop through the string contents of an array, to add it during the saving of the output files. I am trying this code to print each column and save it to unique file name, but it doesn't work. Thanks for any help. fnam=(japan usa uk) alldata.dat contained sample data... (1 Reply)
Discussion started by: ida1215
1 Replies

4. UNIX and Linux Applications

Array in Ksh

Hi Guys, My code is something like this set -A A1 1 7 13 19 set -A A2 2 8 14 20 set -A A3 3 9 15 21 echo "Enter a number" read number for i in 0 2 3 4 do if }" ] then do something elif }" ] then do something elif }" ] then do something (4 Replies)
Discussion started by: jeanzibbin
4 Replies

5. Shell Programming and Scripting

Array in Ksh Shell script

hi team, i have a file, which contains only variable and its value param.ksh --------- export A=123 export B=345 export C=567 export D=OPLI export E=OL89PO From shell script, i am invoking this file and use the value of this variable. Now there are 5 variable in above file. Before i... (1 Reply)
Discussion started by: ace_friends22
1 Replies

6. Shell Programming and Scripting

Passing array to functions in ksh script

Let me know if there is a way to pass array to a funtion in ksh script. function isPresent { typeset member member=$1 dbList=$2 echo '$1:' $1 echo '$2' $dbList The array will be at the second position....something like this isPresent 12 <array> if then echo... (3 Replies)
Discussion started by: prasperl
3 Replies

7. UNIX for Dummies Questions & Answers

Help in Array looping and creating multiple lines

hi Gurus, I'm a newbie in scripting please check my script if this is correct. I think there's something wrong with it but I;m not sure. I'm trying to create multiple lines using awk from external xml files but i want to add additonal info in the data manually Since i don't knwo how to... (0 Replies)
Discussion started by: sexyTrojan
0 Replies

8. Shell Programming and Scripting

using array in ksh

hi all, need help with putting names in an array, i have a few servers which i look up by doing a 'find . -name "*.pid' and the format of the output is like following : ./servername/myserver.pid i was wondering how can i iterate through and store each name in one array my code is... (1 Reply)
Discussion started by: cesarNZ
1 Replies

9. Shell Programming and Scripting

looping a array inside inside ssh is not working, pls help

set -A arr a1 a2 a3 a4 # START ssh -xq $Server1 -l $Username /usr/bin/ksh <<-EOS integer j=0 for loop in ${arr} do printf "array - ${arr}\n" (( j = j + 1 )) j=`expr j+1` done EOS # END ========= this is not giving me correct output. I... (5 Replies)
Discussion started by: reldb
5 Replies

10. Shell Programming and Scripting

Perl question - looping through an array of hashrefs

I have an array of hashrefs that look like the following: my @LAYOUT = ( {SQL_1 => "select count (*) FROM prospect WHERE PROCESS_DATE = To_date('INSERT_DATE_HERE', 'mm/dd/yyyy') and tiff_filename is not null ... (2 Replies)
Discussion started by: kregh99
2 Replies
Login or Register to Ask a Question