1830244 Members
1901 Online
110000 Solutions
New Discussion

Checking file contents

 
SOLVED
Go to solution
Chern Jian Leaw
Regular Advisor

Checking file contents

HI

I have the following 2 files, which is the form:
#cat f1
/fs12/nwd.cr.24
/fs12/nwd.des.mem
/fs13/nwd.fclk.45
/fs14/nwd.cr.256
/fs37/nwd.srtl.pv

#cat f2
/fs12/nwd.cr.24
/fs12/nwd.des.mem
/fs11/nwd.shark.pv
/fs36/nwd.srtl.rtl
/fs37/nwd.srtl.pv

whereby files f1 and f2 contains lists of filesystems residing on 2 remote machines, A and B respectively.

I was wondering, how do I read the contents of these files, and place them into 2 seperate arrays in Bourne/C-shell?

I would like to compare every index of the 2 arrays to see if they are similar. If similar, then I would like to rcp them from machine A into machine B, else do nothing.

Can someone help me out in this matter?

Thanks.




4 REPLIES 4
Peter Kloetgen
Esteemed Contributor

Re: Checking file contents

Hi Chern,

you can put the output of any command as value into a variable using command substitution:

array[0]=`cat f1`
array[1]=`cat f2`

After this, you can compare the output of your cat- command or whatever you want to do.

Allways stay on the bright side of life!

Peter
I'm learning here as well as helping
Chern Jian Leaw
Regular Advisor

Re: Checking file contents

Peter,

Does the statement array[0]='cat f1' places the entire entries/contents of the file f1 into array[0]?

So how do I compare each of the contents/entries of file f2, which is placed into array[1]?

I would like to have 2 seperate arrays, each holding the entries of files f1 and f2 respectively.

Thanks.
Peter Kloetgen
Esteemed Contributor

Re: Checking file contents

Hi Chern,

please use backticks for the command substitution to get it work. Then the answer is YES, the output of your cat- command ist put into an array. If you want to put single lines into variables, you need a loop construction for this. The awk- command can help you here.

Allways stay on the bright side of life!

Peter
I'm learning here as well as helping
Justo Exposito
Esteemed Contributor
Solution

Re: Checking file contents

Hi Chern,

Try this
integer num=0
for i in `cat f1`
do
array[$num]=$i
(( num = $num + 1 ))
done
integer num2=0
for i in `cat f2`
do
array2[$num2]=$i
(( num2 = $num2 + 1 ))
done
sw=ok
integer F=0
for i in ${array[@]}
do
if [ ${array[$F]} = ${array2[$F]} ]
then
ok=ok
else
ok=nook
exit;
fi
(( F = $F + 1 ))
done

if [ $ok = "ok" ]
then
echo " are the same copying"
integer F1=0
for i in ${array[@]}
do
rcp hosta:${array[$F1]} hostb:${array2[$F1]}
(( F1 = $F1 + 1 ))
done
fi

Hope this helps

Justo.
Help is a Beatiful word