Computing.Net > Forums > Unix > why isn't this for loop working??

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 isn't this for loop working??

Reply to Message Icon

Name: Eric_1
Date: May 17, 2005 at 01:45:49 Pacific
OS: AIX
CPU/Ram: n/a
Comment:

I have this for loop...It reads a bunch of configuration files and for each file it should call the function function_process. After function_process had been executed is should read the next configuration file and call the function again and do this for all the configuration files.However, after the first execution of the function_process for the first configuration file it exits.


#Main function

for file in `find . -name "*.cnf" -exec grep -l tocode {} \; ` ;
do
tocode=`awk -F= '/tocode/ { print $2 }' $file | sed -e "s/'//g"`
stream=`awk -F= '/stream/ { print $2 }' $file | sed -e "s/'//g"`
if [[ ! -n "$stream" ]]; then
FMASK=`awk -F= '/FMASK/ { print $2 }' $file | sed -e "s/['\^]//g"`
stream=$FMASK
fi

if [[ ! -d $TESTDIR/$stream ]]; then
mkdir "$TESTDIR/$stream"

function_process $stream $tocode

fi
done

function_process
{

stream=$1
tocode=$2

function definition

}


Does anyone have an idea about how to solve this or does anyone sees what's going wrong in my
main function?

Thanks for you help in advance



Sponsored Link
Ads by Google

Response Number 1
Name: David Perry
Date: May 17, 2005 at 05:07:56 Pacific
Reply:

Define your function before you plan to use it. Enable debugging with "set -x" to help you see what is happening. Where is $testdir defined ?

function_process () {
stream=$1
tocode=$2
function definition
}

for file in `find . -name "*.cnf" -exec grep -l tocode {} \; ` ; do
tocode=`awk -F= '/tocode/ { print $2 }' $file | sed -e "s/'//g"`
stream=`awk -F= '/stream/ { print $2 }' $file | sed -e "s/'//g"`
if [ -z "$stream" ]; then
FMASK=`awk -F= '/FMASK/ { print $2 }' $file | sed -e "s/['\^]//g"`
stream=$FMASK
fi

if [ ! -d ${TESTDIR}/${stream} ]; then
mkdir ${TESTDIR}/${stream}
function_process $stream $tocode
fi
done


0

Response Number 2
Name: Eric_1
Date: May 17, 2005 at 05:48:22 Pacific
Reply:

Hi David,

Thank you for your reply.

TESTDIR is defined in an include file. When I manually delete the processed configuration file...and execute the shell scripts again...then it processes it properly. so..it just doesn't return to the for loop to get the next config file.



0

Response Number 3
Name: David Perry
Date: May 17, 2005 at 06:38:15 Pacific
Reply:

Include
set -x

at the top of your script as well as within the function. Also, if you are using ksh, you can include this after the set -x line

PS4='$0 line $LINENO: '

This should tell you where the break is happening.


0

Sponsored Link
Ads by Google
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 isn't this for loop working??

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

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

Unix - Ksh www.computing.net/answers/unix/unix-ksh/6778.html