Shell Programming by Shoaib Ahmed

download Shell Programming by Shoaib Ahmed

of 24

Transcript of Shell Programming by Shoaib Ahmed

  • 7/31/2019 Shell Programming by Shoaib Ahmed

    1/24

    M. Shoaib Ahmed

    MC-111002

    Shell Programming

  • 7/31/2019 Shell Programming by Shoaib Ahmed

    2/24

    Unix

    Definition:

    Unix is a multitasking operating system.

    It was developed in early 1970 at Bell Laboratories.

    Uses command line interface.

    Mainly used in networking environment.

  • 7/31/2019 Shell Programming by Shoaib Ahmed

    3/24

    Cont.

    . Description

    Unix is the very popular operating system thatare been using in various platforms. So

    Why Unix Is that much Popular

    Unix is very powerful,

    multiuser, time sharing, multitasking operatingsystem with extensive commands fasciitis and

    communication capabilities.

  • 7/31/2019 Shell Programming by Shoaib Ahmed

    4/24

    Unix consists of basically two parts.

    Kernel.

    Set of commands and Utilities.

    or we can called it the Shell and Utilities.

    UNIX

  • 7/31/2019 Shell Programming by Shoaib Ahmed

    5/24

    Kernel and shell

  • 7/31/2019 Shell Programming by Shoaib Ahmed

    6/24

    kernel

    In Ms Windows and other operating system, we canvisible only one operating system and that operatingsystem interfacing with the hardware

    Kernelis interface between the shell and the

    hardware of the computerShellis the outer cover which is providing theinterface between the user and the kernel.

    The inner cover called as the kernel and the outer

    cover sour ding the kernel is called the shell.

  • 7/31/2019 Shell Programming by Shoaib Ahmed

    7/24

    Cont

    so let us looking to the graphical image in order

    to understand these aspect.

  • 7/31/2019 Shell Programming by Shoaib Ahmed

    8/24

    Kernel

    Heart of Unix

    Core part of the Unix operating system.

    Kernel resident in main memory.It provides basic function including concurrentprogram execution, memory management, I/O andfile services and network interface.

    So these are the primary function does by thekernel.

  • 7/31/2019 Shell Programming by Shoaib Ahmed

    9/24

    What is shell

    Shell forms an interface between the user and thekernel.

    It collects and interpret the user commands, invokes

    the concerned program file from memory and directsthe kernel to execute them.

    It acts like a command processor of other operatingsystems.

    The duty of the shell is to fetch the file and direct thekernel to execute the file in order to get desire outputor result

  • 7/31/2019 Shell Programming by Shoaib Ahmed

    10/24

    Kernel and shell

    Shell picking up the command from the user or totrying understand the command from the user and itis trying to convey to the kernel to execute the related

    file.

    So this is the way how to kernel and the shell and theuser are coordinating the whole work.

  • 7/31/2019 Shell Programming by Shoaib Ahmed

    11/24

    Bourne shell

    Written by S. R. Bourne.

    Denoted by sh.

    Default prompt is $.Whenever we are seeing that particular $ prompt onthe terminal, it is nothing but we are working with theBourne shell

  • 7/31/2019 Shell Programming by Shoaib Ahmed

    12/24

    Berkeley shellWritten by Berkeley.

    Denoted by Csh.

    % is the prompt for the C shell.

    Ksh shell:Ksh, the korn shell, developed by David korn.

  • 7/31/2019 Shell Programming by Shoaib Ahmed

    13/24

    Cont

    Whenever we are taking about the shellprogramming, this is nothing but we are programmingthe shell, either it may be a C shell, it may be a

    Bourne shell, or it may be a k shell, or it may be a jetshell, so

    Whenever we are programming that particular shell,that particular programming with respect to the shell

    has a different type of syntax.

  • 7/31/2019 Shell Programming by Shoaib Ahmed

    14/24

    Cont

    Suppose if you want to work with Bourne shell, youshould able to learn the basic of the Bourne shell andyou can program very good.

    The particular shell will be having certain syntaxwhich is reserve.

    Suppose if you are working with the C shell, then youhave to complete knowledge of the C shell

    programming

  • 7/31/2019 Shell Programming by Shoaib Ahmed

    15/24

    Shell scripts

    so whenever we are taking about the shellprogramming or programming a shell, either it maybe a ksh shell, it may be C shell or it may be Bourne

    shell.

    We have to write a program and that program iscalled as in technical terms, it is called as the shellscript, or shell program.

    These are the two names giving to the programming.

  • 7/31/2019 Shell Programming by Shoaib Ahmed

    16/24

    Cont

    It is a file containing a set of commands to beexecuted by the shell in sequence as it reads a file.

    Suppose if you are creating some program in aBourne shell.

    Suppose if you want to keep the name as abc for thefile, so you have to give the extension as sh.

    So the complete name reads as abc.sh.

    So this is the program you are creating.

  • 7/31/2019 Shell Programming by Shoaib Ahmed

    17/24

    Cont

    scripting languages will be having its own features, ithas own limited character set, certain symbol may berestricted and certain conditional statements are

    available, iterative statements are available,assignment statements are available, variables areavailable.

  • 7/31/2019 Shell Programming by Shoaib Ahmed

    18/24

    Shell vs. Convential

    Convential languages like C, Cobol, Pascal etc.

    They are full tragibal languages.

    Scripting languages are minimum features not thefeatures that are entirely available in conventionalprogramming languages.

    We have very limited features where in which theuser can create program and execute the program ina limited use.

    It is not an equal lent or a super set.

    It forms subset of a conventinal programminglanguag.

  • 7/31/2019 Shell Programming by Shoaib Ahmed

    19/24

    Search shell script file

    When the shell is given a command to execute, itlooks the command in different directories in a fixedsequence.

    The first directory, searched is the current directory,followed by/ binand/ user / bin.

    First current directory.

    Secondly/ bin.

    Third/ user / bin.

  • 7/31/2019 Shell Programming by Shoaib Ahmed

    20/24

    Basic of Shell

    ProgrammingCharacter set.

    a-z, A-Z, 0-9.

    Meta character.* for matching a string.

    ? For a single character.

    Arithmetic operator.

    + - * / %.

  • 7/31/2019 Shell Programming by Shoaib Ahmed

    21/24

    Cont

    Logical operator.

    a for AND operator.

    O for OR operator.Relational operator.

    Equal toeq, Not equal tone, Less thanlt.

    Greater thangt, greater than equal toge.

    Less than equal tole.

  • 7/31/2019 Shell Programming by Shoaib Ahmed

    22/24

    Types of statements

    Output statements : echo.

    Input statements : read, assignment statement.

    Process statements : expr, test, exist.Conditional statements.

    Loop statements.

  • 7/31/2019 Shell Programming by Shoaib Ahmed

    23/24

    Cont

    #!/bin/sh

    When unix tries to execute the script, it sees the firsttwo characters (#!) and knows that it is a script.

    It then reads the rest of the line to find out whichprogram is to execute the script.

    In Bourne shell script, the first line of our script mustbe.

    #!/bin/sh

  • 7/31/2019 Shell Programming by Shoaib Ahmed

    24/24

    Sample Program of Calculator

    echo "**** My calculator ****"

    echo "M A I N - M E N U

    echo "1. Multiplication"

    echo "2. Subtraction"

    echo"3. Remainder

    echo "4. Divide"

    echo -n "Please select your choice (1-4) :

    read choice

    echo -n "Enter your first number : "

    read n1

    echo -n "Enter your second number : "

    read n2

    if[ $choice -eq 1 ]

    thenanswer="$n1 x $n2 = $(( $n1 * $n2 ))

    elif[ $choice -eq 2 ]

    then answer="$n1 - $n2 = $(( $n1 - $n2 ))"

    elif[ $choice -eq 3 ]thenanswer="$n1 % $n2 = $(( $n1 % $n2 ))

    elif[ $choice -eq 4 ]

    then answer="$n1 / $n2 = $(( $n1 / $n2 ))"

    elseecho "Sorry please select number between 1-4 only

    exit 1

    fiecho $answer