430 likes | 440 Views
Learn to write scripts, analyze program flow, and utilize shell commands effectively. Includes pseudocode, flowcharting, and testing file existence.
E N D
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 • Use the translate command, tr, to display a record with duplicate fields • Use the sed command in a script to delete phone records
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
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
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
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
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
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)
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
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
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
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
Formatting Record Output tr was used to change lowercase characters to uppercase and replace colon characters with spaces
Formatting Record Output tr was used to help format the record output of the program
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
Deleting Phone Records The menu has been updated to allow for deleting a phone record
Deleting Phone Records Examine the corp_phones file before deleting a record
Deleting Phone Records The sed command is behind the delete option
Deleting Phone Records The record is no longer in the file
Lesson B Completing the Case Project
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
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
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
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
Creating Program Algorithms Incorrect information has been entered by the user
Creating Program Algorithms The algorithm has encountered a minus sign and moved the cursor to the previous field
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
Protecting Against EnteringDuplicate Phone Numbers The phoneadd program now does input validation
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
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
Sorting the Phone List The code that generated this list includes a shell script which contains sort functions
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
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
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