Computing.Net > Forums > Unix > Why won't this loop???

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Why won't this loop???

Reply to Message Icon

Name: Reed
Date: September 12, 2003 at 13:04:17 Pacific
OS: SunOS 5.6
CPU/Ram: 8/4GB
Comment:

I have written a for loop to iterate through a file list in an array generated by a find command... I can only get it to make one pass but when I print the array it contains the two files that the find command was able to locate.

#!/bin/ksh


# Get current Month and Year:
RUN_DATE=$(date '+%m%y')

# Find all files in directory that are for current month and put list in array
cd /path to files/here
set -A INPUT_FILE_LIST - `find *FILE$RUN_DATE.txt`
export INPUT_FILE_LIST
echo Show what Array is holding: "${INPUT_FILE_LIST[*]}"
#Loop Through List of Files to be processed
for FILE in $INPUT_FILE_LIST ; do
APP_NAME=`echo $FILE | cut -c1-3`
echo Now Processing $FILE from $APP_NAME Application
done
# script ends



Sponsored Link
Ads by Google

Response Number 1
Name: WilliamRobertson
Date: September 14, 2003 at 15:04:08 Pacific
Reply:

You display "${INPUT_FILE_LIST[*]}" but you are only looping through $INPUT_FILE_LIST.

"for f in list; do..."
doesn't need an array, just one or more words separated by ${IFS} (default is spaces and tabs). Therefore you need something like:

for f in *$RUN_DATE.txt; do
...
done

btw, `` is the old Bourne shell expression evaluation syntax generously supported by Korn shell for backward compatibility. $() is Korn shell and IMHO way better.


0
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Why won't this loop???

Why won't this work? www.computing.net/answers/unix/why-wont-this-work/7538.html

ksh scripting - numeric vs. alpha www.computing.net/answers/unix/ksh-scripting-numeric-vs-alpha/4549.html

why isn't this for loop working?? www.computing.net/answers/unix/why-isnt-this-for-loop-working/6915.html