Saturday, January 19, 2013

UNIX Architecture

There are two major agencies on which UNIX depends are :Kernel and Shell. The Kernel interacts with the machines hardware, and the Shell is interacts with the user of the machine. The diagram below shows the architecture of UNIX



Kernel


    • It is the core of the operating system, a collection of routines mostly written in C.
    • It is loaded into memory when the system is booted and communicates directly with the hardware.
    • User programs/applications that need to access the hardware like hard disk or terminal that use the services of the kernel, which performs the job on the users behalf. These programs access the kernel through a set of functions called system calls.
    • It manages the systems memory, schedules processes, decides their priorities, and performs other tasks.

    Shell

    • Computers don't have any inherent capability of translating commands into action. That requires a command interpreter, a job that is handled by the "Outer Part" of the operating system.
    • Shell is interface between the user and kernel. Even though there's only one kernel running on the system, there could be several shells in action-one for each user who is logged in.

    Why UNIX?

    Control

    • Commands often provide complete access to the system and its devices.
    • Most devices can be manipulated just like files

    Flexibility

    • Commands are just programs.
    • Commands have common interface to allow inter-operation with other commands.
    • Unix offers thousands of tools that can be combined and recombined.

    Reliability

    • Commands are typically lightweight since they typically do little more than invoke operating system calls.
    • Unix is hard to crash.
    • Provide multitasking, each user can perform multiple task easily.

    Powerful

    • Unix as an operating system can support almost any kind of software.
    Note :
             UNIX was developed at AT&T Bell Laboratories by Ken Thompson and Dennis Ritchie. It was finally written in C.
             All UNIX flavors today offer a graphical user interface (GUI) in the X Window system. But the strength of UNIX lies in its commands and options.

    Wednesday, January 2, 2013

    Unix File System

    File

    • File is a container for storing information.
    • It stored sequence of characters,it doesn't contain the eof(end-of-file) mark like DOS files.
    • A file's size is not stored in the file, nor even its name.
    • All file attributes are kept in a separate area of the hard disk, not directly accessible to humans, but only to the kernel.
    Unix treats directories and devices as files as well. A directory is simply a folder where you store filenames and other directories. All physical devices like the hard disk, memory, CD-ROM, printer and modem are treated as files. The below picture depicts the file structure.



    The implicit features of every UNIX file system is that there is a top, which serves as the reference point for all files. This top is called root and is represented by a / (front-slash). root is a directory. It is conceptually different from the user-id root used by the system administrator to log in.

    The root directory (/) has a number of subdirectories under it. These subdirectories in turn have more subdirectories and other files under them.  For instance, usr and home are two directories directly under /, while a second tim,jane,tom and julia are subdirectories are home.

    Every file , apart from root, must have a parent and it should be possible to trace the ultimate parentage of a file to root. Thus, the home directory is the parent of jane, while / is the parent of home, and the grandparent of jane. If you create a file prog under jane directory, then jane will be parent of this file.

    It's also obvious that, in these parent-child relationships, the parent is always a directory. home and jane are both directories as they are both parents of at least one file or directory. prog is simply an ordinary file. it cant have any directory under it.

    Tuesday, January 1, 2013

    WHILE LOOP in Shell Script Example

    Each while loop consists of a set of commands and a condition

    general syntax as follows for bash while loop:

    while [ condition ]do
     command1
     command2
     commandN
    done
    1. The condition is evaluated, and if the condition is true, the command1,2…N is executed.
    2. This repeats until the condition becomes false.
    3. The condition can be integer ($i < 5), file test ( -e /tmp/lock ) or string ( $ans != "" )
    Example :

    x=1;
    while [ $x -lt 10 ] ; do
    (( x++ ))
    echo "x = $x"
    done


    OUTPUT :

    -bash-3.2$ sh while_sh
    x = 2
    x = 3
    x = 4
    x = 5
    x = 6
    x = 7
    x = 8
    x = 9
    x = 10
    -bash-3.2$
    The while loop should be executed upto the condition failed (1<10)

    UNTIL loop example in Shell Script

    LIMIT=10
    var=0
    until (( var > LIMIT ))
    do  # ^^ ^     ^     ^^   No brackets, no $ prefixing variables.
      echo -n "$var "
      (( var++ )) ## increment operator in shell script
    done    # 0 1 2 3 4 5 6 7 8 9 10
    echo
    exit 0

    OUTPUT :

    -bash-3.2$ sh until_sh
    0 1 2 3 4 5 6 7 8 9 10
    -bash-3.2$

    Shell Script Function can return the value upto 255 example

    #!/bin/bash
    # The largest positive value a function can return is 255.
    return_test ()         # Returns whatever passed to it.
    {
      return $1
    }
    return_test 27         # o.k.
    echo $?                # Returns 27.
     
    return_test 255        # Still o.k.
    echo $?                # Returns 255.
    return_test 257        # Error!
    echo $?                # Returns 1 (return code for miscellaneous error).
    exit 0

    OUTPUT :

    -bash-3.2$ sh func_return_limit
    27
    255
    1
    -bash-3.2$

    IF ELSE & IF ELIF IF in Shell Script

    #!/bin/bash
    x=10
    y=20
    echo "Value x=$x y=$y"
    if [ $x -eq $y ] ; then
            echo -e " x and y are same value "
    elif [ $x -gt $y ] ; then
            echo -e " x is greater than y "
    else
            echo -e " x is less than y"
    fi

    str1="Chidam"
    str2="Chidam"
    echo "Value str1=$str1 str=$str2"
    if [ "$str1" = "$str2" ] ; then
            echo -e " str1 and str2 are same value "
    else
            echo -e " str1 and str2 are not same value "
    fi


    OUTPUT :

    -bash-3.2$ sh if_elif_ex
    Value x=10 y=20
     x is less than y
    Value str1=Chidam str=Chidam
     str1 and str2 are same value

    String Comparison Operator in Shell Script

    The below code snippet shows how to compare two strings

    #!/bin/ksh
    str1="Chidam";
    str2="Chidam";
    if [ "$str1" = "$str2" ] ; then
            echo -e "String str1 and str2 are same"
       echo -e "string1=$str1 and string2=$str2"
    fi
    str1="Chidam";
    str2="Chidamk";
    if [[ "$str1" != "$str2" ]] ; then
        echo -e "String str1 and str2 are not equal"
       echo -e "string1=$str1 and string2=$str2"
    fi


    OUTPUT :

    -bash-3.2$ sh str_comp
    String str1 and str2 are same
    string1=Chidam and string2=Chidam
    String str1 and str2 are not equal
    string1=Chidam and string2=Chidamk

    =  --> EQUAL OPERATOR
    != --> NOT EQUAL OPERATOR

    How to return the value to the function calling using return key

    The below code snippet shows how to return the value to the function calling and how to override the delcared variable value.

    funReturn1()
    {
      x=10;
      echo "$x"
    }
    rvalue=`funReturn1`
    echo -e "function funReturn return rvalue = $rvalue\n"

    y=10;
    funReturn()
    {
      y=20;
      return;
    }
    funReturn
    echo -e "function funReturn return y = $y\n"


    OUTPUT :

    -bash-3.2$ sh func_return
    function funReturn return rvalue = 10
    function funReturn return y = 20
    -bash-3.2$
    Explanation :

    function funReturn1 is just return the value to the function calling and assign the return value into rvalue variable here only one echo statement is used dont use more than one echo statement.

    y=10; # declared the value in global
    funReturn()
    {
      y=20; # here the value y=10 is reassigned to y=20 after the function call the value should be updated to y=20
      return;
    }
    funReturn

    Global & Local Variable in Shel Script

    The below code snippet shows how can we declare global and local variables in shell scripts.

    #!/bin/bash
    x=10
    increment()
    {
     echo "global variable : before increment value x=$x"
     (( x++ ))
    }
    increment
    echo -e "global variable : after increment value x=$x\n"

    localVar()
    {
     local var_local=50;
     echo "inside localVar function value of var_local = $var_local"
    }
    localVar
    echo "inside localVar function value of var_local = $var_local"


    OUTPUT :

    -bash-3.2$ sh local_global_var_ex
    global variable : before increment value x=10
    global variable : after increment value x=11
    inside localVar function value of var_local = 50
    inside localVar function value of var_local =
    global variable is declared outside the function
    local variable is delcared in side the function with key word local.
    we cant use local variable outside the function but global variable can use inside/outside the function

    For Loop in Shell Script

    The below code snippet shows how to use for loop in shell script .

    List="one two three"
    for a in $List     # Splits the variable in parts at whitespace.
    do
      echo "$a"
    done
    # one
    # two
    # three
    echo "---"
    for a in "$List"   # Preserves whitespace in a single variable.
    do #     ^     ^
      echo "$a"
    done
    # one two three
    echo "---"
    for file in `ls * 2>/dev/null`
    do
     echo "File name : $file"
    done


    OUTPUT :

    -bash-3.2$ sh for-loop-ex
    one
    two
    three
    ---
    one two three
    ---
    File name : arithmatic_sh
    File name : case_example
    File name : check_min_params
    File name : comparion_op_ex
    File name : for-loop-ex
    File name : passing_parameter_mech
    File name : simple_function
    File name : simple_multiplication
    Explanation :

    ls * 2>/dev/null  
                             it will try to list all the files in current directory if there is no files found in current directory means it surpress the warning message
    /dev/null            this is the 0 device file

    Dereferenced Passing Parameter Mechanism in Shell Script Function

    The below code snippet shows how to pass the parameters to the function in shell script. If we used $1,$2,,,$n with in the function
    $1 says first argument of this function
    $2 says second argument of this function
    .
    .
    .
    $n says nTh argument of this function.


    #!/bin/bash
    add()
    {
     x=$1;
     y=$2;
     z=`expr $x + $y`;
     echo $z
    }
    sum=`add 10 20`
    echo "sum x=10 y=20 x+y=$sum"


    OUTPUT:

    -bash-3.2$ sh passing_parameter_mech
    sum x=10 y=20 x+y=30

    sum=`add 10 20`
       calling the function add with passed two parameters 10 and 20 and return value is assigned into variable sum here need to put it both side of function call `(tilde) sign.
    x=$1 
      values of 10 is assigned into the variable x
    y=$2
      values of 20 is assigned into the variable y

    we can call x and y as dereferenced variables in shell function.

    Check minimum Command Line Parameters in Shell Script

    The below code snippet shows how can check whether the minimum number for paramters are passed to the shell script or not.

    -bash-3.2$ cat check_min_params
    MINPARAMS=10
    echo "All the command-line parameters are: "$*""
    if [ $# -lt "$MINPARAMS" ]
    then
      echo
      echo "This script needs at least $MINPARAMS command-line arguments!"
    fi
    echo
    exit 0


    OUTPUT :

    -bash-3.2$ sh check_min_paramsAll the command-line parameters are:
    This script needs at least 10 command-line arguments!
    -bash-3.2$ sh check_min_params p1 p2 p3All the command-line parameters are: p1 p2 p3
    This script needs at least 10 command-line arguments!
    -bash-3.2$ sh check_min_params p1 p2 p3 p4 p5 p6 p7 p8 p9 p10All the command-line parameters are: p1 p2 p3 p4 p5 p6 p7 p8 p9 p10

    $# --> Total number of parameter passed from shell script command line to the program.

    Number Comparison Operater in Shell Script

    The below code snippet shows how can we use comparison operator in shell script for number.

    -bash-3.2$ cat comparion_op_ex
    #!/bin/bash
    x=10
    y=10
    echo -e " value x = $x and y = $y"
    if [ $x -eq $y ] ; then
            echo -e " x and y are same value\n "
    fi
    x=10
    y=20
    echo -e " value x = $x and y = $y"
    if [ $x -ne $y ] ; then
            echo -e " x and y are not equal \n"
    fi
    x=20
    y=10
    echo -e " value x = $x and y = $y"
    if [ $x -gt $y ] ; then
            echo -e " x is greater than y \n"
    fi
    x=10
    y=20
    echo -e " value x = $x and y = $y"
    if [ $x -lt $y ] ; then
            echo -e " x is less than y\n"
    fi


    OUTPUT :

    -bash-3.2$ sh comparion_op_ex
     value x = 10 and y = 10
     x and y are same value
     value x = 10 and y = 20
     x and y are not equal
     value x = 20 and y = 10
     x is greater than y
     value x = 10 and y = 20
     x is less than y
    -bash-3.2$

    gt   --> greater than
    lt    --> less than
    eq  --> equal
    ne  --> not equal

    EBS Order Holds details and release responsibility

      SELECT ooh.order_number,                  ooh.ordered_date,                 ooh.flow_status_code,                 ooh.credit_card_approval...