Find the answer to your Linux question:
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 ...
  1. #1
    Banned
    Join Date
    Jun 2011
    Location
    Malaysia
    Posts
    5

    Any error for this code?

    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
    it keeps saying "unary operator expected"
    what's the problem?

  2. #2
    Linux 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)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...