1 / 43

Chapter Seven

Chapter Seven. Advanced Shell Programming. Lesson A. Developing a Fully Featured 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

Télécharger la présentation

Chapter Seven

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

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

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

  9. 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)

  10. String Tests

  11. Testing Files

  12. 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

  13. 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

  14. 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

  15. 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

  16. Formatting Record Output tr was used to change lowercase characters to uppercase and replace colon characters with spaces

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

  18. 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

  19. Deleting Phone Records The menu has been updated to allow for deleting a phone record

  20. Deleting Phone Records Examine the corp_phones file before deleting a record

  21. Deleting Phone Records The sed command is behind the delete option

  22. Deleting Phone Records The record is no longer in the file

  23. Lesson B Completing the Case Project

  24. 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

  25. 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

  26. 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

  27. 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

  28. Creating Program Algorithms Incorrect information has been entered by the user

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

  30. Protecting Against EnteringDuplicate 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

  31. Protecting Against EnteringDuplicate Phone Numbers The phoneadd program now does input validation

  32. 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

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

  34. Sorting the Phone List The code that generated this list includes a shell script which contains sort functions

  35. 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

  36. 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

  37. 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

More Related