Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations

Presentation on theme: "Chapter Seven Advanced Shell Programming. 2 Lesson A Developing a Fully Featured Program."— Presentation transcript:

1 Chapter Seven Advanced Shell Programming

2 2 Lesson A Developing a Fully Featured Program

3 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 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 5

6 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 7

8 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 9

10 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 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 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 13 String Tests

14 14 Testing Files

15 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 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 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 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 19 Formatting Record Output tr was used to change lowercase characters to uppercase and replace colon characters with spaces

20 20 Formatting Record Output tr was used to help format the record output of the program

21 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 22 Deleting Phone Records The menu has been updated to allow for deleting a phone record

23 23 Deleting Phone Records Examine the corp_phones file before deleting a record

24 24 Deleting Phone Records The sed command is behind the delete option

25 25 Deleting Phone Records The record is no longer in the file

26 26 Lesson B Completing the Case Project

27 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 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 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 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 31 Creating Program Algorithms Incorrect information has been entered by the user

32 32 Creating Program Algorithms The algorithm has encountered a minus sign and moved the cursor to the previous field

33 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 34

35 35 Protecting Against Entering Duplicate Phone Numbers The phoneadd program now does input validation

36 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 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 38 Sorting the Phone List The code that generated this list includes a shell script which contains sort functions

39 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 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 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 42

43 43

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

Similar presentations

Ads by Google