chapter six - home | school of computing and information ...ege/cop3344/slides/ch_06.pdf · files...
TRANSCRIPT
![Page 1: Chapter Six - Home | School of Computing and Information ...ege/cop3344/slides/ch_06.pdf · files into executable machine-language files The complier reads the ... that the file is](https://reader031.vdocuments.us/reader031/viewer/2022030413/5a9ea8be7f8b9a67178bb081/html5/thumbnails/1.jpg)
1
Chapter Six
Introduction to Shell Script Programming
2
Lesson A
Using the UNIX Shell as aScripting Language
3
ObjectivesUnderstand 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
![Page 2: Chapter Six - Home | School of Computing and Information ...ege/cop3344/slides/ch_06.pdf · files into executable machine-language files The complier reads the ... that the file is](https://reader031.vdocuments.us/reader031/viewer/2022030413/5a9ea8be7f8b9a67178bb081/html5/thumbnails/2.jpg)
2
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
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
![Page 3: Chapter Six - Home | School of Computing and Information ...ege/cop3344/slides/ch_06.pdf · files into executable machine-language files The complier reads the ... that the file is](https://reader031.vdocuments.us/reader031/viewer/2022030413/5a9ea8be7f8b9a67178bb081/html5/thumbnails/3.jpg)
3
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
Using UNIX Shell ScriptsUnlike 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
Using UNIX Shell ScriptsAfter 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
![Page 4: Chapter Six - Home | School of Computing and Information ...ege/cop3344/slides/ch_06.pdf · files into executable machine-language files The complier reads the ... that the file is](https://reader031.vdocuments.us/reader031/viewer/2022030413/5a9ea8be7f8b9a67178bb081/html5/thumbnails/4.jpg)
4
10
The Programming Shell
All Linux versions use the Bash shell as the default
11
VariablesVariables 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
Variables
Use the printenv command to see a list of environment variables
![Page 5: Chapter Six - Home | School of Computing and Information ...ege/cop3344/slides/ch_06.pdf · files into executable machine-language files The complier reads the ... that the file is](https://reader031.vdocuments.us/reader031/viewer/2022030413/5a9ea8be7f8b9a67178bb081/html5/thumbnails/5.jpg)
5
13
14
15
![Page 6: Chapter Six - Home | School of Computing and Information ...ege/cop3344/slides/ch_06.pdf · files into executable machine-language files The complier reads the ... that the file is](https://reader031.vdocuments.us/reader031/viewer/2022030413/5a9ea8be7f8b9a67178bb081/html5/thumbnails/6.jpg)
6
16
VariablesTo set:
example=oneTo see:
echo $exampleTo make part of the environment:
export exampleTo remove:
unsetenv example
17
Shell OperatorsBash shell operators are in three groups:– Defining and Evaluating operators are used to set a
variable to a value and to check variable valuesThe 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
Shell Operators
![Page 7: Chapter Six - Home | School of Computing and Information ...ege/cop3344/slides/ch_06.pdf · files into executable machine-language files The complier reads the ... that the file is](https://reader031.vdocuments.us/reader031/viewer/2022030413/5a9ea8be7f8b9a67178bb081/html5/thumbnails/7.jpg)
7
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
Shell Logic Structures
Four basic logic structures needed for program development are:– Sequential logic– User input– Decision logic– Looping logic– Case logic
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
![Page 8: Chapter Six - Home | School of Computing and Information ...ege/cop3344/slides/ch_06.pdf · files into executable machine-language files The complier reads the ... that the file is](https://reader031.vdocuments.us/reader031/viewer/2022030413/5a9ea8be7f8b9a67178bb081/html5/thumbnails/8.jpg)
8
22
User input
Script can read user data
Command: read variable
reads user input and assigns text to variable
23
User inputCommand: read var1 var2 var3
reads 3 words and assigns 3 variablesLast variable contains rest of input line
24
User input
Command: read –p “enter name: “ name
Prompts user, then reads input and assigns to variable
![Page 9: Chapter Six - Home | School of Computing and Information ...ege/cop3344/slides/ch_06.pdf · files into executable machine-language files The complier reads the ... that the file is](https://reader031.vdocuments.us/reader031/viewer/2022030413/5a9ea8be7f8b9a67178bb081/html5/thumbnails/9.jpg)
9
25
User input example
26
Decision LogicEnables 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
If statementSyntax:
if [ condition ]then
statementselse
statementsfi
![Page 10: Chapter Six - Home | School of Computing and Information ...ege/cop3344/slides/ch_06.pdf · files into executable machine-language files The complier reads the ... that the file is](https://reader031.vdocuments.us/reader031/viewer/2022030413/5a9ea8be7f8b9a67178bb081/html5/thumbnails/10.jpg)
10
28
Decision Logic example
29
Nested Decision Logic
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
![Page 11: Chapter Six - Home | School of Computing and Information ...ege/cop3344/slides/ch_06.pdf · files into executable machine-language files The complier reads the ... that the file is](https://reader031.vdocuments.us/reader031/viewer/2022030413/5a9ea8be7f8b9a67178bb081/html5/thumbnails/11.jpg)
11
31
For Loop
Syntax
for var in listdo
statementsdone
32
For Loop exampleProgram control structures can be entered from the command line
33
For loop in script
![Page 12: Chapter Six - Home | School of Computing and Information ...ege/cop3344/slides/ch_06.pdf · files into executable machine-language files The complier reads the ... that the file is](https://reader031.vdocuments.us/reader031/viewer/2022030413/5a9ea8be7f8b9a67178bb081/html5/thumbnails/12.jpg)
12
34
Loop with wildcard
35
While Loop
Syntax
while [ condition ]do
statementsdone
36
Looping LogicThe while loop tests repeatedly for a matching condition
![Page 13: Chapter Six - Home | School of Computing and Information ...ege/cop3344/slides/ch_06.pdf · files into executable machine-language files The complier reads the ... that the file is](https://reader031.vdocuments.us/reader031/viewer/2022030413/5a9ea8be7f8b9a67178bb081/html5/thumbnails/13.jpg)
13
37
Looping Logic
While loops can serve as data-entry forms
38
While loop to enter data
39
Case LogicThe 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
![Page 14: Chapter Six - Home | School of Computing and Information ...ege/cop3344/slides/ch_06.pdf · files into executable machine-language files The complier reads the ... that the file is](https://reader031.vdocuments.us/reader031/viewer/2022030413/5a9ea8be7f8b9a67178bb081/html5/thumbnails/14.jpg)
14
40
Case statementSyntax:
case $variable in“pattern1”)
statements;;
“pattern2”)statements;;
esac
41
Case example
42
Case Logic
![Page 15: Chapter Six - Home | School of Computing and Information ...ege/cop3344/slides/ch_06.pdf · files into executable machine-language files The complier reads the ... that the file is](https://reader031.vdocuments.us/reader031/viewer/2022030413/5a9ea8be7f8b9a67178bb081/html5/thumbnails/15.jpg)
15
43
Debugging a Shell ScriptShell 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
Debugging a Shell Script
View the script line by line as it is running to help locate errors
45
Lesson B
Creating and Completing theCorporate Phone Application
![Page 16: Chapter Six - Home | School of Computing and Information ...ege/cop3344/slides/ch_06.pdf · files into executable machine-language files The complier reads the ... that the file is](https://reader031.vdocuments.us/reader031/viewer/2022030413/5a9ea8be7f8b9a67178bb081/html5/thumbnails/16.jpg)
16
46
ObjectivesCreate 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
Using Shell Scripting toCreate 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
tput commandtput clear– clear the screen
tput cup r c– position cursor to row and column– ex: tput cup 0 0
tput cup 20 10bold=`tput smso`offbold=`tput rmso`
![Page 17: Chapter Six - Home | School of Computing and Information ...ege/cop3344/slides/ch_06.pdf · files into executable machine-language files The complier reads the ... that the file is](https://reader031.vdocuments.us/reader031/viewer/2022030413/5a9ea8be7f8b9a67178bb081/html5/thumbnails/17.jpg)
17
49
Example
tput clear; tput cup 10 15;echo “Hello”; tput cup 20 0
50
Creating a Menu
tput can be used to help create data entry screens
51
Creating a Menu
![Page 18: Chapter Six - Home | School of Computing and Information ...ege/cop3344/slides/ch_06.pdf · files into executable machine-language files The complier reads the ... that the file is](https://reader031.vdocuments.us/reader031/viewer/2022030413/5a9ea8be7f8b9a67178bb081/html5/thumbnails/18.jpg)
18
52
Creating a Menu
tput can be used to help create user menus
53
Creating a Menu
54
The trap Command
used to guard against abnormal termination of script– user ^C– OS intervention
normal: remove temporary fileexample:trap ’rm ~/tmp/*’ 2 15
![Page 19: Chapter Six - Home | School of Computing and Information ...ege/cop3344/slides/ch_06.pdf · files into executable machine-language files The complier reads the ... that the file is](https://reader031.vdocuments.us/reader031/viewer/2022030413/5a9ea8be7f8b9a67178bb081/html5/thumbnails/19.jpg)
19
55
Creating the corp_phones File
The grep command is useful when building script applications by extracting data from files
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
Creating the phoneadd Shell Script
The phoneadd script allows you to add new records to the corp_phones file
![Page 20: Chapter Six - Home | School of Computing and Information ...ege/cop3344/slides/ch_06.pdf · files into executable machine-language files The complier reads the ... that the file is](https://reader031.vdocuments.us/reader031/viewer/2022030413/5a9ea8be7f8b9a67178bb081/html5/thumbnails/20.jpg)
20
58
Chapter Summary
A high-level language uses English-like expressions and must be converted into a low-level language before being executedThe shell interprets shell scriptsUNIX 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 defaultUNIX employs three types of variables: configuration, environment, and shell
59
Chapter SummaryThe shell supports numerous operators, including many for performing arithmetic operationsWildcard characters are used in shell scriptsThe logic structures supported by the shell are sequential, decision, looping and caseThe tput command can be used to manage cursor placement on the screenProgrammers and system administrators often customize the .bashrc file to suit their needs
60
Chapter SummaryAliases, used to simplify commonly used commands, can be entered into the .bashrc Use the trap command to remove temporary files after the script exitsThe grep command serves a key role in the development of shell scripts by allowing searching and retrieving data from filesThe awk command serves as an effective and easy-to-use tool for generating reports
![Page 21: Chapter Six - Home | School of Computing and Information ...ege/cop3344/slides/ch_06.pdf · files into executable machine-language files The complier reads the ... that the file is](https://reader031.vdocuments.us/reader031/viewer/2022030413/5a9ea8be7f8b9a67178bb081/html5/thumbnails/21.jpg)
21
61