Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 14
Like Tree2Likes
Hi All, This is my first post in this forum and I am very new to shell scripting. I have a small requirement wherein I have a file like ABC.CON ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Mar 2013
    Posts
    6

    Comparing the contents of a file in shell script


    Hi All,

    This is my first post in this forum and I am very new to shell scripting.

    I have a small requirement wherein I have a file like ABC.CON and the file contents changes everyday.I want to zip the file only if the contents of a file is changed from the previous day.

    Note:File name never changes,it gets overwritten daily.

    Any help is really appreciated.

    Thanks in advance!!

  2. #2
    Trusted Penguin Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    3,439
    Hi and welcome

    That is quite straight forward.
    I would create a md5 checksum of the file.
    If there is no md5 hecksum file or one with a different checksum --> write the new checksum to a file and zip ABC.CON
    Last edited by Irithori; 03-26-2013 at 07:13 PM.
    You must always face the curtain with a bow.

  3. #3
    Just Joined!
    Join Date
    Mar 2013
    Posts
    6
    Quote Originally Posted by Irithori View Post
    Hi and welcome

    That is quite straight forward.
    I would create a md5 checksum of the file.
    If there is no or a different md5 checksum file --> write the new checksum to a file and zip ABC.CON
    Thanks for your reply,can you explain with a short example so that it's too clear for me.

    Thanks in advance!!

  4. $spacer_open
    $spacer_close
  5. #4
    Trusted Penguin Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    3,439
    Something like this should do.
    Treat it as a skeleton. One could add some error handling or configurability.
    Also there is room for optimization, as ABC.CON is read up to three times.
    Or maybe use a gzip, that can make use of multiple cpu cores.

    Code:
    #!/usr/bin/env bash
    
    DATAFILE="ABC.CON"
    DATA_CHECKSUMFILE="ABC.CON.previous_md5"
    
    if ! md5sum -c $DATA_CHECKSUMFILE &>/dev/null ; then 
      md5sum $DATAFILE > $DATA_CHECKSUMFILE
      gzip -9 $DATAFILE
    fi
    sandy0594 likes this.
    You must always face the curtain with a bow.

  6. #5
    Just Joined!
    Join Date
    Mar 2013
    Posts
    6
    How can I get the previous_md5?

    For comparison sake you added a file DATA_CHECKSUMFILE?

    Sorry for asking too many questions,I am not comfortable with shell programming.

    Thanks for your help!!

  7. #6
    Penguin of trust elija's Avatar
    Join Date
    Jul 2004
    Location
    Either at home or at work or down the pub
    Posts
    3,649
    Quote Originally Posted by sandy0594 View Post
    How can I get the previous_md5?
    Not sure what you mean here but in the script given the line
    Code:
    md5sum $DATAFILE > $DATA_CHECKSUMFILE
    saves the md5sum and the file name into the file defined in the DATA_CHECKSUM variable and the code
    Code:
    md5sum -c $DATA_CHECKSUMFILE
    reads it from the file and passes them into md5sum
    "I used to be with it, then they changed what it was.
    Now what was it isn't it, and what is it is weird and scary to me.
    It'll happen to you too."

    Grandpa Simpson



    The Fifth Continent

  8. #7
    Just Joined!
    Join Date
    Mar 2013
    Posts
    6
    My doubt is from the below line

    DATAFILE="ABC.CON"
    DATA_CHECKSUMFILE="ABC.CON.previous_md5"
    DATA_FILE is the file which I want to check the md5,then what about DATA_CHECKSUMFILE?

  9. #8
    Trusted Penguin Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    3,439
    The $DATA_CHECKSUMFILE contains the md5 checksum of the previous run of the script.
    And should the actual and previous checksum be different (aka: a new ABC.CON), then the new one gets written into $DATA_CHECKSUMFILE

    If there should be no previous checksumfile, then the if-conditions returns true, which means a checksum file and a gzipped datafile get created.
    You must always face the curtain with a bow.

  10. #9
    Just Joined!
    Join Date
    Mar 2013
    Posts
    6
    Understood now,Thanks for your time and replies.


  11. #10
    Just Joined!
    Join Date
    Mar 2013
    Posts
    6
    Hi All,

    Can someone explain me the below lines of code

    Code:
    if ! md5sum -c $DATA_CHECKSUMFILE &>/dev/null ; then 
      md5sum $DATAFILE > $DATA_CHECKSUMFILE
      gzip -9 $DATAFILE
    fi
    md5sum $DATAFILE > $DATA_CHECKSUMFILE
    What I interpret from above line is we get the md5 of DATAFILE and sending it to DATA_CHECKSUMFILE.

    But I don't understand how comparison(checking the md5 of the file which is in system with the new one) happens in the above statement that too in If condition.

    Any help is appreciated.

    Thanks in advance!!

Page 1 of 2 1 2 LastLast

Posting Permissions

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