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

43
Chapter Seven Chapter Seven Advanced Shell Advanced Shell Programming Programming

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

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

Chapter SevenChapter Seven

Advanced Shell Advanced Shell ProgrammingProgramming

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

22

Lesson ALesson A

Developing aDeveloping a

Fully Featured ProgramFully Featured Program

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

33

ObjectivesObjectives

Use flowcharting and pseudocode toolsUse flowcharting and pseudocode tools

Learn to write scripts that tell the system which Learn to write scripts that tell the system which shell to use as an interpretershell to use as an interpreter

Use the test command to compare values and Use the test command to compare values and validate file existence validate file existence

Use the translate command, tr, to display a Use the translate command, tr, to display a record with duplicate fieldsrecord with duplicate fields

Use the sed command in a script to delete Use the sed command in a script to delete phone recordsphone records

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

44

Analyzing the ProgramAnalyzing the Program

A computer program is developed by A computer program is developed by analyzing the best way to achieve the analyzing the best way to achieve the desired results – standard programming desired results – standard programming tools help with this processtools help with this process

The two most popular and proven analysis The two most popular and proven analysis tools are:tools are:– The program flowchartThe program flowchart– PseudocodePseudocode

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

55

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

66

FlowchartingFlowcharting

Each step in the program is represented by a Each step in the program is represented by a symbol in the flowchartsymbol in the flowchart

The shape of the symbol indicates the type of The shape of the symbol indicates the type of operation being performedoperation being performed– Arrows connect the symbols and indicate the direction Arrows connect the symbols and indicate the direction

in which the program flowsin which the program flows– Input and output operations are represented by Input and output operations are represented by

trapezoids and decision structures as diamondstrapezoids and decision structures as diamonds

Flowcharts are manually created using a Flowcharts are manually created using a drawing templatedrawing template

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

77

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

88

Writing PseudocodeWriting Pseudocode

After flowcharting, the next step in designing a After flowcharting, the next step in designing a program is to write pseudocodeprogram is to write pseudocode

Pseudocode instructions are similar to actual Pseudocode instructions are similar to actual programming statements and are used to create programming statements and are used to create a model that is later used as the basis for a real a model that is later used as the basis for a real programprogram

Pseudocode is a design tool only, and never Pseudocode is a design tool only, and never processed by the computer; therefore there are processed by the computer; therefore there are no strict rules to followno strict rules to follow

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

99

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

1010

Ensuring the Correct Shell Ensuring the Correct Shell Runs the ScriptRuns the Script

Since each UNIX user has the freedom to Since each UNIX user has the freedom to choose which shell they will use, it is important choose which shell they will use, it is important to ensure that the correct shell is used to run the to ensure that the correct shell is used to run the scriptscript

This is because all shells do not support the This is because all shells do not support the same commands and programming statementssame commands and programming statements

The first line of the script is where you instruct a The first line of the script is where you instruct a user to run it with a specific shelluser to run it with a specific shell

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

1111

Using the test CommandUsing the test Command

The test command makes preliminary checks of The test command makes preliminary checks of the UNIX internal environment and other useful the UNIX internal environment and other useful comparisonscomparisons

Place the test command inside the shell script or Place the test command inside the shell script or execute it directly from the command lineexecute it directly from the command line

The test command can be used to:The test command can be used to:– Perform relational tests with integersPerform relational tests with integers– Test stringsTest strings– Determine if a file exists and what type of file it isDetermine if a file exists and what type of file it is– Perform Boolean testsPerform Boolean tests

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

1212

Relational Integer TestsRelational 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)

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

1313

String TestsString Tests

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

1414

Testing FilesTesting Files

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

1515

Performing Boolean TestsPerforming 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

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

1616

Using the test CommandUsing the test Command

The content of this file was created in part due to using the test command to determine if a directory existed

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

1717

Using the test CommandUsing 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

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

1818

Formatting Record OutputFormatting Record Output

Record output is formatted using the Record output is formatted using the translate utility (tr)translate utility (tr)

Use tr to:Use tr to:– Change the characters typed at the keyboard, Change the characters typed at the keyboard,

character by charactercharacter by character– Work as a filter when the input comes from Work as a filter when the input comes from

the output of another UNIX commandthe output of another UNIX command– Redirect standard input to come from a file Redirect standard input to come from a file

rather than the keyboardrather than the keyboard

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

1919

Formatting Record OutputFormatting Record Output

tr was used to change lowercase characters to uppercase and replace colon characters with spaces

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

2020

Formatting Record OutputFormatting Record Output

tr was used to help format the record output of the program

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

2121

Deleting Phone RecordsDeleting Phone Records

Bash shell operators are in three groups:Bash shell operators are in three groups:– Defining and Evaluating operators are used to set a Defining and Evaluating operators are used to set a

variable to a value and to check variable valuesvariable to a value and to check variable valuesThe equal sign (=) is an exampleThe equal sign (=) is an example

– Arithmetic operators are used to perform Arithmetic operators are used to perform mathematical equationsmathematical equations

The plus sign (+) is an exampleThe plus sign (+) is an example

– Redirecting and piping operators are used to specify Redirecting and piping operators are used to specify input and output data specificationsinput and output data specifications

The greater than sign (>) is an exampleThe greater than sign (>) is an example

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

2222

Deleting Phone RecordsDeleting Phone Records

The menu has been updated to allow for deleting a phone record

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

2323

Deleting Phone RecordsDeleting Phone Records

Examine the corp_phones file before deleting a record

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

2424

Deleting Phone RecordsDeleting Phone Records

The sed command is behind the delete option

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

2525

