[Solved] Endless while loop when compare files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [Solved] Endless while loop when compare files
# 1  
Old 09-20-2011
[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
Code:
for line in test1.txt | while read line
do
   grep -i $line test2.txt >/dev/null
   exitcode=$?
   if [ $exitcode = 0 ]; then
      echo "$line : String Exists"
   else
      echo "String does not exists"
   fi


Last edited by vbe; 09-20-2011 at 01:17 PM.. Reason: please use code tags for code and data
# 2  
Old 09-20-2011
missing the done or is it a bad copy paste?
# 3  
Old 09-20-2011
Why do you not use:
Code:
while read line
do
   grep -i $line test2.txt >/dev/null
   exitcode=$?
   if [ $exitcode = 0 ]; then
      echo "$line : String Exists"
   else
      echo "String does not exists"
   fi
done < test1.txt

# 4  
Old 09-20-2011
Code:
while read line
do
   grep -i $line test2.txt >/dev/null
   exitcode=$?
   if [ $exitcode -eq 0 ]; then
      echo "$line : String Exists"
   else
      echo "String does not exists"
   fi
done < test1.txt

--ahamed

---------- Post updated at 09:31 AM ---------- Previous update was at 09:31 AM ----------

oops... Smilie
vbe already posted!

--ahamed
# 5  
Old 09-20-2011
Hi,

tried to amend the code but loop still endless :
Code:
while read line 
        do 
        grep "$line" test2.txt >> /dev/null 

        if [ $? = 1 ]; then 
                /usr/bin/echo "No Error found" 
               else 
               /usr/bin/echo "Error found" 
              exit 2 
        fi 
done < test1.txt

---------- Post updated at 11:51 AM ---------- Previous update was at 11:40 AM ----------

Hi,

Problem fixed. It was due to other parts of the script that caused the loop to be endless.

Thanks for the help.

Nick

Last edited by Franklin52; 09-21-2011 at 03:29 AM.. Reason: Please use code tags, thank you
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

[Solved] awk compare two different columns of two files and print all from both file

Hi, I want to compare two columns from file1 with another two column of file2 and print matched and unmatched column like this File1 1 rs1 abc 3 rs4 xyz 1 rs3 stu File2 1 kkk rs1 AA 10 1 aaa rs2 DD 20 1 ccc ... (2 Replies)
Discussion started by: justinjj
2 Replies

4. Shell Programming and Scripting

[solved]compare two files

I have to files. One file contains two fields: server|userThe other files contains records with 13 fields (always 13). The fields to match on are 3 and 5, but I want to output the whole record. I have been trying this over and over with not much success, or at least inaccurate success. Here is my... (0 Replies)
Discussion started by: dagamier
0 Replies

5. Shell Programming and Scripting

[SOLVED] for loop to process files

I need to process a dirtree containing ms office files such that each file is stored as a variable and also, just the file file stem. Why? They will be using as input and output parameters for another script. For example /path/to/second_script -i filename.docx -o filename Here's what I... (1 Reply)
Discussion started by: graysky
1 Replies

6. 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

7. 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

8. 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

9. Shell Programming and Scripting

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... (3 Replies)
Discussion started by: JeffR
3 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