Results 1 to 4 of 4
  1. #1
    Join Date
    May 2002
    Location
    Ca
    Posts
    2

    Unanswered: cshell questions

    I am *very* new to shell programming, so please be gentle.

    I am trying to write a small script using a nested foreach loop. I have 4 files which contain a list of numbers, and one file that contains the bad numbers that are contained in the above-mentioned 4 files. I would like to be able to match up (from bad number file) the bad numbers listed in the 4 files, but I'm not sure how to make the one file search the other 4...?

    I've looked in various sites and cannot seem to find anything that gives me a hint...so any help would be appreciated. Here's a rough of what I am trying to construct:

    foreach i ( file1 file2 file3 file4 )
    foreach j ( ~/badnum )

    ...and then I'm not sure...I'm thinking tho, that in my "foreach j" I may need to "cat badnum"?

    Thanks in advance

    nicomen

  2. #2
    Join Date
    Feb 2002
    Posts
    2,232
    You are on the right track:

    something like the following:
    foreach badnum (`cat badnumfile`)

    next you can do a number of tests -- do a grep against the 4 files to test that particular bad number, redirect the output to a file and test to see if the file is non-zero in length -- you can use a foreach of the filenames to do this, so you would have nested foreach loops. You can cat the values out from the 4 files and use an if statement to do a comparison (this method would be more time consuming).

    Good luck.

  3. #3
    Join Date
    May 2002
    Location
    Ca
    Posts
    2
    Thank you...I appreciate your response. Although I am no closer to completing the script than I was before, your comments supported my plan of attack.

    nicomen

  4. #4
    Join Date
    Aug 2002
    Location
    UK
    Posts
    87
    Something like this ??

    Code:
    #!/bin/sh
    
    integer check
    badnum=`cat badnumfile`
    
    for File in ( file1 file2 file3 file4 ) ; do
     check=`grep -i ${badnum} ${File}
    
     if [ ${check} -eq 0 ] ; then
        echo "Bad nums found in file " ${File}
     fi
    
    done

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •