Terminal Friday: Patch and Diff

Two of the essential tools in software development are Patch y Diff. It is not a mystery how they work, but I think this will be an interesting post. 🙂

Both are very powerful, and this is just like touching the surface, they have more utilities and other functions. Basically with these two tools we can create version control,


Diff

We refer to comparison, it compares an "original" file with the "new" one, and informs us of the differences that exist between them. This tool also allows us to create .patch files that we use to create patches for our programs.


Patch

It is the command with which we literally "patch" our original file, adding and / or removing lines according to the instructions contained in the .patch file


There is also vimdiff, which is a visual tool to apply patches without the need for a .patch file, since it compares "original" and "new" and on the same file it is possible to edit line by line or the entire document. This I won't explain but I think it deserves a mention.


Example

Now the fun is. Example!

This is the case, we have a great script that asks for your name and your age, if you are over 18 it tells you that you can vote, otherwise it tells you that you cannot vote.

original.sh

#! / bin / bash echo "Enter your name:" read name echo "Enter your age:" read age if [[18 -lt $ age]] then echo "Hello $ name, you are $ old and you can vote!" else echo "Hello $ name, you are $ old and cannot vote ..." fi
Image of the code in Vim

Image of the code in Vim

Done, here is the running script:

Everything seems to be working fine

Everything seems to be working fine

So, as good users we are, we share our script with a friend :), but we get a message saying that it has a flaw, that when it is 18 it says that it cannot vote when it should.

Logic error :(

Logic error 🙁

Now we begin to correct the small error and make a few modifications ...

new.sh

#! / bin / bash maxAge = 18 echo "Enter your name:"; read name echo "Write your age:"; read age if [[$ maxAge -le $ age]]; then echo "Hello $ name, you are $ years old and you can vote!" else echo "Hello $ name, you are $ years old and you cannot vote ..." fi exit 0
New code written in Vim

New code written in Vim

Suppose the script is very heavy. So, in order not to send the whole script again, we create a .patch 😀

$ diff -u original.sh new.sh> patch.patch

And now we have our patch. Here's a view on Vim:

This is what the instructions for a .patch look like. In white the lines that were not modified, in blue those removed, in orange those added.

This is what the instructions for a .patch look like. In white the lines that were not modified, in blue those removed, in orange those added.

And to apply it we simply use the .patch file in the script to be patched. Here is called originalAmigo.sh, which is an exact copy of the script original.sh

Friend script

Friend script

$ patch originalFriend.sh <patch.patch

And this leaves us our file originalAmigo.sh So:

Friend script after applying patch

Friend script after applying patch

As you can see it is very easy to get the diffs and apply the patches. It's all from me.

Greetings people, we will read next Friday.


Leave a Comment

Your email address will not be published. Required fields are marked with *

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   Xerix said

    Great, thank you very much 🙂

  2.   angel blade said

    If you want some color, please use colordiff ^ __ ^

  3.   eliotime3000 said

    Now I understand how patches work in Debian.

  4.   fer_pflores said

    Hello, I know that system notifications can be shown with notify-send from the console, but what I would like to do is to be able to schedule what time or how long to show me a notification, is there any way to do it? I use elementary, which is based on Ubuntu 12.04, in case it helps, thanks

    1.    elav said

      Well, it can be done using the system cron 😉

      1.    fer_pflores said

        and how can that be done? I just learned how to use the notify-send command

        1.    wada said

          You can search for cron here in the blog there are many posts about it 🙂

  5.   Joaquin said

    Very good, thank you!

  6.   dhunter said

    I always use this to update the kernel, I download only the patches and apply to the sources, so I don't have to download 80mb each release.