TDC597 Linuxbased Network Management Lecture Two - PowerPoint PPT Presentation

1 / 45
About This Presentation
Title:

TDC597 Linuxbased Network Management Lecture Two

Description:

What is Shell Programming? A high-level program writing in a script language (called Shell) ... Shell script name $0. Description. Argument. 07/25/07. 17 ... – PowerPoint PPT presentation

Number of Views:133
Avg rating:3.0/5.0
Slides: 46
Provided by: Pau1191
Category:

less

Transcript and Presenter's Notes

Title: TDC597 Linuxbased Network Management Lecture Two


1
TDC597Linux-based Network ManagementLecture
Two
  • James T. Yu, Ph.D.
  • jyu_at_cs.depaul.edu
  • School of CTI
  • DePaul University

2
Lecture Outline
  • Shell Programming
  • Input/Output
  • Data Structures
  • Control Structures
  • Modular Design
  • Awk
  • Pattern Match
  • Associative Array (hashing)

3
What is Shell Programming?
  • A high-level program writing in a script language
    (called Shell).
  • Compiler vs. Interpreter
  • No object code
  • Simple data structure
  • Simple control structure
  • Easy to use
  • Task automation

4
Your First Shell Program
5
How to create a shell program
  • Write a program using a text editor (such as vi)
  • Convention !/bin/sh at the beginning
  • Good programming practice
  • If it is a shell program, use .sh as the suffix
    of the file
  • first.sh
  • Add comments in your program (anything after
    is comment.)
  • Change the file mode to be executable
  • chmod x first.sh
  • Run the program
  • If the current directory is in PATH, enter
    first.sh
  • If the current directory is not in PATH, enter
    ./first.sh
  • . means the current directory

6
File I/O
7
Shell Variables(with the program scope)
X define a variable X reference a
variable X and Y are defined in the program
(variable.sh) but are not defined outside the
program.
8
Shell Variables(outside the program scope)
X and Y are also available to other Shell
Programs
9
Simple Arithmetic
10
Simple Command I/O
Read data from the keyboard The output goes to
the screen.
11
Program Structure
  • Sequence you already learned it
  • Conditions
  • comparison and boolean operation
  • if
  • case
  • Loop
  • while
  • for

12
if structure
if ltconditiongt then ltshell commandsgt fi
if ltconditiongt then ltshell commandsgt elif
ltconditiongt then ltshell commandsgt elif
ltconditiongt ltshell commandsgt else ltshell
commandsgt fi
if ltconditiongt then ltshell commandsgt else ltshell
commandsgt fi
13
Comparison
14
if-condition
15
More Comparison
16
Shell Command Arguments
17
Get Input from Command Line
1 1st parameter 2 2nd parameter
18
File Test
-s ltfilegt file exists with size gt 0 -f ltfilegt
file exists
19
File Test (cont.)
test ltexpgt same as ltexpgt
-d is it a directory?
20
Save Execution to a Variable
The special character (back quote) is to save
the execution result into a variable.
21
case
case variable in v1) ltshell commandsgt
v2) ltshell commandsgt ) not required,
but good practice ltshell commandsgt esac
22
while structure
while ltconditiongt do ltshell commandsgt done
23
while a practical example
24
while loop through the command line arguments
number of command arguments
shift move to the next argument
25
for structure
for ltvar gt in LIST do ltshell commandsgt done
26
function
  • Why do we need functions in the program
    structure?
  • Software reuse
  • modular design

Function Declaration define Function
Invocation call/use it
27
a telnet script
28
AWKAho, Kernighan, Weinberger
Rule never change the order
It is a programming language for pattern patch
and more
29
AWK Program Structure
awk BEGIN statements pattern1
statements pattern2 statements END
statements INPUTDATA
30
Awk Program Statements
  • Almost the same as C (or Java)
  • if
  • while
  • do

31
One-Line Awk Program
32
Essential Features
  • Automatic Input Separation
  • Pattern Match
  • String Processing
  • Associative Array

33
Input Data Separation
34
Pattern Match - Regular Expression
  • Regular expression ( r )
  • Pattern match ltexpressiongt / r /
  • Example 6 /tdc511/
  • Example 7 /bash/
  • Example 6 ! /tdc511/ does not match

35
Pattern Match regular expression
36
String Processing
37
String Processing
38
String Processing
39
Associative Array
  • One of the most powerful features in Awk (and
    also in Perl)
  • It is known as hashing in Perl
  • In traditional programming language, array index
    must be an integer and must be in sequence
  • Associative array use character string as array
    index
  • studentIDjohn 1234
  • studentGradeMarytdc511 A-

40
A non-trivial programCompute the Grade Point
Average
41
Awk program
42
Homework 2
  • Task Automation
  • performance test with ping

43
Output of ping
Round Trip Delay 0.728 ms Jitter (mdev)
0.328 ms
44
One-line script for data collection and
formatting
45
Script Prolog
!/bin/sh /home/jyu/tdc597/hw02/hw02script.sh
Author James Yu Date 11/27/06
Description compute the round trip delay with
various packets Usage hw02script.sh ltno
command line argumentsgt
You must have a prolog at the beginning of your
script.
Write a Comment
User Comments (0)
About PowerShow.com