I want to compare 2 files with the same name in different directories.
$1 and $2 are 2 directories. I can check if there are same name files, but then i don’t know how to get the 2nd file..
cd $1
for i in `ls`
do
if [ -f $2/$i ]
then
echo "find it in another directory"
GET THE OTHER FILE IN $2, THEN COMPARE THEM
cmp -s $i THE OTHER FILE
if [ $? = 0 ]
echo "they are same"
else
echo "they are different"
fi
fi
done
Simplest problem would be spaces in the args – easy to fix, just quote $1 and $2
But I suspect the problem is that you are CDing into $1, which means $2 is invalid (if it is a relative path)
Solution1) Use absolute paths (e.g. /staff/bathpp/stuff/dir2)
Solution2) If you are expecting relative paths, then grab the current dir before jumping.
Personally I’d to some checks so it worked for both.