Loops within loops


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Loops within loops
# 1  
Old 04-06-2005
Loops within loops

I am running on HPUX using ksh.

I have a script that uses a loop within a loop, for some reason the script seems to hang on a particuliar record. The record is fine and hits the condition in Blue. If I kill the 1st loop process the script continues on with no problem.
Code:
Begin code>

<Some other code>

cat $fileloc2 | while read line
do
  npanxx=`echo $line |cut -c1-6`
  ocn=`echo $line | cut -c10-13`
  brange=`echo $line |cut -c14-17`
  erange=`echo $line |cut -c18-21` 
  revnpa=`echo $npanxx|sed '/\n/!G;s/\(.\)\(.*\n\)/&\2\1/;//D;s/.//'`
  cat $ocnfile |cut -c1-4| while read input
  do 
    if [ $ocn = $input ]
     then
       if [ $pnpanxx != $npanxx ] and [ $frange != T ]
         then
            pnpanxx=$npanxx
            
            if [ $brange = 0000 ]
              then
                if [ $erange = 9999 ]
                  then
                    frange=T
                    sed 's/NPANXX/'$npanxx'/g;s/XXNAPN/'$revnpa'/g;s/Y/I/g' $hfile >> $tempfile
                   else
                     frange=F
                     erange=`expr $erange + 1`
                     sed '0001,'$erange'!d' $hfile > $temp
                     sed 's/NPANXX/'$npanxx'/g;s/XXNAPN/'$revnpa'/g;s/Y/I/g' $temp >> $tempfile
                     rm $temp
                 fi
             else
               frange=F
               brange=`expr $brange + 1`
               erange=`expr $erange + 1`
               sed ''$brange','$erange'!d' $hfile > $temp
               sed 's/NPANXX/'$npanxx'/g;s/XXNAPN/'$revnpa'/g;s/Y/I/g' $temp >> $tempfile
               rm $temp
          fi
      fi
   fi
 done
done

Edited -- Added code tags for readability.

Last edited by google; 04-06-2005 at 04:31 PM..
# 2  
Old 04-06-2005
Sorry my script had tabs and it didn't retain them when I pasted in. I hope it is not too hard to read.
# 3  
Old 04-06-2005
You can't do stuff like: if [ $pnpanxx != $npanxx ] and [ $frange != T ]

Or at least it won't do what I'm guessing you think it might do. Try this:
if [ 1 = 1 ] and [ 99999 = 1 ] ; then
echo yes
fi

I don't understand how you can "kill the first loop process" and have it continue. Smilie
# 4  
Old 04-06-2005
i'm not sure about this one ...
Code:
sed ''$brange','$erange'!d' $hfile > $temp

you might want to check the size of $temp --- it's probably 0 bytes ... i'm guessing that you're killing the sed process in this loop when it hangs because $brange and $erange seem to be within single quotes and are unusable to sed ...
# 5  
Old 04-07-2005
You might consider putting a set -x at the top of the script
and then running it. This would let you debug the script and
find possible error(s)
# 6  
Old 04-08-2005
Quote:
Originally Posted by Just Ice
i'm not sure about this one ...
Code:
sed ''$brange','$erange'!d' $hfile > $temp

you might want to check the size of $temp --- it's probably 0 bytes ... i'm guessing that you're killing the sed process in this loop when it hangs because $brange and $erange seem to be within single quotes and are unusable to sed ...
The $hfile is a hidden file that contains static values that I use a for global replace. I have to create anywhere from 1000 to 10000 block telephone numbers. the vales 0000 - 9999 never change. so I use the sed statement to pull the block out that I need brange = begin range erange = end range. Then I use a global replace on the static values. Lot faster than doing +1 especially if you have to create close to 21 mil records.
# 7  
Old 04-08-2005
Quote:
Originally Posted by Perderabo
You can't do stuff like: if [ $pnpanxx != $npanxx ] and [ $frange != T ]

Or at least it won't do what I'm guessing you think it might do. Try this:
if [ 1 = 1 ] and [ 99999 = 1 ] ; then
echo yes
fi

