The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com




View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #1 (permalink)  
Old 11-16-2008
dawn_1030 dawn_1030 is offline
Registered User
  
 

Join Date: Nov 2008
Posts: 2
Double while loop problem

Hi all, i have encountered a strange problem with the double while loop. The aim of the two while loops is to compare 2 files, tmplist.txt is the reference file with the complete list of servers. Please see the attached picture for the logic and relationship between input and output.

The 2 while loops should search the servers in /test.csv in the /tmplist.txt, line by line. For example, search first item in /test.csv server2 in /tmplist.txt, starting from first line in /tmplist.txt (LINENUM=1), if not found then output a 'NA' and then go to next line in /tmplist.txt until it finds the matching server2, and update LINENUM at the same time. Else will proceed to search the next server server3 in /tmplist.txt from the current line onwards (since all servers are always listed in alphabatic order).

The variable LINENUM (indicator of line number) used in the inner while loop. It is predefined as 1, after the if condition if [ $SERVER = $REFS ] is true, LINENUM will increment and then break out the inner while loop and continue in the outer while loop. However, in the outer while loop, every time it loops and reads SERVER COUNT, the LINENUM starts from the initial value 1 again instead of from the incremented LINENUM coming from the inner while loop.


Below shows part of the program with the details of 2 while loops.
Could anyone help to explain to me what is wrong here? This is my first UNIX assignment, will really appreciate your help!!


#!/bin/bash
TYPE=P2PS
LINENUM=1

while read SERVER COUNT; do

cat tmplist.txt | while read LINE; do

REFS=$(head -$LINENUM tmplist.txt | tail -1)

if [ $SERVER = $REFS ]; then
let LINENUM++
echo -n ","$COUNT >>t.csv
break
else
let LINENUM++
echo -n ",NA" >> t.csv
continue
fi
done

done < test.csv
Attached Images
File Type: bmp untitled.bmp (177.0 KB, 13 views)