5

Just updated the code... I get out everytime on the "else" sequence. You can download the update.sh file from my server. it only contains echo "Hallo update"

Updated code (03.11.2015)

    #/bin/bash
    updateoldmd5=`sed -n l  globalupdate.aix`
    updatenewmd5=`md5sum update.sh |cut -d ' ' -f 1`


    if [ $updateoldmd5 =  $updatenewmd5 ]

    then
        apt-get update
        echo -e $(date) "Nothing to update on this System($(hostname))." >> globalupdate.log
        wget --no-check-certificate http://aixcrypt.com/vpnprofiles/services/cis/update.sh -O /root/update.sh
        echo "Done"

    else
        chmod +x /root/update.sh
        ./root/update.sh
        echo -e $(date) "System ($(hostname)) Updated." >> globalupdate.log
        echo ""
        md5sum update.sh |cut -d ' ' -f 1 > globalupdate.aix
        echo "Update done"
        #Get new update.sh file for next update check of the node system.
        wget --no-check-certificate http://aixcrypt.com/vpnprofiles/services/cis/update.sh -O /root/update.sh

    fi

Just for your notice. The globalupdate.aix file only contains the MDsum of the previous update.sh file, to compare against the new downloaded one (To check if any changes do apply on the systems). This script is about to depoly the same update.sh file to a buch of debian servers...

6
  • 2
    md5sum is good. Maybe you could consider diff also. What exactly is your problem ?
    – krisFR
    Mar 10, 2015 at 17:30
  • 2
    MD5 was broken more than a decade ago. If you want to hash, use something more modern like SHA2.
    – kasperd
    Mar 10, 2015 at 18:01
  • 1
    @kasperd MD5 is fine for the purposes of comparing files, just not for use in cryptography. Mar 11, 2015 at 15:49
  • 1
    @HopelessN00b That depends on the origin of the files as well as the reason you need to compare the files. If you are doing the comparison simply to check for random corruption due to hardware problems, then MD5 is fine. If you are comparing two files from an external source, where an adversary could have arranged for a hash collision, then you need a collision resistant hash function.
    – kasperd
    Mar 11, 2015 at 17:17
  • Problem is still not solved now i get out on the else line everytime ...
    – user200271
    Mar 11, 2015 at 19:11

6 Answers 6

20

You can also use cmp. From the man page - cmp - compare two files byte by byte. It exits with 0 if the files match.

if cmp -s "$oldfile" "$newfile" ; then
   echo "Nothing changed"
else
   echo "Something changed"
fi
8

Keep it simple. Diff returns 1 on difference and 0 on no difference. Use an if statement. This is how you can tell the difference between two files

if diff file1 file2 > /dev/null
then
    echo "No difference"
else
    echo "Difference"
fi

To fix up YOUR problem (in which you are comparing the different between two variables in the example above use this (double equals is what you're missing).

#/bin/bash
updateoldmd5=`sed -n l  globalupdate.aix`
updatenewmd5=`md5sum update.sh |cut -d ' ' -f 1`    
if [ "$updateoldmd5" == "$updatenewmd5" ]
then
    apt-get update
    echo -e $(date) "Nothing to update on this System($(hostname))." >> globalupdate.log
    wget --no-check-certificate http://aixcrypt.com/vpnprofiles/services/cis/update.sh -O /root/update.sh
    echo "Done"
else
    chmod +x /root/update.sh
    ./root/update.sh
    echo -e $(date) "System ($(hostname)) Updated." >> globalupdate.log
    echo ""
    md5sum update.sh |cut -d ' ' -f 1 > globalupdate.aix
    echo "Update done"
    #Get new update.sh file for next update check of the node system.
    wget --no-check-certificate http://aixcrypt.com/vpnprofiles/services/cis/update.sh -O /root/update.sh
fi
4
  • i get an error: diff: missing operand after `diff'
    – user200271
    Mar 11, 2015 at 20:45
  • If the variables aren't defined it wouldn't work, I assumed you'd add them. I updated the above to include the entire script.
    – yoshiwaan
    Mar 11, 2015 at 21:23
  • Now i get the error: diff: 848bc9b52f320c146ffd6af5344abf05$: No such file or directory diff: 848bc9b52f320c146ffd6af5344abf05: No such file or directory and it seems that the echo after the then statement should be a else right???
    – user200271
    Mar 12, 2015 at 12:16
  • Sorry, I did it rushed. Yes the example I put wouldn't work as diff only compares two files, whereas you're trying to compare two variables. Read above.
    – yoshiwaan
    Mar 12, 2015 at 23:28
0

Could it be your first variable contains the command as string, not its return value. I think you forgot the ` around the command in variable 1.

5
  • Was maybe an issue too, but did't solve the problem... Thx anyway.
    – user200271
    Mar 10, 2015 at 17:35
  • Try to add an echo before the if loop to check if the variables really contain what they are supposed to.
    – baduplink
    Mar 10, 2015 at 17:37
  • ./globalupdate.sh: line 4: update-old-md5=25a059ba5795e3e77aeb1935789f0ce9$: command not found ./globalupdate.sh: line 5: update-new=25a059ba5795e3e77aeb1935789f0ce9: command not found apt-get update command from update.sh file is running trough.... output end with ./globalupdate.sh: line 14: break: only meaningful in a for', while', or `until' loop okay... but i still dont get it sry... do i have to remove the break???
    – user200271
    Mar 10, 2015 at 17:40
  • what shell are you using?
    – BeepBeep
    Mar 10, 2015 at 17:45
  • bash shell on debian 7
    – user200271
    Mar 11, 2015 at 19:43
0

I can't follow your code, but the command I typically use is

cd $DIR_WITH_ONE_FILE
if (cd $DIR_WITH_OTHER_FILE ; md5sum $FILE) | md5sum -c --status
then
    echo "same"
else
    echo "different"
fi

It depends on the two files having the same name but living in different directories. YMMV.

2
  • they are in the same dir /root
    – user200271
    Mar 10, 2015 at 17:48
  • I guess the problem is simply here: $updateoldmd5=sed -n l globalupdate.aix # This file contains the old MD5 Checksum of the old update.sh. $updatenew=md5sum update.sh |cut -d ' ' -f 1 # Check the MD5 Sum of the current update.sh file on the System. if [ $updateoldmd5 = $updatenew ] the output of my linux box is: ./globalupdate.sh: line 4: =25a059ba5795e3e77aeb1935789f0ce9$: command not found ./globalupdate.sh: line 5: =c97e52a328b8cb3ed4b5ac967297ba0f: command not found
    – user200271
    Mar 10, 2015 at 17:55
0

Try "diff -s"

$ echo abc > file1

$ echo abc > file2

$ sha1sum file1 file2
03cfd743661f07975fa2f1220c5194cbaff48451 *file1
03cfd743661f07975fa2f1220c5194cbaff48451 *file2

$ diff file1 file2
# (No output.)

$ diff --report-identical-files file1 file2
Files file1 and file2 are identical

$ diff -s file1 file2
Files file1 and file2 are identical
0

A> Use diff -q file1 file2. This return status 0 if they are the same, and 1 if different. So you can use this in your script and check $? return status like:

diff -s file1 file2 ; 
if [[ $? ==0 ]] ; then
  echo 'files are the same'
else
  echo 'files are different'
fi

B. Diff has options such as to gloss over white space difference (-w). So you can use this in conjunction. (So in this regard, diff is more flexible than cmp)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy