Shell Programming - PowerPoint PPT Presentation


PPT – Shell Programming PowerPoint presentation | free to view - id: 1d907b-ZDc1Z


The Adobe Flash plugin is needed to view this content

Get the plugin now

View by Category
About This Presentation

Shell Programming


Also internal shell commands, such as testing and loop constructs, give ... C, C , C#, Java, PHP, Perl, Python, Tcl, Unix shells (sh, csh, ksh, bash) ... – PowerPoint PPT presentation

Number of Views:18
Avg rating:3.0/5.0
Slides: 16
Provided by: ayse7


Write a Comment
User Comments (0)
Transcript and Presenter's Notes

Title: Shell Programming

Shell Programming
  • A shell program, called a script, is an
    easy-to-use tool for building applications by
    "gluing" together system calls, tools, utilities,
    and compiled binaries.
  • Virtually the entire repertoire of UNIX commands,
    utilities, and tools is available for invocation
    by a shell script.
  • Also internal shell commands, such as testing and
    loop constructs, give additional power and
    flexibility to scripts.
  • Shell scripts lend themselves exceptionally well
    to administrative system tasks and other routine
    repetitive jobs not requiring a full-blown
    tightly structured programming language

When not to use shell scripts
  • resource-intensive tasks, especially where speed
    is a factor (sorting, hashing, etc.)
  • procedures involving heavy-duty math operations,
    especially floating point arithmetic, arbitrary
    precision calculations, or complex numbers (use
    C or FORTRAN instead)
  • cross-platform portability required (use C
  • complex applications, where structured
    programming is a necessity (need typechecking of
    variables, function prototypes, etc.)
  • need multi-dimensional arrays, need data
    structures, such as linked lists or trees

Developers Dilemma
  • Which language(s) you should invest your time on?
  • C, C, C, Java, PHP, Perl, Python, Tcl, Unix
    shells (sh, csh, ksh, bash)
  • The general answer find the right language for
    your problem.

Shell Programming
  • A First Script
  • !/bin/sh
  • This is a comment!
  • echo Hello World This is a comment, too!
  • The first line tells Unix that the file is to be
    executed by /bin/sh
  • The second line begins with a special symbol .
    This marks the line as a comment, and it is
    ignored completely by the shell. The only
    exception is when the very first line of the file
    starts with !
  • The third line runs a command echo, with two
    parameters, or arguments - the first is "Hello"
    the second is "World".
  • run chmod 755 to make the text file
    executable, and run ./

Shell Variables
  • !/bin/sh
  • MY_MESSAGE"Hello World"
  • echo MY_MESSAGE
  • Note that there must be no spaces around the ""
    sign VARvalue works VAR value doesn't work.
    In the first case, the shell sees the "" symbol
    and treats the command as a variable assignment.
    In the second case, the shell assumes that VAR
    must be the name of a command and tries to
    execute it.
  • The shell variables have no type they may store
    strings, integers, real numbers - anything you
  • In reality, these are all stored as strings, but
    routines which expect a number can treat them as

Shell Variables
  • x"hello"
  • yexpr x 1
  • expr non-numeric argument
  • If you assign a string to a variable then try to
    add 1 to it, you will get an error. But there is
    no syntactic difference between below
  • MY_MESSAGE"Hello World
  • MY_PI3.142
  • MY_OTHER_PI"3.142"
  • MY_MIXED123abc

Shell Variables
  • !/bin/sh
  • echo What is your name?
  • read MY_NAME
  • echo "Hello MY_NAME - hope you're well."
  • We can interactively set variable names using the
    read command
  • This is using the shell-built-in command read
    which reads a line from standard input into the
    variable supplied.

Scope of Variables
  • Variables in the bourne shell do not have to be
    declared, as they do in languages like C. But if
    you try to read an undeclared varable, the result
    is the empty string. You get no warnings or
  • There is a command called export which has a
    fundamental effect on the scope of variables.

Scope of Variables
  • !/bin/sh
  • echo "MYVAR is MYVAR"
  • MYVAR"hi there"
  • echo "MYVAR is MYVAR"
  • ./ Run the script
  • MYVAR is
  • MYVAR is hi there
  • MYVARhello Now run it like this
  • ./
  • MYVAR is
  • MYVAR is hi there

Exporting Variables
  • When you call from your interactive
    shell, a new shell is spawned to run the script.
    We need to export the variable for it to be
    inherited by another program - including a shell
    script. Type
  • export MYVAR
  • ./
  • MYVAR is hello
  • MYVAR is hi there

Sourcing Scripts
  • Look at line 3 of the script this is changing
    the value of MYVAR. But there is no way that this
    will be passed back to your interactive shell.
    Try reading the value of MYVAR
  • echo MYVAR
  • hello
  • Once the shell script exits, its environment is
    destroyed. But MYVAR keeps its value of hello
    within your interactive shell.

Sourcing Scripts
  • In order to receive environment changes back from
    the script, we must source the script - this
    effectively runs the script within our own
    interactive shell, instead of spawning another
    shell to run it.
  • We can source a script via the "." command
  • MYVARhello
  • echo MYVAR
  • hello
  • . ./
  • MYVAR is hello
  • MYVAR is hi there
  • echo MYVAR
  • hi there

More about variables
  • !/bin/sh
  • echo "What is your name?"
  • read USER_NAME
  • echo "Hello USER_NAME"
  • echo I will create a file USER_NAME_file"
  • touch USER_NAME_file
  • This results in an error unless there is a
    variable called
  • USER_NAME_file. Why?

More about variables
!/bin/sh echo "What is your name?" read
USER_NAME echo "Hello USER_NAME" echo I will
create a file USER_NAME_file" touch
USER_NAME_file The shell now knows that we
are referring to the variable USER_NAME and that
we want it suffixed with _file.
Lab Work
  • Write a shell script which reads two directory
    paths (dir1 and dir2) and a file name from the
    user. Then moves the file from dir1 to dir2.
  • Write a shell script which will archive and zip
    the files which are modified in the last day.
    (Hint use the shell commands find, xargs, tar)