Saturday, July 1, 2017

Unix/Linux Cheat Sheet

To compare files recursively, between 2 directories on Linux
dir1=/home/...../dir1
dir2=/home/...../dir2

diff --brief --recursive --new-file --no-ignore-file-name-case $dir1 $dir2



To kill all processes of a certain type:
ps -ef | grep <string> | grep -v grep | awk '{print $2}' | xargs kill -9 > /dev/null


To check status of previous command:
check_last_command()
{
if [ $? -eq 0 ] ;
  then
     #good
  else
     #bad
     exit 1
fi
}


To check if a file exists:
#
if [ -f /path/filename ]; then
.
.
<do something>
.
.
fi

To check if a port is busy
netstat -an | grep <string>

To lookup dns
nslookup < >

To send email
mailx  -s "<message>" abc@cde.com < "mail_message_file_name"

Friday, June 9, 2017

Simple Java Programs

1. Sample program to calculate tips
-----------------------------
import java.lang.*;
public class Tip {

   public long getTip( int amt ) {

      double tip = 0.15;
      if (amt < 50) tip = 0.1;
      return Math.round(amt*tip);
   }

   public static void main(String []args) {

      Tip myTip = new Tip( );

           /* You can access instance variable as follows as well */
      System.out.println("Tip = " + myTip.getTip(30 ) );
      System.out.println("Tip = " + myTip.getTip(60 ) );
   }

---------
Same program, without defining a class
public class Tip1 {

   public static void main(String []args) {

      System.out.println("Tip = " + getTip(30 ) );
      System.out.println("Tip = " + getTip(60 ) );
   }

public static long getTip( int amt ) {
      double tip = 0.15;
      if (amt < 50) tip = 0.1;
      return Math.round(amt*tip);
   }
}
----------------------------------
2. Method Overload
1. Sample programs:
import java.lang.*;
public class Tip {

   public long getTip( int amt ) {

      double tip = 0.15;
      if (amt < 50) tip = 0.1;
      return Math.round(amt*tip);
   }

   public static void main(String []args) {

      Tip myTip = new Tip( );

           /* You can access instance variable as follows as well */
      System.out.println("Tip = " + myTip.getTip(30 ) );
      System.out.println("Tip = " + myTip.getTip(60 ) );
   }
-----------------------------------
3. Loops
import java.io.*;
public class LoopTest{

   public static void main(String args[]) {
   int i;
   for (i =0;i <= 10;i++) {
      if ((i == 2) || (i == 7)) continue;
    System.out.println(i);
    //System.out.println("\n");
    }
   
  
   System.out.println("while loop \n");
   i = 0;
  
    while( i < 20 ) {
         System.out.print("value of i : " + i );
         i++;
         if (i > 10) break;
         System.out.print("\n");
      }
}
}
---------------------------------------
4. Strings and Arrays
public class Palindrome {

      public static void main (String args[]) {
          String palindrome = "palindrome";
          char a[] = palindrome.toCharArray();
          char b[] = new char[20];
          System.out.println("String is " + palindrome);
         
           for (int i = palindrome.length() -1; i >= 0; i--) {
               b[palindrome.length() - i] = a[i];  
           }
      String reversed = new String(b);
       System.out.println ("Reverse is " + reversed);
}
}
-----------------------------------
 5. PolyMorphism
    class Shape{ 
// method findArea is overloaded
    double findArea (int z,int y){return(0);} 
    double findArea (int x){return(0);} 
    } 
    class Rectangle extends Shape{ 
     double findArea (int a, int b) {
             return (a*b); 
    }
    }
    class Circle extends Shape{ 
      double findArea (int r) {
       return (r * r * 3.14);  
      } 
    } 
    class Triangle extends Shape{ 
      double findArea (int a, int b) {
         return (0.5 * a * b); 
      } 
    }
    class TestPolymorphism{ 
    public static void main(String args[]){ 
   
    Shape s =new Rectangle(); 
    System.out.println("Area of a rectangle is " + s.findArea(5, 7 )); 
   
    s=new Circle(); 
    System.out.println("Area of a Circle is " + s.findArea(6)); 
   
    s=new Triangle(); 
    System.out.println("Area of a triangle is " + s.findArea(2,3)); 
    } 
    } 
------------------------


Java CheatSheet

- class name same as filename.java
- To instantiate: classname myclass = new classname ();
- Constructor method has same name as class name.
- to call a class method within main(): instancename.method(parameters)
- Methods can be overloaded by giving same name and different set of parameters.
- 'main' can instantiate a class, as long as the compiled class is in the same directory.
- static method can be called without instantiating the class.
- Local variables: local to a method. Should be initialized.
- Instance Variables: declared at instance-level. Have default values. Visible to ALL methods in the class.
- Loops: while, for, do-while. break and continue. break: goes out of loop. continue: skips rest of the statements, and starts
next iteration.
- or ||, and &&
- Inheritance: one class (subclass) extends other class (super class). Means subclass inherits methods and variables of the
superclass
- Overriding: subclass overrides the superclass's method.
- Polymorphism:
Method overloading: compile-time polymorphism
Method overriding: runtime polymorphism

Unix/Linux Cheat Sheet

To compare files recursively, between 2 directories on Linux dir1=/home/...../dir1 dir2=/home/...../dir2 diff --brief --recursive -...