Results 1 to 2 of 2
Code:
if test -f $1 && test -f $2
then
d=$(diff $1 $2)
if [ $d -eq 0 ]
then
echo "File is same"
else
echo "File is different"
fi
...
- 07-02-2011 #1Banned
- Join Date
- Jun 2011
- Location
- Malaysia
- Posts
- 5
Any error for this code?
it keeps saying "unary operator expected"Code:if test -f $1 && test -f $2 then d=$(diff $1 $2) if [ $d -eq 0 ] then echo "File is same" else echo "File is different" fi fi
what's the problem?
- 07-02-2011 #2Linux Newbie
- Join Date
- Dec 2009
- Posts
- 241
Well I'm not sure myself but I guess it's the secound test that has the error.
If you use -eq (UNIX man pages : test ()) it needs both values to be integer. You may wanna try [ -z $d ].
As far as I see diff will return the difference of the files, so that will be a string or an array.
You may also consider to first check the file-size since it is a lot faster when you have big files, and you can tell if it's not the same before you need to read the whole file.
It's also possible to create Hashs (md5) of the files, and compare them if you just wanna test for same content very often. (As practiced in web-applications)


Reply With Quote