Endless Loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Endless Loop
# 1  
Old 06-06-2008
Endless Loop

Hi,
I'm pretty new to UNIX shell scripting and need some help. We have an Informatica interface that dumps any files that have errors into a directory. I need to check that directory periodically for any of up to 9 files that might be in it and run a specific process for each file found. The directory could (and hopefully would be) empty.

I've gotten some help from some of the folks here and have worked out some of the initial problems, but it goes into an endless loop and I have to stop it. I'm missing something.

Any help would be greatly appreciated.

Here is the test code without the actual processes:
Code:
#!/bin/ksh

ls /home/inform/LeanLogistic/bkp | while read filename
echo $filename
do
echo 'did do statement'

	if [ "$filename" = "CX_ORDER_LOAD_SHIPMENTexport.csv" ]
       then
       /usr/bin/echo "CX_ORDER_LOAD_SHIPMENT file." 
	elif [ "$filename" = "CX_CHARGEexport.csv" ]
	then 
	  /usr/bin/echo "CX_CHARGE file." 
	elif [ "$filename" = "CX_LOAD_HISTORYexport.csv" ]
	then
	  /usr/bin/echo "CX_LOAD_HISTORY file." 
	elif [ "$filename" = "CX_DEDICATED_EQUIPMENTexport.csv" ]
	then
	  /usr/bin/echo "CX_DEDICATED_EQUIPMENT file." 
	elif [ "$filename" = "CX_LOAD_NOTEexport.csv" ]
	then
	  /usr/bin/echo "CX_LOAD_NOTE file."
	elif [ "$filename" = "CX_PRODUCT_INFOexport.csv" ]
	then
	  /usr/bin/echo "CX_PRODUCT_INFO file." 
	elif [ "$filename" = "CX_RATE_CHANGE_REQUESTexport.csv" ]
	then
	  /usr/bin/echo "CX_RATE_CHANGE_REQUEST file."
	elif [ "$filename" = "CX_SERVICE_REQUESTexport.csv" ]
	then
	  /usr/bin/echo "CX_SERVICE_REQUEST file." 
	elif [ "$filename" = "CX_SERVICE_FAILUREexport.csv" ]
	then
	  /usr/bin/echo "CX_SERVICE_FAILURE file." 
	fi
echo 'did if statement'
done

Below is the output it produces when all 9 files are in the directory. It loops through and reads all the files, but then just keeps looping.

CX_CHARGEexport.csv
did do statement
CX_CHARGE file.
did if statement
CX_DEDICATED_EQUIPMENTexport.csv
did do statement
CX_DEDICATED_EQUIPMENT file.
did if statement
CX_LOAD_HISTORYexport.csv
did do statement
CX_LOAD_HISTORY file.
did if statement
CX_LOAD_NOTEexport.csv
did do statement
CX_LOAD_NOTE file.
did if statement
CX_ORDER_LOAD_SHIPMENTexport.csv
did do statement
CX_ORDER_LOAD_SHIPMENT file.
did if statement
CX_PRODUCT_INFOexport.csv
did do statement
CX_PRODUCT_INFO file.
did if statement
CX_RATE_CHANGE_REQUESTexport.csv
did do statement
CX_RATE_CHANGE_REQUEST file.
did if statement
CX_SERVICE_FAILUREexport.csv
did do statement
CX_SERVICE_FAILURE file.
did if statement
CX_SERVICE_REQUESTexport.csv
did do statement
CX_SERVICE_REQUEST file.
did if statement

did do statement
did if statement

did do statement
did if statement

did do statement
did if statement

did do statement
did if statement

did do statement
did if statement
# 2  
Old 06-06-2008
Hammer & Screwdriver Is there something else in the directory?

Your
Code:
ls /home/inform/LeanLogistic/bkp | while read filename

will read through all files in that directory folder. So, if that commands retrieves more than nine filenames, you will get more than nine results.
Perhaps try
Code:
ls /home/inform/LeanLogistic/bkp/CX* | while read filename

to limit the number of found files.
# 3  
Old 06-09-2008
The echo $filename before the do statement causes the while to examine the exit code of the echo, which is always true. You presumably want to move it after the do
# 4  
Old 06-16-2008
Computer Thanks