I don't understand how you can "kill the first loop process" and have it continue. Smilie
This simply strips out duplicates. The table I am parsing has ranges in it
312 314 0000-9999
sometimes the next record would have the same value in it but a smaller range which has already been built.

312 314 0000-9999
312 314 1000-1999

pnpanxx = previous npanxx in file, npanxx = current one I am looking at

I set the frange value to T or F based on if it is a full range or a partial 10k block
312 314 0000-1999
312 314 5000-5999
would set the frange to F


When I do a ps -ef I kill the process that shows my

cat $fileloc2 |while read line
The script continues on at that point, but not where it left off. Which seems like the cat $fileloc is still working.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Need help with for loops

Why wont my for statements work? Im trying to get this script to swich to a user an if you put in a start/stop/or restart paramater to do just that for each user. I commented out the actual start/stop actions to test it just by using echos and not do anything hasty in the environment but it... (0 Replies)
Discussion started by: LilyClaro
0 Replies

2. UNIX for Advanced & Expert Users

Help with loops?

I'm trying to understand better the while and until loops, can someone help me with this example? #!/bin/bash # Listing the planets. for planet in Mercury Venus Earth Mars Jupiter Saturn Uranus Neptune Pluto do echo $planet # Each planet on a separate line. done echo; echo for... (3 Replies)
Discussion started by: jose2802
3 Replies

3. UNIX for Dummies Questions & Answers

loops with tr

Hello, I'm not sure if this is more appropriate for the 'unix for dummies' or the 'unix for experts' forum because I'm new to this forum and this is the second topic I've discussed, but if you could let me know which one was more appropriate for something like this, please do! So in tr (an... (2 Replies)
Discussion started by: juliette salexa
2 Replies

4. Shell Programming and Scripting

Loops

Hi All, I want to execute a script the number of times a user enters. Please can you advise on hor can I do the same. Many Thanks, Shazin (4 Replies)
Discussion started by: Shazin
4 Replies

5. Shell Programming and Scripting

Help with the 2 for loops

#!/bin/bash IFS=$'\n' A= a c b t g j i e d B= t y u i o p counter=0 found="" for i in $(cat $A) do for j in $(cat $B) do if then found="yes" fi done if then (1 Reply)
Discussion started by: vadharah
1 Replies

6. UNIX for Dummies Questions & Answers

Help with While Loops

I am traversing down a list, and I am not quite sure how to tell the loop to break when it's done going through the file. #!/bin/sh while : do read list <&3 echo $list done is the code. The file "list" is simply 5 4 3 2 1 any advice on how to break the loop after the file is... (1 Reply)
Discussion started by: MaestroRage
1 Replies

7. Shell Programming and Scripting

while loops

Hi I've a file like so: Now, I want to read my file and take ex. the Media ID and the Type for each groups of Media (Media1,Media2,...,Media(n): cat /tmp/file|\ while read FILE do while $(FILE|cut -d: -f1)=Media$i do #here will be some test, ex: #if Media ID < 23 ... (4 Replies)
Discussion started by: nymus7
4 Replies

8. UNIX for Dummies Questions & Answers

two loops

Hi, how can I use "for" to have two loops : this is my script : for i in (A B C) do for j in (a b c) do echo $i$j done done #End I want to print out Aa Ab Ac .... But I have error message : syntax error at line 1 : `(' unexpected Many thanks before. How should I use "for" ?? (2 Replies)
Discussion started by: big123456
2 Replies

9. UNIX for Dummies Questions & Answers

While Loops

I'm trying to create a loop that will prompt the user for 15 values, not forcing them to enter all 15. If the user enters through one or more of the prompts the null value needs to be converted to 0, otherwise set the parameter = to the value entered: ex. Please enter file no #1: 17920 ... (4 Replies)
Discussion started by: vdc
4 Replies

10. UNIX for Dummies Questions & Answers

loops?

hello....very new user to unix...and i have a question..i am not sure if there is such a thing For example...the user is asked if he likes Bananas....if he says yes.... echo You like Bananas $name at the end of the script it echos all that the user has entered so they can read it.... but... (1 Reply)
Discussion started by: jonas27
1 Replies
Login or Register to Ask a Question