Sunday, March 11, 2012

SHELL PROGRAMMING ---ARITHMETIC OPERATION USING SHELL PROGRAM unix lab


Shell Programming

Introduction:

                                    The shell is the unix system mechanism for interfacing between user and the system.It is command interpreter that reads line typed by the user and causes the execution of the requested system features. Three of the most popular shells are Bourne shell,berkely$shell and kern shell. The Bourne shell has been the primary at Unix system shell.Shell command language includes control flow capabilities command language program may be written using if…then..else,case..while for structure. These programs are written in interfacing editors like vi editors.
                                    Shell programming is a series of unix command executed one by one.

                        1) The #Symbol:
                                    This symbol is used in shell script to reverse the value that is scanned.
                        2) The $Symbol:
                                    This command is used in shell script to reverse the value that is scanned.
                        3) The read Command:
                                    This command is used in shell script to reverse the value of the variable.
                                    Syntax:
                                                read Variable name
                        4) The echo command:
                                    This command is used to display what ever message we want to display on the screen.
            Arithmetic Operation & Expression:
                        The arithmetic operators provided by the shell are Addition in operator, Subtraction in operators, Multiplication & Division/operator. The “expr” command helps us in getting the numerical value of the digit strings.
            Logical Operators:

            a          -           Used for indicating “AND
            o          -           Used for indicating “OR”
Comparison Operators:
            n1-eq n2          will check if the integer are equal
            n1-ne n2          will check if the two integer are not equal
            n1-gt n2           will check if the integer n1 is greater than n2
                        n1-lt n2            will check if the integer n1 is less than n2
                        n1-ge n2          will check if the integer n1 is greater than or equal to n2
                        n1-le n2           will check if the integer n1 is less than or equal to n2
            Programming Language Constructs:
            The if construct:
            Syntax 1
                        If(condition)
                        then
                                    command
                        else
                                    command
                        fi
            The case Construct:
            Syntax:
                        case value n
                        Choice 1) commands;;
                        Choice 2) command;;
                        Esac
            The for command:
            Syntax:
                        for<variable>in value1,value2……….
                        do
                                    command
                        done
            The while Construct:
            Syntax:
                        while control command
                        do
                                    command list
                        done
            The until loop:
            Syntax:
                        until control command
                        do
                                    command
                        done

            The execution Commands:
                        A shell script can be executed in two ways
                                    1) Type in “sb FILENAME” at the $ prompt.
                                    2) Grant execute permission to the file and then type in the FILENAME at                        the $prompt
            The Debug Command:
                        This command is used for debugging the program.
            Syntax:          
                                    sb- x filename.












ARITHMETIC OPERATION USING SHELL PROGRAM


AIM:
            To write a shell Program to do the Arithmetic Operations like Addition, Subtraction, Multiplication, Division.

ALGORITHM:

Step 1: Start
Step 2: Read the values of ‘a’ and ‘b’
Step 3: Write the expression to calculate sum, difference, Product, Quotient and Remainder.
Step 4: Print the Results
Step 5: Stop.

SHELL PROGRAM:

echo "Enter Two Numbers"
read a b
c=`expr $a + $b`
d=`expr $a - $b`
e=`expr $a \* $b`
f=`expr $a / $b`
g=`expr $a % $b`
echo "Sum = $c"
echo "Difference = $d"
echo "Product = $e"
echo "Quotient = $f"
echo "Remainder = $g"

SAMPLE OUTPUT:
Enter Two Numbers
12 10
Sum = 22
Difference = 2
Product = 120
Quotient = 1
Remainder = 2

RESULT:      Thus the shell Program to do arithmetic operation was executed without any errors.

No comments:

Post a Comment