Hi era,
Just wanted to thank you for the help on the Endless Loop problem I posted a week or so ago. Your solution:

"The echo $filename before the do statement causes the while to examine the exit code of the echo, which is always true. You presumably want to move it after the do"

SOLVED the problem. Thanks so much.

Jeff R
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Help with accidental endless loop

I was practicing writing simple loops as I am a new bash user and I created this script, which turned out to be an endless loop where the echo output does not stop and I do not see where my mistake is. #!/bin/bash echo 'enter a number from 1 to 100' read number while do ... (2 Replies)
Discussion started by: goldenlinx
2 Replies

2. Shell Programming and Scripting

Script runs in endless loop

Hi, AM very new to shell scripting and try to run a simple do while loop statement, but it ends up running endlessly. please can anyone assist, dunno what am doing wrong, any useful suggestions will be welcomed. #!/bin/ksh ### To check a running process instance #################... (5 Replies)
Discussion started by: bayoo
5 Replies

3. Shell Programming and Scripting

awk loop using array:wish to store array values from loop for use outside loop

Here's my code: awk -F '' 'NR==FNR { if (/time/ && $5>10) A=$2" "$3":"$4":"($5-01) else if (/time/ && $5<01) A=$2" "$3":"$4-01":"(59-$5) else if (/time/ && $5<=10) A=$2" "$3":"$4":0"($5-01) else if (/close/) { B=0 n1=n2; ... (2 Replies)
Discussion started by: klane
2 Replies

4. Shell Programming and Scripting

[Solved] Endless while loop when compare files

Hi All, I've written a script to read 2 files and compare the contents using while loop but somehow when $line is not found in test2, the script will continue looping. Below is my code, pls advise what could went wrong TIA Nick for line in test1.txt | while read line do grep -i... (4 Replies)
Discussion started by: Nick1971
4 Replies

5. Shell Programming and Scripting

KSH - Issue with endless loop.

First time post. I did a search so I didn’t see this specific issue. It seems to be a head scratcher for me. I have an hourly job that on rare occasions, gets into an endless loop. I’ve tried different scenarios but the current version does basically the following. Find all the *.arc files and... (18 Replies)
Discussion started by: Sylvan303
18 Replies

6. Shell Programming and Scripting

Preventing an endless loop with recursive grep

When finding a string in files within a directory, one can use this: grep -r "searchstring" dir/subdir/ > listofoccurrences.txt For brevity sake one can enter the intended directory and use this: grep -r "searchstring" . > listofoccurrences.txt which as I found out leads to an endless loop,... (2 Replies)
Discussion started by: figaro
2 Replies

7. Shell Programming and Scripting

[PHP] endless loop mimics a cron. Make sure only one instance is running

Hi, PHP user here. I'm using an endless loop to perform to mimic a cron. The script does something every 20 minutes. It sleep()s in the meantime. I have various checks that ensure that only instance can run, including a "gentleman agreement" locked file. However, I'd like to make sure... (2 Replies)
Discussion started by: jjshell
2 Replies

8. Shell Programming and Scripting

calculating endless columns

I have about 5000 columns of data that i need to convert all of it into pecentages. for shorter colums i have been using this code: {print $1/($1+$2)*100,$2/($1+$2),$3/($3+$4)*100 .....} but this is a teadious process... is there anyway to do it without having to write all of them out? sample... (20 Replies)
Discussion started by: chronicx
20 Replies

9. HP-UX

printer spewing out endless copies

Hi I've set up a remote IP-addressed printer using lpadmin on a hp-ux server. It is - as far as I can tell - identical to another printer on the system of the same model type. The default printer works fine with the lp command; the new printer sends endless copies and the job has to be cancelled... (2 Replies)
Discussion started by: ewans
2 Replies

10. Shell Programming and Scripting

Endless loop - Fork function failed?

I need a quick script that will serve as a sort of "real time monitor" for watching some log files. I am using Bourne shell in HP-UX 10.20. I have basically created a script that never ends, unless of course I manually terminate it. Here's the script (it's called qhistory): clear echo "REAL... (3 Replies)
Discussion started by: cdunavent
3 Replies
Login or Register to Ask a Question