#
#Write a script to compare identically named files in two different directories and if they are same, copy 
#one of them in a third directory.
#
clear
echo -n "Enter first directory name :"
read d1
echo -n "Enter second directory name :"
read d2
if [ ! -d $d1 ]
then
    echo "Enter proper first directory"
elif [ ! -d $d2 ]
then
	echo "Enter proper second directory"
else
	echo -n "Enter destination directory :"
	read destination
	if [ ! -d $destination ]
	then
		$(mkdir $destination)
		echo "Destination directory : $destination created"
	fi
	$(ls $d1 > "firstdirectory.txt")
	$(ls $d2 > "seconddirectory.txt")
	count=0
	for i in `comm -12 firstdirectory.txt seconddirectory.txt`
	do
		filename=$(ls $d1/$i | tr -s " " | cut -d " " -f8)
		a=$(diff $d1/$i $d2/$i)
		if [ $? -eq 0 ]
		then
			$(cp $filename $destination)
			count=$(echo $count + 1 | bc -l)
		fi
	done
	echo "Total files copied : $count"
fi