1 / 49

Introduction to Shell Script Programming

2. Lesson A. Using the UNIX Shell as aScripting Language. 3. Objectives. Understand the program development cycle using a high-level computer language and UNIX shell scriptsCompare the shells to determine the best choice for creating scriptsLearn about shell variables, operators, and wildcard charactersWrite simple shell scripts to illustrate programming logic.

johana
Download Presentation

Introduction to Shell Script Programming

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. Introduction to Shell Script Programming

    2. 2 Lesson A Using the UNIX Shell as a Scripting Language

    3. 3 Objectives Understand the program development cycle using a high-level computer language and UNIX shell scripts Compare the shells to determine the best choice for creating scripts Learn about shell variables, operators, and wildcard characters Write simple shell scripts to illustrate programming logic

    4. 4 The Program Development Cycle The program development cycle is the process of developing an application The first step in the cycle is to create program specifications The second step in the cycle is to create the program design The third step is developing the code, which is written, tested, and debugged

    5. 5

    6. 6 Using High-Level Languages High-level languages are computer languages that use English-like expressions Example are; COBOL, C, C++ A programs high-level language statements are stored in a file called the source file, which programmers creates using editors In order to execute, high-level source files must be converted into a low-level machine language file

    7. 7 Using High-Level Languages A compiler is a program that converts source files into executable machine-language files The complier reads the lines of code the programmer wrote in the source file and converts them to the appropriate machine language instructions If a source file contains syntax errors, it cannot be converted into an executable file A programmer must correct these errors before the program can be run

    8. 8 Using UNIX Shell Scripts Unlike high-level language programs, shell scripts do not have to be converted into machine language by a compiler The UNIX shell acts as an interpreter when reading script files Interpreters read statements in script files and immediately translate them into executable instructions and cause them to run

    9. 9 Using UNIX Shell Scripts After creating shell script, the OS is instructed that the file is an executable shell script via the chmod command When the file is designated as executable, you may it run in one of many ways: Type the script name at the command prompt after updating the path variable If the script is in the current directory, proceed its name at the prompt with a dot slash (./) If not in the current directory, specify the absolute path at the command prompt

    10. 10 The Programming Shell

    11. 11 Variables Variables are symbolic names that represent values stored in memory Three types of variables are: Configuration variables store information about the setup of the OS Environment variables hold information about your login session Shell variables are created at the command prompt or in shell scripts and are used to temporarily store information

    12. 12 Variables

    13. 13

    14. 14

    15. 15

    16. 16 Shell Operators 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

    17. 17 Shell Operators

    18. 18 Shell Operators

    19. 19 More About Wildcard Characters Shell scripts often use wildcard characters Wildcard characters are also known as glob characters and are a part of glob patterns Glob patterns are intended to match filenames and words Question mark (?) matches exactly one character Asterisk (*) matches zero or more characters [chars] defines a class of characters, the glob pattern matches any singles character in the class

    20. 20 Shell Logic Structures Four basic logic structures needed for program development are: Sequential logic Decision logic Looping logic Case logic

    21. 21 Sequential Logic Works so that commands are executed in the order in which they appear in the script or program The only break in this sequence comes when a branch instruction changes the flow of execution by redirecting to another location in the script or program Used for simple, straightforward command sequences

    22. 22 Decision Logic Enables your script or program to execute a statement or series of statements only if a certain condition exists In many cases, the condition depends upon the result of a command or on a comparison The if statement is the primary decision-making control structure in this type of logic

    23. 23 Decision Logic

    24. 24 Decision Logic

    25. 25 Looping Logic A control structure repeats until some condition exists or some action occurs Two common looping mechanisms: For loops cycle through a range of values until the last in a set of values is reached The while loop cycles as long as a particular condition exists

    26. 26 Looping Logic

    27. 27 Looping Logic

    28. 28 Looping Logic

    29. 29 Looping Logic

    30. 30 Looping Logic

    31. 31 Case Logic The case logic structure simplifies the selection from a list of choices It allows the script to perform one of many actions, depending on the value of a variable Two semicolons (;;) terminate the actions taken after the case matches what is being tested

    32. 32 Case Logic

    33. 33 Debugging a Shell Script Shell script will not execute if there is an error in one or more commands Running a shell script using sh, plus one of its options, allows for more quickly debugging of the problem sh -v option displays lines of code in the script as they are read by the interpreter sh -x option displays the command and its arguments line by line as they are run

    34. 34 Debugging a Shell Script

    35. 35 Lesson B Creating and Completing the Corporate Phone Application

    36. 36 Objectives Create screen-management scripts Learn how to edit the .bashrc file to customize your personal working environment Use the trap command Enter and test shell scripts to print the phone records, view the contents of the corp_phone file, and add new phone records to the file

    37. 37 Using Shell Scripting to Create a Menu A menu is a good example of a shell script that employs the four basic logic structures A significant feature of the menu script is the screen presentation which should be as appealing and user-friendly as possible The tput command helps you create a better screen presentation tput initializes the terminal to respond to a setting that the user chooses

    38. 38 Creating a Menu

    39. 39 Creating a Menu

    40. 40 Creating a Menu

    41. 41 Customizing Your Personal Environment When programming and shell scripting, customizing your environment by modifying the initial settings in the login scripts provides many benefits Login scripts run just after logging in Setting up personal bin directories and modify editor defaults are common customizations

    42. 42 Customizing Your Personal Environment An alias is a name that represents another command and is a way to automate frequently used commands and their options The .bashrc file that resides in your home directory is often used to establish customizations that take effect at each login The .bashrc script is executed each time a shell is generated, such as when shell scripts are run

    43. 43 The trap Command The trap command is useful in that it causes your shell program to automatically remove temporary files created when shell scripts run Programmers often set up a subdirectory of a users home directory to store temporary files, and when a script file exits, the trap command can specify to remove the files Having files removed from a temporary directory like this is considered good housekeeping on the part of the programmer

    44. 44 Creating the corp_phones File

    45. 45 Creating the corp_phones File

    46. 46 Creating the phoneadd Shell Script

    47. 47 Chapter Summary A high-level language uses English-like expressions and must be converted into a low-level language before being executed The shell interprets shell scripts UNIX shell script instructions do not need to be written from scratch, they are chosen from an inventory of executable commands Linux shells are derived from the UNIX Bourne, Korn and C shells, and bash is the default UNIX employs three types of variables: configuration, environment, and shell

    48. 48 Chapter Summary The shell supports numerous operators, including many for performing arithmetic operations Wildcard characters are used in shell scripts The logic structures supported by the shell are sequential, decision, looping and case The tput command can be used to manage cursor placement on the screen Programmers and system administrators often customize the .bashrc file to suit their needs

    49. 49 Chapter Summary Aliases, used to simplify commonly used commands, can be entered into the .bashrc Use the trap command to remove temporary files after the script exits The grep command serves a key role in the development of shell scripts by allowing searching and retrieving data from files The awk command serves as an effective and easy-to-use tool for generating reports

    50. 50

More Related