Chapter Seven Advanced Shell Programming. 2 Lesson A Developing a Fully Featured Program.

Slides:



Advertisements
Similar presentations
CHAPTER 1: AN OVERVIEW OF COMPUTERS AND LOGIC. Objectives 2  Understand computer components and operations  Describe the steps involved in the programming.
Advertisements

Introduction to C Programming
A Guide to Unix Using Linux Fourth Edition
ITEC113 Algorithms and Programming Techniques
Chapter 8 and 9 Review: Logical Functions and Control Structures Introduction to MATLAB 7 Engineering 161.
Linux+ Guide to Linux Certification, Second Edition
Chapter 5: More on the Selection Structure Programming with Microsoft Visual Basic.NET, Second Edition.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
JavaScript, Third Edition
Programming Logic and Design, Introductory, Fourth Edition1 Understanding Computer Components and Operations (continued) A program must be free of syntax.
Guide To UNIX Using Linux Third Edition
Guide To UNIX Using Linux Third Edition
Guide to Linux Installation and Administration, 2e1 Chapter 6 Using the Shell and Text Files.
Chapter 1 Program Design
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
Introduction to C Programming
Introduction to Unix (CA263) Introduction to Shell Script Programming By Tariq Ibn Aziz.
Chapter 3 Planning Your Solution
The Program Design Phases
Fundamentals of Python: From First Programs Through Data Structures
Chapter 8: String Manipulation
Advanced File Processing
Advanced Shell Programming. 2 Objectives Use techniques to ensure a script is employing the correct shell Set the default shell Configure Bash login and.
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
Fundamentals of Python: First Programs
CIS162AD - C# Decision Statements 04_decisions.ppt.
Introduction to Shell Script Programming
Week 7 Working with the BASH Shell. Objectives  Redirect the input and output of a command  Identify and manipulate common shell environment variables.
Chapter 6: Shell Programming
An Introduction to Unix Shell Scripting
Chapter 12: How Long Can This Go On?
Input, Output, and Processing
Shell Script Programming. 2 Using UNIX Shell Scripts Unlike high-level language programs, shell scripts do not have to be converted into machine language.
Advanced File Processing. 2 Objectives Use the pipe operator to redirect the output of one command to another command Use the grep command to search for.
Linux+ Guide to Linux Certification, Third Edition
Linux Operations and Administration
Chapter Five Advanced File Processing Guide To UNIX Using Linux Fourth Edition Chapter 5 Unix (34 slides)1 CTEC 110.
Chapter Five Advanced File Processing. 2 Objectives Use the pipe operator to redirect the output of one command to another command Use the grep command.
Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell.
C++ Programming Language Lecture 2 Problem Analysis and Solution Representation By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Chapter 9 I/O Streams and Data Files
Chapter 5: More on the Selection Structure Programming with Microsoft Visual Basic 2005, Third Edition.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
ITEC113 Algorithms and Programming Techniques
Chapter Five Advanced File Processing. 2 Lesson A Selecting, Manipulating, and Formatting Information.
Chapter Six Introduction to Shell Script Programming.
Lesson 3-Touring Utilities and System Features. Overview Employing fundamental utilities. Linux terminal sessions. Managing input and output. Using special.
Java Programming Fifth Edition Chapter 5 Making Decisions.
Chapter 5: Making Decisions. Objectives Plan decision-making logic Make decisions with the if and if…else structures Use multiple statements in if and.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
Agenda Positional Parameters / Continued... Command Substitution Bourne Shell / Bash Shell / Korn Shell Mathematical Expressions Bourne Shell / Bash Shell.
The Hashemite University Computer Engineering Department
Chapter Topics 2.1 Designing a Program 2.2 Output, Input, and Variables 2.3 Variable Assignment and Calculations 2.4 Variable Declarations and Data Types.
Linux+ Guide to Linux Certification, Second Edition
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Lesson 6-Using Utilities to Accomplish Complex Tasks.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
An Introduction to Programming with C++1 The Selection Structure Tutorial 6.
Linux Administration Working with the BASH Shell.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
Programming Logic and Design Seventh Edition Chapter 1 An Overview of Computers and Programming.
Lesson 5-Exploring Utilities
Topics Designing a Program Input, Processing, and Output
The Selection Structure
Designing and Debugging Batch and Interactive COBOL Programs
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Chapter 1 c++ structure C++ Input / Output
Presentation transcript:

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

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

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

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 (>) 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 user’s 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 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

34

35 Protecting Against Entering Duplicate 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 file’s 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

43