shell programing

Embed Size (px)

Citation preview

  • 8/7/2019 shell programing

    1/26

    Software Development 1:7005INT

    Lecture 12

  • 8/7/2019 shell programing

    2/26

    Announcements

    Next weeks lecture topics (week 13): Revision

    Discussion of the final exam

    Course evaluation

    Attendance essential!

  • 8/7/2019 shell programing

    3/26

    Shell programming/scripts

    What is a shell?

    A shell is a program that allows system users tocommunicate with the operating system

    The shell accepts commands from the system-users and passes them to the system for execution

    In UNIX, three shells are available:

    Bourne shell

    Korn Shell

    C Shell

  • 8/7/2019 shell programing

    4/26

    UNIX Shells

    The Bourne Shell was the first popularUNIX Shell

    It is called the Bourne Shell because it was

    written by Stephen Bourne

    The Bourne Shell was written in the C

    programming language and the executablefile (sh) is stored in the directory bin ( /bin/ )

  • 8/7/2019 shell programing

    5/26

    UNIX Shells (cont.)

    The C Shell was the second most popular UNIXShell after the Bourne Shell

    It is called the C Shell because control structures

    for the C Shell are very similar to the C language The Korn Shell is a mixture of the Bourne and C

    Shells

    It is called the Korn Shell because it was written

    by David Korn

  • 8/7/2019 shell programing

    6/26

    Shell Programming

    What is shell programming? Shell programming is programming that

    contains shell commands

    A shell program is called a Shell Script

    Shell programs or Scripts do not need acompiler because they contain shellcommands and can be executed from thecommand prompt

  • 8/7/2019 shell programing

    7/26

    Example: A simple C Shell Script

    File name: list.csh

    #!/bin/csh

    finger

    ls -a | more

    In the above script, the first line signals that thefile list.csh contains a C Shell Script (csh)

    To execute the above script we must change thepermissions of the file list.csh using thechmod command as follows:

    {/home1/staff/mblum} > chmod 700 list.csh

    Run the script as follows:

    {/home1/staff/mblum} > ./list.csh

  • 8/7/2019 shell programing

    8/26

  • 8/7/2019 shell programing

    9/26

    Example: Assigning/Recallinginteger variable values

    #!/bin/csh@ myage = 20

    @ age1 = 10

    @ age2 = 20

    @ age = $age1 + $age2 + $myage

    echo $age

    The output of the above script will be 50

  • 8/7/2019 shell programing

    10/26

    List Variables

    The C shell allows users to create and access listvariables

    Some examples are given below:

    set programmingLanguages= (Pascal C LISP Java Ada) will create a list programmingLanguages with five

    elements: Pascal, C, LISP, Java, Ada

    echo $programmingLanguages will display Pascal C LISP Java Ada on the screen

  • 8/7/2019 shell programing

    11/26

    List Variables (cont.)

    set colors=(red blue green) will create a list colors with three elements red,

    blue and green

    echo $colors[2] will display blue on the screen

    set colors=($colors yellow) will add yellow to the list colors

  • 8/7/2019 shell programing

    12/26

    Example: Create and display a list

    #!/bin/csh# create a list variable subjects

    set subjects=(history, physics)

    # add math to the list variable subjects

    set subjects=($subjects math)

    # display third element of the listecho $subjects[3]

  • 8/7/2019 shell programing

    13/26

    Input/Output Commands

    The echo command displays information on thescreen

    For example:

    echo I am here will display "I am here" on the screen

    The $< command reads data from the keyboard

    Example:

    set myname = $