SHELL PROGRAMMING BASIC IN UNIX - PART 2

📌Highlights: In this post we will learn about shell programming basic. This post cover so many basic commands which is useful in programming such as how we declare variable in shell program, display value of variable, how logical operator used in program, type of Relational operators, how we compare two strings, syntax of if statement with example, how for statement is used and their syntax.  

⧭ Shell programming is basically a shell command which is interpreted by shell.

Let we first understand user memory variables:

    $ a =800  (variable declaration)

    $  echo  a  (It will display string / text which are written after echo command.)

here output is:  a

    $ echo  $ a  ( It will display value of a)

 here output is: 800

Example:

    $ ab = " ls -l "    

    $ b = " abc "

    $  $ ab  $ b  (or) we can write ($  ls  -l    abc)

Let we understand program in shell:

# Program:

    echo " I am very powerful  $0  command "

    echo " I have  $# argument "

    echo " My argument are :  $* "

    echo " My first argument is :  $1 "

    : wq

For running the above program write:








here the output will be:

Now remember:

    $# For number of arguments

    $*  For name of arguments

    $0  Command 

    $1  Command for first argument

    $2  Command for second argument

    $3  Command for third argument

          and more ........

Note: ($1, $2, $3,....) are number of memory variables.


Statement of [For Statement]:

For variable in List .....

    do

            Command

            Command

            Command

            -------

    done

Example of For Statement:

$ vi xsort  → Enter

clear

For  x is  $*

    do echo " ......................."

        echo " sorted content from file $x "

        echo " ......................"

        echo

        sort $x

    done

: wq

$


here the output is:

........................

Sorted content from file abc

---------------

---------------

---------------

Sorted content from file abc1

---------------

---------------

---------------

Sorted content from file abc2

---------------

---------------

---------------

and so on...


Statement of [If condition]:

if condition
    than
            command
            -----------
            -----------
            -----------
    else
            command
            -----------
            -----------
            -----------

fi

Note:

For comparing two files

    $ cmp  -s  file1  file2

    Example: $  cmp  ab1  ab2

This command compare the content of ab1 and ab2 if it will same than return 0 for true and  if the content is not same than return 1 for false.

Example:

    $  cmp  -s  ab1  ab2

    $ echo $ ?

    0

    $

Relational Operators:

    -gt  is used for greater than

    -gt is used for equal to

    -ge is used for greater than equal to

    -lt  is used for less then

    -le  is used for less than and equal to.

    -ne is used for not equal to.


Example: 

    if [$x  -gt  5]

        --------
        --------
    else

        --------
        --------
    fi

In the above example  we check whether x is greater than 5 or not.


Logical Operator: 

    -a is used for logical AND

    -o is used for logical OR

Example:

    if [ $x  -gt  5  -a  $y  -eq  9]

        ---------
        ---------
    else
        ---------
        ---------
    fi

In the above example condition 1 = $x  -gt  5  , condition 2 = $y  -eq  9 , -a is logical and
 
It is just like: if (x>5 and y=9)

Now this is the end of this post shell programming basic - part 2. I hope you understand the above things about basics. We will continue shell programming basic in next post with part 3. Read shell programming basic - part 1.

No comments:

Post a Comment