Chapter Seven - PowerPoint PPT Presentation

About This Presentation
Title:

Chapter Seven

Description:

Learn to write scripts that tell the system which shell to use as an interpreter ... Record output is formatted using the translate utility (tr) Use tr to: ... – PowerPoint PPT presentation

Number of Views:62
Avg rating:3.0/5.0
Slides: 44
Provided by: til4
Learn more at: http://faculty.bucks.edu
Category:
Tags: chapter | seven | use

less

Transcript and Presenter's Notes

Title: Chapter Seven


1
Chapter Seven
  • Advanced Shell Programming

2
Lesson A
  • Developing a
  • Fully Featured Program

3
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

4
Analyzing the Program
  • A computer program is developed by analyzing the
    best way to achieve the desired results
    standard programming tools help with this process
  • The two most popular and proven analysis tools
    are
  • The program flowchart
  • Pseudocode

5
(No Transcript)
6
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

7
(No Transcript)
8
Writing Pseudocode
  • After flowcharting, the next step in designing a
    program is to write pseudocode
  • 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

9
(No Transcript)
10
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 a user to run it with a specific shell

11
Using the test Command
  • 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
  • Perform relational tests with integers
  • Test strings
  • Determine if a file exists and what type of file
    it is
  • Perform Boolean tests

12
Relational Integer Tests
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)
13
String Tests
14
Testing Files
15
Performing Boolean Tests
AND returns true (0) if both expressions are
true, otherwise returns false (1) OR returns
true if either expression is true, otherwise if
neither is true, returns false ! negates the
value of the expression
16
Using the test Command
The content of this file was created in part due
to using the test command to determine if a
directory existed
17
Using the test Command
The output shown here was created in part due to
using the test command to determine a value
entered by a user
18
Formatting Record Output
  • Record output is formatted using the translate
    utility (tr)
  • Use tr to
  • 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

19
Formatting Record Output
tr was used to change lowercase characters to
uppercase and replace colon characters with spaces
20
Formatting Record Output
tr was used to help format the record output of
the program
21
Deleting Phone Records
  • Bash shell operators are in three groups
  • Defining and Evaluating operators are used to set
    a variable to a value and to check variable
    values
  • The equal sign () is an example
  • Arithmetic operators are used to perform
    mathematical equations
  • The plus sign () is an example
  • Redirecting and piping operators are used to
    specify input and output data specifications
  • The greater than sign (gt) is an example

22
Deleting Phone Records
The menu has been updated to allow for deleting a
phone record
23
Deleting Phone Records
Examine the corp_phones file before deleting a
record
24
Deleting Phone Records
The sed command is behind the delete option
25
Deleting Phone Records
The record is no longer in the file
26
Lesson B
  • Completing the Case Project

27
Objectives
  • 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
  • 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

28
Clearing the Screen
  • 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
  • This works about ten times faster than the actual
    command since the system does not have to locate
    and execute the clear command

29
Moving the Cursor
  • 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 make the minus sign (-) the
    users means to move back a 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

30
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

31
Creating Program Algorithms
Incorrect information has been entered by the user
32
Creating Program Algorithms
The algorithm has encountered a minus sign and
moved the cursor to the previous field
33
Protecting Against EnteringDuplicate Phone
Numbers
  • Input validation is necessary because users dont
    always enter valid data
  • Programs should always check to ensure that users
    enter acceptable information

34
(No Transcript)
35
Protecting Against EnteringDuplicate Phone
Numbers
The phoneadd program now does input validation
36
Using Shell Functions
  • A shell function is a group of commands that is
    stored in memory and assigned 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

37
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
  • This practice helps to prevent duplications, save
    time, and reduce errors

38
Sorting the Phone List
The code that generated this list includes a
shell script which contains sort functions
39
Chapter Summary
  • The two most popular and proven analysis tools
    are the program flowchart and pseudocode
  • Pseudocode is a model of a program
  • 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

40
Chapter Summary
  • The translate utility (tr) changes characters
    typed at the keyboard and also works as a filter
    when input comes from the output of another UNIX
    command
  • The sed command reads a file as its input and
    outputs the files modified content
  • To speed clearing the screen, assign the clear
    command sequence to the shell variable CLEAR that
    can be set inside the login script

41
Chapter Summary
  • 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

42
(No Transcript)
43
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com