Shell Programming - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

Shell Programming

Description:

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:24
Avg rating:3.0/5.0
Slides: 16
Provided by: ayse7
Category:
Tags: php | programming | shell

less

Transcript and Presenter's Notes

Title: Shell Programming


1
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

2
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
    instead)
  • 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

3
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.

4
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 first.sh to make the text file
    executable, and run ./first.sh

5
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
    like
  • In reality, these are all stored as strings, but
    routines which expect a number can treat them as
    such.

6
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_SHORT_MESSAGEhi
  • MY_NUMBER1
  • MY_PI3.142
  • MY_OTHER_PI"3.142"
  • MY_MIXED123abc

7
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.

8
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
    errors.
  • There is a command called export which has a
    fundamental effect on the scope of variables.

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

10
Exporting Variables
  • When you call myvar2.sh 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
  • ./myvar2.sh
  • MYVAR is hello
  • MYVAR is hi there

11
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.

12
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
  • . ./myvar2.sh
  • MYVAR is hello
  • MYVAR is hi there
  • echo MYVAR
  • hi there

13
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?

14
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.
15
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)
Write a Comment
User Comments (0)
About PowerShow.com