PDA

View Full Version : cshell questions


nicomen
05-11-02, 13:51
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

rnealejr
05-12-02, 13:39
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.

nicomen
05-13-02, 15:18
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

WingMan
08-20-02, 05:34
Something like this ??


#!/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