Saturday, January 18, 2014

using diff and patch commands in linux to get original/modified files

diff and patch tools are very useful while using version controlling applications.
Here I am going to explain a simple and powerful usage of patch command.

Lets say we have a file StringOne.java
Then we modified the file and committed the changes.
Lets assume that currently we have only two files:
1. diff file(between original and modified version)
2. file after modifications

Suppose after some days I want to view the original file before modifications. Since I do not have that, its not possible to view it directly as we didnt store it anywhere.
But with "patch" command, we can get it.

File Before modifications: PatchExample.java

package com.str1;

import java.io.*;

public class StringOne {
    static public void main(String[] args) {
        System.out.println("This is String one");
    }
}

File After modifications: PatchExample_p1.java

package com.str2;

public class StringOne {
    static public void main(String[] args) {
        System.out.println("This is String one");
        System.out.println("This is the extra line that got added...");
    }
}

diff file (original to modified): diff.patch
1,3c1
< package com.str1;
<
< import java.io.*;
---
> package com.str2;
7a6
>         System.out.println("This is the extra line that got added...");


Case 1: You have original file and diff file --> You want to get the modified file:

patch -i diff.patch -o PatchExample_p1.java PatchExample.java

This will produce a required new file named "PatchExample_p1.java".

Here:
    -i : input diff file name (diff.patch)
    -o : output file name to be generated (PatchExample_p1.java)
       : input file (PatchExample.java)

Case 2: You have modified file and diff file --> You want to get the original file:

patch -R -i diff.patch -o PatchExample.java PatchExample_p1.java

This will produce a required new file named "PatchExample.java".

Here:
    -R : does the reverse patching

Wednesday, January 1, 2014

automate unix scp command in shell scripts : password less scp with expect shell command

expect is a unix shell command.
This is like a separate shell, as we use it as shebang in the shell script.

you can check the presence of this command by:
which expect

As you know, scp (secury copy) is a tool to copy files/directories from one host to another.

syntax of scp:

scp user@source_host:source_file_path user@destination_host:destination_file_path

(as you know, you can avoid corresponding user/host, if the source/destination host is localhost)

this prompts user to enter the password.

few ways to avoid entering password for scp:
1. generate public/private keys and export them to the required host
2. use expect command

you can get links for first method by google'ng. Here I am going to describe the second method.

script for password less scp with expect command:

#!/usr/bin/expect

# use the scp command
spawn scp <user>@<source_host>:<source_file_path> <user>@<destination_host>:<destination_file_path>

# use the expect to answer the prompts
# we are providing inputs to two propts
# 1. answer for "do you realy want to connect to this host: (yes/no)?"
# 2. "enter password"

expect {
  -re ".*es.*o.*" {
    exp_send "yes\r"
    exp_continue
  }
  -re ".*assword.*" {
    exp_send "<your_password>\r"
  }
}
interact



Note: Please change the following accordingly in the above script:
<user>, <source_host>, <source_file_path>, <destination_host>, <destination_file_path>, <your_password>