Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter Six Introduction to Shell Script Programming.

Similar presentations


Presentation on theme: "Chapter Six Introduction to Shell Script Programming."— Presentation transcript:

1 Chapter Six 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 program’s 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 All Linux versions use the Bash shell as the default

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 Use the printenv command to see a list of environment variables

13 13

14 14

15 15

16 16 Variables To set: example=one To see: echo $example To make part of the environment: export example To remove: unsetenv example

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

18 18 Shell Operators

19 19 More About Wildcard Characters Shell scripts often use wildcard characters Wildcard characters 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 –User input –Decision logic –Looping logic –Case logic

21 21 Sequential Logic commands are executed in the order in which they appear in the script break in sequence occurs when a branch instruction changes the flow of execution by redirecting to another location in the script

22 22 User input Script can read user data Command: read variable reads user input and assigns text to variable

23 23 User input Command: read var1 var2 var3 reads 3 words and assigns 3 variables Last variable contains rest of input line

24 24 User input Command: read –p “enter name: “ name Prompts user, then reads input and assigns to variable

25 25 User input example

26 26 Decision Logic Enables your script to execute statement(s) only if a certain condition is true Condition: –result of a command –Comparison of variables or values if statement

27 27 If statement Syntax: if [ condition ] thenstatementselsestatementsfi

28 28 Decision Logic example

29 29 Nested Decision Logic

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

31 31 For Loop Syntax for var in list dostatementsdone

32 32 For Loop example Program control structures can be entered from the command line

33 33 For loop in script

34 34 Loop with wildcard

35 35 While Loop Syntax while [ condition ] dostatementsdone

36 36 Looping Logic The while loop tests repeatedly for a matching condition

37 37 Looping Logic While loops can serve as data-entry forms

38 38 While loop to enter data

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

40 40 Case statement Syntax: case $variable in “pattern1”)statements;;“pattern2”)statements;;esac

41 41 Case example

42 42 Case Logic

43 43 Debugging a Shell Script Shell script will not execute if there is an error in one or more commands sh has options for debugging –sh -v displays lines of script as they are read by the interpreter –sh -x displays the command and its arguments line by line as they are run

44 44 Debugging a Shell Script View the script line by line as it is running to help locate errors

45 45 Lesson B Creating and Completing the Corporate Phone Application

46 46 Objectives Create screen-management scripts 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

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

48 48 tput command tput clear –clear the screen tput cup r c –position cursor to row and column –ex: tput cup 0 0 tput cup 20 10 tput cup 20 10 bold=`tput smso` offbold=`tput rmso`

49 49 Example tput clear; tput cup 10 15; echo “Hello”; tput cup 20 0

50 50 Creating a Menu tput can be used to help create data entry screens

51 51 Creating a Menu

52 52 Creating a Menu tput can be used to help create user menus

53 53 Creating a Menu

54 54 The trap Command used to guard against abnormal termination of script –user ^C –OS intervention normal: remove temporary file example: trap ’rm ~/tmp/*’ 2 15

55 55 Creating the corp_phones File The grep command is useful when building script applications by extracting data from files

56 56 Creating the corp_phones File Using awk speeds development in that it can select fields from many records and display them in a specified format on the screen

57 57 Creating the phoneadd Shell Script The phoneadd script allows you to add new records to the corp_phones file

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

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

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

61 61


Download ppt "Chapter Six Introduction to Shell Script Programming."

Similar presentations


Ads by Google