Topic 4.2 Topic 4.2 Advanced Shell Programming

advertisement
2014/10/6
Topic 4.2 Advanced Shell
Programming
Analyzing the Program
Objectives:
Use flowcharting and pseudocode tools
Learn to write scripts that tell the system
which shell to use as an interpreter
Use the test command to compare values
and validate file existence
Use the translate command, tr, to display a
record with duplicate fields
Use the sed command in a script to delete
phone records
A computer program is developed by
analyzing the best way to achieve the
desired results – standard
programming tools help with this
process
DPR355 1403
The two most popular and proven
analysis tools are:
– The program flowchart
– Pseudocode
1
UNIX Shell Script Programming
DPR355 1403
UNIX Shell Script Programming
2
Flowcharting
Each step in the program is represented by
a symbol in the flowchart
The shape of the symbol indicates the type
of operation being performed
– Arrows connect the symbols and indicate the
direction in which the program flows
– Input and output operations are represented by
trapezoids and decision structures as diamonds
Flowcharts are manually created using a
drawing template
Figure 7-1 (Palmer)
DPR355 1403
UNIX Shell Script Programming
A
Writing Pseudocode
Decision
B
After flowcharting, the next step in designing
a program is to write pseudocode
Connector
C
D
Magnetic disk
E
Input/Output
operation
F
Terminator
DPR355 1403
DPR355 1403
Process
Offpage connector
DPR355 0901
1403
3
UNIX Shell Script Programming
UNIX Shell Script Programming
Operating Systems
G
Figure 7-2 (Palmer)
4
Pseudocode instructions are similar to
actual programming statements and are
used to create a model that is later used as
the basis for a real program
Pseudocode is a design tool only, and never
processed by the computer; therefore there
are no strict rules to follow
55
DPR355 1403
UNIX Shell Script Programming
6
1
2014/10/6
Ensuring the Correct Shell
Runs the Script
Since each UNIX user has the freedom to
choose which shell they will use, it is important
to ensure that the correct shell is used to run
the script
This is because all shells do not support the
same commands and programming statements
The first line of the script is where you instruct
the shell or a user to run it with a specific shell
#!/bin/bash
(Palmer)
DPR355 1403
UNIX Shell Script Programming
7
Using the test Command
DPR355 1403
8
UNIX Shell Script Programming
Relational Integer Tests
(Palmer)
The test command makes preliminary
checks of the UNIX internal environment
and other useful comparisons
Place the test command inside the shell
script or execute it directly from the
command line
The test command can be used to:
–
–
–
–
The test command returns a value known as an
exit status, which is a numeric value and indicates
the results of the test performed: true if 0 (zero)
and false if 1 (one)
Perform relational tests with integers
Test strings
Determine if a file exists and what type of file it is
Perform Boolean tests
DPR355 1403
UNIX Shell Script Programming
9
DPR355 1403
10
UNIX Shell Script Programming
Testing Strings
String Tests With the Test
Command
You can use the test command to determine
if a string has a length of zero characters or a
non-zero number of characters
To demonstrate the test command’s string
testing capabilities:
(Palmer)
1. Type name=“Bjorn” and press Enter
2. Type test $name = “Bjorn” and press Enter
3. Type echo $? and press Enter
4. Type test $name != “Bjorn” and press Enter
5. Type echo $? and press Enter
6. Type test -z $name and press Enter
7. Type echo $? and press Enter
DPR355 1403
DPR355 1403
UNIX Shell Script Programming
Operating Systems
11
DPR355 1403
UNIX Shell Script Programming
12
2
2014/10/6
Testing Files or Directories
Performing Boolean Tests
(Palmer)
(Palmer)
The -a operator (AND)– returns true (0) if both
expressions are true, otherwise returns false (1)
test expression1 -a expression 2
The –o operator (OR) – returns true if either expression
is true, otherwise if neither is true, returns false
The ! operator – negates the value of the expression
NOTE: Test a single file or a single directory
DPR355 1403
13
UNIX Shell Script Programming
Using the test Command in a
Script File
In an if structure, [ $guess != “red” ] is equivalent
to the test statement test $guess != “red”
UNIX Shell Script Programming
– Change the characters typed at the
keyboard, character by character
– Work as a filter when the input comes
from the output of another UNIX command
Redirect standard input to come from a
file rather than the keyboard
tr [a-z] [A-Z] < counters
cat corp_phones | tr ’:’ ’ ’
15
Operating Systems
UNIX Shell Script Programming
16
The two most popular and proven analysis
tools are the program flowchart and
pseudocode
You can use the first line in a script file to tell
the operating system which shell to use
when interpreting the script
You can use the test command to validate
the existence of directories and files as well
as compare numeric and string values
The translate utility (tr) changes the
characters typed at the keyboard, character
by character, and also works as a filter when
the input comes from the output of another
UNIX command
Reading: Chapter 7A (Palmer)
(Palmer)
DPR355 1403
DPR355 1403
So far, we have learned…
tr was used to
change
lowercase
characters to
uppercase and
replace colon
characters with
spaces
UNIX Shell Script Programming
14
Record output can be formatted using
the translate utility (tr)
Use tr to:
Formatting Record Output
DPR355 1403
UNIX Shell Script Programming
Formatting Record Output
.bashrc
# Source global definitions
# test if any global definition existed
if [ -f /etc/bashrc ]; then
# if test -f /etc/bashrc; then
. /etc/bashrc
# if yes, run it first: sh /etc/bashrc
fi
samplescript.sh
#!/bin/bash
if [ ! -w . ] # if test ! -w . or if ! test -w .
...
# do these if the directory is NOT writable
fi
if [ ! -r *.txt ] # MORE than one file, NO test equivalent
...
# do these if NO *.txt file existed and readable
fi
DPR355 1403
DPR355 1403
17
DPR355 1403
UNIX Shell Script Programming
18
3
2014/10/6
Topic 4.2 Advanced Shell
Programming (Part B)
Clearing the Screen
Objectives: (cont’)
Set up a quick screen-clearing technique
Create a program algorithm to solve a cursor repositioning
problem
Develop and test a program to reenter fields during data entry
– This works about ten times faster than the actual command
since the system does not have to locate and execute the
clear command
ex.
[@]$ CLEAR=`clear`
echo $CLEAR
Develop and test a program to eliminate duplicate records
Create shell functions and use them in a program
Load shell functions automatically when you log in
DPR355 1403
19
UNIX Shell Script Programming
An Example of Creating Program
Algorithms (1)
20
UNIX Shell Script Programming
You can allow a user to correct data
entered into a previous data entry field.
This will involve moving the cursor from
one field to another
One option would be to make the minus
sign (-) the user’s means to move back
one field
– If a user enters a minus and hits enter, the
cursor is repositioned at the start of the
previous field
– To accomplish this, the first step is to create
a program algorithm
(Palmer)
UNIX Shell Script Programming
DPR355 1403
Moving the Cursor
Incorrect
information has
been entered by
the user
DPR355 1403
The clear command is a useful
housekeeping utility for clearing the screen
before new screens appear, but there is a
faster way
You can clear the screen faster by storing
the output of the clear command in a
variable and then echoing the contents of
the variable on the screen
21
DPR355 1403
22
UNIX Shell Script Programming
An Example of Creating
Program Algorithms (2)
Creating Program Algorithms
An algorithm is a sequence of commands or
instructions that produces a desired result
A good practice for creating an algorithm would
be to develop both the logic shown in a
flowchart and the expressed conditions
necessary to carry out the logic described in
the pseudocode
The algorithm has
encountered a
minus sign and
moved the cursor
to the previous
field.
Next, is the new
phone number a
duplicate number
or a ‘q’?
See Slide 7 for a
bigger figure.
(Palmer)
DPR355 1403
DPR355 1403
UNIX Shell Script Programming
Operating Systems
23
DPR355 1403
UNIX Shell Script Programming
24
4
2014/10/6
Protecting Against Entering
Duplicate Phone Numbers
Input validation is necessary because
users don’t always enter valid data
Programs should always check to
ensure that users enter acceptable
information
(Palmer)
DPR355 1403
25
UNIX Shell Script Programming
Example Screen of Entering
Duplicate Phone Numbers
DPR355 1403
26
UNIX Shell Script Programming
Using Shell Functions
A shell function is a group of
commands that is stored in memory
and assigned with a name
Shell scripts can use function names to
execute the commands
Shell functions are used to isolate
reusable code sections, so there is no
need to duplicate the same algorithm
throughout your program
datenow( )
The phoneadd
program now
does input
validation
{
date
(Palmer)
DPR355 1403
UNIX Shell Script Programming
}
27
DPR355 1403
28
UNIX Shell Script Programming
Sorting the Phone List
Reusing Code
To improve programming productivity, learn
to reuse code
This means that functions and programs
developed should be shared with other
programs and functions as much as possible
The code that generated this list includes a shell script which
contains sort functions.
sort_name()
This practice helps to prevent duplications,
save time, and reduce errors
{
sort +1 –t: corp_phones
}
sort_name > sorted_phones
awk -F: '{ printf "%-12s %-12s %s\t%s %s %10.10s %s\n",
$2, $3, $4, $1, $5, $6, $7 }' sorted_phones
DPR355 1403
DPR355 1403
UNIX Shell Script Programming
Operating Systems
29
DPR355 1403
UNIX Shell Script Programming
30
5
2014/10/6
Summary of the Topic
The two most popular and proven analysis tools are the program
flowchart and pseudocode
Use the first line in a script file to tell the OS which shell to use
when interpreting the script
Use the test command to validate the existence of directories
and files as well as compare numeric and string values
The translate utility (tr) can be used to reformatting the output
The sed command reads a file as its input and outputs the file’s
modified content (e.g. Deleting a phone record from a file)
To speed clearing the screen, assign the clear command
sequence to the shell variable CLEAR that can be set inside the
login script
An algorithm is a sequence of instructions or commands that
produces a desired result
Shell functions can simplify program code by isolating code that
can be reused throughout one or many programs
Reading: Chapter 7 (Palmer)
DPR355 1403
31
UNIX Shell Script Programming
DPR355 1403
UNIX Shell Script Programming
(Palmer)
32
Example: os_choice
echo -n "What is your favorite operating system? "
read OS_NAME
if [ "$OS_NAME" = "UNIX" ]
then
echo "You will like Linux."
else
echo "You should give Linux a try!"
fi
********************************************************
( read TMPNAME
OS_NAME=`echo $TMPNAME | tr [a-z] [A-Z]` )
if [ "$OS_NAME" = "UNIX" -o "$OS_NAME" = "UNix" \
-o "$OS_NAME" = “Unix“ -o "$OS_NAME" = "unix“ -o ... ]
DPR355 1403
DPR355 1403
UNIX Shell Script Programming
Operating Systems
(Palmer)
33
DPR355 1403
UNIX Shell Script Programming
34
6
Download