Deleting Phone RecordsDeleting Phone Records

The record is no longer in the file

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

2626

Lesson BLesson B

Completing the Case ProjectCompleting the Case Project

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

2727

ObjectivesObjectivesSet up a quick screen-clearing techniqueSet up a quick screen-clearing technique

Create a program algorithm to solve a cursor Create a program algorithm to solve a cursor repositioning problemrepositioning problem

Develop and test a program to reenter fields during Develop and test a program to reenter fields during data entrydata entry

Develop and test a program to eliminate duplicate Develop and test a program to eliminate duplicate recordsrecords

Create shell functions and use them in a programCreate shell functions and use them in a program

Load shell functions automatically when you log inLoad shell functions automatically when you log in

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

2828

Clearing the ScreenClearing the Screen

The clear command is a useful housekeeping The clear command is a useful housekeeping utility for clearing the screen before new screens utility for clearing the screen before new screens appear, but there is a faster wayappear, but there is a faster way

You can clear the screen faster by storing the You can clear the screen faster by storing the output of the clear command in a variable and output of the clear command in a variable and then echoing the contents of the variable on the then echoing the contents of the variable on the screenscreen– This works about ten times faster than the actual This works about ten times faster than the actual

command since the system does not have to locate command since the system does not have to locate and execute the clear commandand execute the clear command

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

2929

Moving the CursorMoving the Cursor

You can allow a user to correct data entered into You can allow a user to correct data entered into a previous data entry field. This will involve a previous data entry field. This will involve moving the cursor from one field to anothermoving the cursor from one field to another

One option would make the minus sign (-) the One option would make the minus sign (-) the user’s means to move back a fielduser’s means to move back a field– If a user enters a minus and hits enter, the cursor is If a user enters a minus and hits enter, the cursor is

repositioned at the start of the previous fieldrepositioned at the start of the previous field– To accomplish this, the first step is to create a To accomplish this, the first step is to create a

program algorithm program algorithm

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

3030

Creating Program Creating Program AlgorithmsAlgorithms

An algorithm is a sequence of commands or An algorithm is a sequence of commands or instructions that produces a desired resultinstructions that produces a desired result

A good practice for creating an algorithm would A good practice for creating an algorithm would be to develop both the logic shown in a flowchart be to develop both the logic shown in a flowchart and the expressed conditions necessary to carry and the expressed conditions necessary to carry out the logic described in the pseudocodeout the logic described in the pseudocode

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

3131

Creating Program Creating Program AlgorithmsAlgorithms

Incorrect information has been entered by the user

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

3232

Creating Program Creating Program AlgorithmsAlgorithms

The algorithm has encountered a minus sign and moved the cursor to the previous field

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

3333

Protecting Against EnteringProtecting Against EnteringDuplicate Phone NumbersDuplicate Phone Numbers

Input validation is necessary because Input validation is necessary because users don’t always enter valid datausers don’t always enter valid data

Programs should always check to ensure Programs should always check to ensure that users enter acceptable informationthat users enter acceptable information

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

3434

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

3535

Protecting Against EnteringProtecting Against EnteringDuplicate Phone NumbersDuplicate Phone Numbers

The phoneadd program now does input validation

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

3636

Using Shell FunctionsUsing Shell Functions

A shell function is a group of commands that is A shell function is a group of commands that is stored in memory and assigned a namestored in memory and assigned a name

Shell scripts can use function names to execute Shell scripts can use function names to execute the commandsthe commands

Shell functions are used to isolate reusable code Shell functions are used to isolate reusable code sections, so there is no need to duplicate the sections, so there is no need to duplicate the same algorithm throughout your programsame algorithm throughout your program

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

3737

Reusing CodeReusing Code

To improve programming productivity, learn to To improve programming productivity, learn to reuse codereuse code

This means that functions and programs This means that functions and programs developed should be shared with other developed should be shared with other programs and functions as much as possibleprograms and functions as much as possible

This practice helps to prevent duplications, save This practice helps to prevent duplications, save time, and reduce errorstime, and reduce errors

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

3838

Sorting the Phone ListSorting the Phone List

The code that generated this list includes a shell script which contains sort functions

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

3939

Chapter SummaryChapter Summary

The two most popular and proven analysis The two most popular and proven analysis tools are the program flowchart and tools are the program flowchart and pseudocodepseudocode

Pseudocode is a model of a programPseudocode is a model of a program

Use the first line in a script file to tell the OS Use the first line in a script file to tell the OS which shell to use when interpreting the scriptwhich shell to use when interpreting the script

Use the test command to validate the Use the test command to validate the existence of directories and files as well as existence of directories and files as well as compare numeric and string values compare numeric and string values

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

4040

Chapter SummaryChapter Summary

The translate utility (tr) changes characters The translate utility (tr) changes characters typed at the keyboard and also works as a filter typed at the keyboard and also works as a filter when input comes from the output of another when input comes from the output of another UNIX commandUNIX command

The sed command reads a file as its input and The sed command reads a file as its input and outputs the file’s modified contentoutputs the file’s modified content

To speed clearing the screen, assign the clear To speed clearing the screen, assign the clear command sequence to the shell variable command sequence to the shell variable CLEAR that can be set inside the login scriptCLEAR that can be set inside the login script

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

4141

Chapter SummaryChapter Summary

An algorithm is a sequence of An algorithm is a sequence of instructions or commands that produces instructions or commands that produces a desired resulta desired result

Shell functions can simplify program Shell functions can simplify program code by isolating code that can be code by isolating code that can be reused throughout one or many reused throughout one or many programsprograms

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

4242

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

4343