SHELL PROGRAMMING BASIC IN UNIX - PART 3


 📌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  comparing string using if statement,different format of if statement, display date & time, use of cut command, type of condition used in shell programming, how to read variable name, Use of case statement, Use of while and Until condition.

Before reading this post you must read previous post to understand better. for  previous post link is given below.

SHELL PROGRAMMING BASIC IN UNIX - PART 1

SHELL PROGRAMMING BASIC IN UNIX - PART 2

Now continue to learn about shell programming:

📕 Understand If statements in different pattern by the help of programs:

# program

$  vi clear

clear 

Pattern-1

if [ cmp  -s  $1  $2 ]

    than 

        echo " fies  $1  and $2  are same "

        echo " Removing  $2 ..... "

        rm  $2

      else

        echo " files  $2  and   $2  are not same "

fi


Pattern-2

if condition

    than

            --------

            --------

     else

            if condition

                than

                            ----------

                            ----------

                  else

                fi

fi


Pattern-3

if condition

    than

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

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

    elif condition

        than

                ----------

                ----------

        else

                -----------

                -----------

fi


📕 Command for Display Time

    $ date    → enter

    output: 10 : 20 : 52

where, 10 for Hours | 20 for Minutes | 52 for seconds

If we want to display only hours i.e only one column than

    $ date  % H

    output : 10

    $


📕 Command for cutting specified columns

The cut command cut specified columns from a file and display them on the output. -c is used for is purpose.

Syntax: $ cut  -cn1  ,  -n2   filename

                                (OR)

            $ cut  -cn1  ,  +n2  filename

Example:   

    $  cut  -c1  -25  ab1 

It will display first 25 column from each line in file ab1.

Example: 

    $  date | cut  -c12  ,13

    Output: 10

    $


Note:

📕 Three condition are used in shell programming such as:

" ------------ " used to show Meaning of special character are exist.

` ------------ ` used to show what we write it will display as it is.

' -------------' used for valid command.( Those command which is write in between these  ' ---' are valid )

Example :

    $ x = ' date | cut  -c12 , 13 '

    $ echo  $x ( show value of memory variable )

    output: 10

    $


# Program:

Write a program which display

Good morning if time is between  00.00 to 11.59

Good afternoon if time is between 12.00 to 17.59

Good evening if time is between 18.00 to 23.39

 

$  vi  xsolute

x =  ' date | cut  -c12 , 13 '

if [ $x  -ge 0   -a  $x  -le  11.59 ]

    than

            echo ( "Good morning")

    elif [ $x  -ge  12  -a  $x  -le  17.59 ]

        than

                echo ( "Good afternoon ")

        else

                echo ( "Good evening ")

fi


Command for reading any variable:

    $  vi  xcopy


    echo " Enter a1 "

    read a1

    echo " Enter a2 "

    read a2

    cp  $a1  $a2

    echo  " File is now copied to $a2 "


Case Statement:

Syntax :

    case  value  in

    choice 1)

                    ---------

                    --------- ; ;

    choice 2)

                    ---------

                    --------- ; ;

    choice n)

                    ---------

                    --------- ; ;

    esac

Example:

#  Program: 

    Create xcase file by using select case.

    choices are

    1. Today date

    2. Files in present directory

    3. Present working directory

    4. Number of user login

    5. Exit

    Enter your choice: -----


$ vi  xcase  → Enter

    clear

    echo "1 __________"

    echo "2 __________"

    echo "3 __________"

    echo "4 __________"

    echo "5 __________"

    echo

    echo "Enter your choice \c"

    read c

    case  $c  in

    1) echo  " Todays date is "

            date ; ;

    2) echo " Files are "

            ls  -l  | more; ;            

    3) echo " Present working directory is "

            pwd ; ;

    4) echo " No of user logged  in are "

            who | wc  -l ; ;

    5) ; ;

    *) echo " Not a valid choice " ; ;

            esac


📕 While and Until condition

while condition : structure is same as in c language but syntax is change. It will run till the condition is true. 

    while condition

        do

                ---------

                ---------

         done

Until condition: It will run until the condition is true.

      until condition

            do

                ---------

                ---------

            done

Example for while:

$ vi  xcount

    x = 1

    while  ( $x  -le  10 )

        do  echo  $x

            $x  =  e  expr  $ + 1

            echo  $x

        done


output:

                1

                2

                3

                4

                5

                6

                7

                8

                9

                10


Example for until:

clear

echo  " I am thinking  1 - 50 "

echo  " Guess the number ..... "

read  n

until  [ $n  -eq  21 ]

do

echo " Not matched "

read n

done

echo " Matched "

Output: It will match the input number with number 21 if user input 21 than message will show " Matched" other wise "Not matched"


# Program

Write a program to guess the number in only  5 chance.

In 1st chance give 100 marks

In 2nd chance give 80 marks

In 3rd chance give 60 marks

In 4th chance give 40 marks

In 5th chance give 20 marks


$ vi xguess

clear

echo

echo

read n

c = 1

m = 100

until  [ $n  -eq   $1  -0   $c  -lt  6 ]

        do

            echo " No ! guess again "

            read n

            c = ' expr  $c + 1'

            m = ' expr $m -20'

        done

echo " your marks are  $m  in  $c  chance "


📌 Dear friends it is the end of this post "Shell programming Basic Part 3". I hope it is beneficial for you if you want to know about part 1 and part 2 the link is given above please refer. Thank you !

No comments:

Post a Comment