Title: TDC597 Linuxbased Network Management Lecture Two
1TDC597Linux-based Network ManagementLecture
Two
- James T. Yu, Ph.D.
- jyu_at_cs.depaul.edu
- School of CTI
- DePaul University
2Lecture Outline
- Shell Programming
- Input/Output
- Data Structures
- Control Structures
- Modular Design
- Awk
- Pattern Match
- Associative Array (hashing)
3What 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
4Your First Shell Program
5How 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
6File I/O
7Shell 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.
8Shell Variables(outside the program scope)
X and Y are also available to other Shell
Programs
9Simple Arithmetic
10Simple Command I/O
Read data from the keyboard The output goes to
the screen.
11Program Structure
- Sequence you already learned it
- Conditions
- comparison and boolean operation
- if
- case
- Loop
- while
- for
12if 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
13Comparison
14if-condition
15More Comparison
16Shell Command Arguments
17Get Input from Command Line
1 1st parameter 2 2nd parameter
18File Test
-s ltfilegt file exists with size gt 0 -f ltfilegt
file exists
19File Test (cont.)
test ltexpgt same as ltexpgt
-d is it a directory?
20Save Execution to a Variable
The special character (back quote) is to save
the execution result into a variable.
21case
case variable in v1) ltshell commandsgt
v2) ltshell commandsgt ) not required,
but good practice ltshell commandsgt esac
22while structure
while ltconditiongt do ltshell commandsgt done
23while a practical example
24while loop through the command line arguments
number of command arguments
shift move to the next argument
25for structure
for ltvar gt in LIST do ltshell commandsgt done
26function
- Why do we need functions in the program
structure? - Software reuse
- modular design
Function Declaration define Function
Invocation call/use it
27a telnet script
28AWKAho, Kernighan, Weinberger
Rule never change the order
It is a programming language for pattern patch
and more
29AWK Program Structure
awk BEGIN statements pattern1
statements pattern2 statements END
statements INPUTDATA
30Awk Program Statements
- Almost the same as C (or Java)
- if
- while
- do
31One-Line Awk Program
32Essential Features
- Automatic Input Separation
- Pattern Match
- String Processing
- Associative Array
33Input Data Separation
34Pattern Match - Regular Expression
- Regular expression ( r )
- Pattern match ltexpressiongt / r /
- Example 6 /tdc511/
- Example 7 /bash/
- Example 6 ! /tdc511/ does not match
35Pattern Match regular expression
36String Processing
37String Processing
38String Processing
39Associative 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-
40A non-trivial programCompute the Grade Point
Average
41Awk program
42Homework 2
- Task Automation
- performance test with ping
43Output of ping
Round Trip Delay 0.728 ms Jitter (mdev)
0.328 ms
44One-line script for data collection and
formatting
45Script 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.