introduction to shell script programming
DESCRIPTION
Introduction to Shell Script Programming. Objectives. Understand the program development cycle Compare UNIX/Linux shells for creating scripts Use shell variables, operators, and wildcard characters. Objectives (continued). Use shell logic structures Employ shell scripting to create a menu - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Introduction to Shell Script Programming](https://reader035.vdocuments.us/reader035/viewer/2022081504/56813407550346895d9afa4a/html5/thumbnails/1.jpg)
Introduction to Shell Script Programming
![Page 2: Introduction to Shell Script Programming](https://reader035.vdocuments.us/reader035/viewer/2022081504/56813407550346895d9afa4a/html5/thumbnails/2.jpg)
2
Objectives
• Understand the program development cycle
• Compare UNIX/Linux shells for creating scripts
• Use shell variables, operators, and wildcard characters
![Page 3: Introduction to Shell Script Programming](https://reader035.vdocuments.us/reader035/viewer/2022081504/56813407550346895d9afa4a/html5/thumbnails/3.jpg)
3
Objectives (continued)
• Use shell logic structures
• Employ shell scripting to create a menu
• Use commands to help debug shell scripts
![Page 4: Introduction to Shell Script Programming](https://reader035.vdocuments.us/reader035/viewer/2022081504/56813407550346895d9afa4a/html5/thumbnails/4.jpg)
4
Objectives (continued)
• Explain ways to customize your personal environment
• Use the trap command
![Page 5: Introduction to Shell Script Programming](https://reader035.vdocuments.us/reader035/viewer/2022081504/56813407550346895d9afa4a/html5/thumbnails/5.jpg)
5
Previewing the Application
• Shell scripts can be used to customize your environment
• Will develop a simulated employee information system using shell scripts
• Will gain experience with shell variables, shell script operators, and logic structures
![Page 6: Introduction to Shell Script Programming](https://reader035.vdocuments.us/reader035/viewer/2022081504/56813407550346895d9afa4a/html5/thumbnails/6.jpg)
6
The Program Development Cycle
• The program development cycle is the process of developing an application
– First step is to create program specifications
– Second step is to create the program design
– Third step is developing the code, which is written, tested, and debugged
![Page 7: Introduction to Shell Script Programming](https://reader035.vdocuments.us/reader035/viewer/2022081504/56813407550346895d9afa4a/html5/thumbnails/7.jpg)
7
The Program Development Cycle
![Page 8: Introduction to Shell Script Programming](https://reader035.vdocuments.us/reader035/viewer/2022081504/56813407550346895d9afa4a/html5/thumbnails/8.jpg)
8
Using High-Level Languages
• High-level languages are computer languages that use English-like expressions
• Examples are; COBOL, C, C++• High-level language statements are stored in a source
file, which programmers create using an editor
![Page 9: Introduction to Shell Script Programming](https://reader035.vdocuments.us/reader035/viewer/2022081504/56813407550346895d9afa4a/html5/thumbnails/9.jpg)
9
Using High-Level Languages (continued)
• High-level source files must be converted into a low-level machine language file
• A compiler is a program that converts source files into executable machine-language files
• 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
![Page 10: Introduction to Shell Script Programming](https://reader035.vdocuments.us/reader035/viewer/2022081504/56813407550346895d9afa4a/html5/thumbnails/10.jpg)
10
Using UNIX/Linux Shell Scripts
• Unlike high-level language programs, shell scripts do not have to be converted into machine language
• The UNIX/Linux shell acts as an interpreter when reading script files
• Interpreters read statements in script files and immediately translate them into executable instructions and run them
![Page 11: Introduction to Shell Script Programming](https://reader035.vdocuments.us/reader035/viewer/2022081504/56813407550346895d9afa4a/html5/thumbnails/11.jpg)
11
Using UNIX/Linux Shell Scripts(continued)
• After creating shell script, the OS is instructed that the file is an executable shell script via the chmod 755 command.
• Script files can be run in several ways:– Set the path variable and type the script name at the
command prompt– Type ./filename if script is in current directory– Type the script name preceded by the full path
![Page 12: Introduction to Shell Script Programming](https://reader035.vdocuments.us/reader035/viewer/2022081504/56813407550346895d9afa4a/html5/thumbnails/12.jpg)
12
Prototyping an Application
• A prototype is a running model of your application– Less detail, less design time than a full application– Shows potential without full programming effort
• Shell scripts can be used to prototype applications that will later be moved to compiled languages
![Page 13: Introduction to Shell Script Programming](https://reader035.vdocuments.us/reader035/viewer/2022081504/56813407550346895d9afa4a/html5/thumbnails/13.jpg)
13
Using Comments
• Comments are important!• Provide useful documentation to both the programmer
and to others who need to understand or debug the code• To use, start comment line with a #
![Page 14: Introduction to Shell Script Programming](https://reader035.vdocuments.us/reader035/viewer/2022081504/56813407550346895d9afa4a/html5/thumbnails/14.jpg)
14
The Programming Shell
All Linux versions use the Bash shell as the default
![Page 15: Introduction to Shell Script Programming](https://reader035.vdocuments.us/reader035/viewer/2022081504/56813407550346895d9afa4a/html5/thumbnails/15.jpg)
15
Variables
• Variables are symbolic names that represent values stored in memory
• Three types of variables:– 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
![Page 16: Introduction to Shell Script Programming](https://reader035.vdocuments.us/reader035/viewer/2022081504/56813407550346895d9afa4a/html5/thumbnails/16.jpg)
16
Environment and Configuration Variables
• Environment and configuration variables can be used to set up and personalize your sessions
• The printenv command shows current environment and configuration variables
![Page 17: Introduction to Shell Script Programming](https://reader035.vdocuments.us/reader035/viewer/2022081504/56813407550346895d9afa4a/html5/thumbnails/17.jpg)
17
Environment and Configuration Variables (continued)
Use the printenv variable to see a list of environment variables
![Page 18: Introduction to Shell Script Programming](https://reader035.vdocuments.us/reader035/viewer/2022081504/56813407550346895d9afa4a/html5/thumbnails/18.jpg)
18
Environment and Configuration Variables (continued)
![Page 19: Introduction to Shell Script Programming](https://reader035.vdocuments.us/reader035/viewer/2022081504/56813407550346895d9afa4a/html5/thumbnails/19.jpg)
19
Environment and Configuration Variables (continued)
![Page 20: Introduction to Shell Script Programming](https://reader035.vdocuments.us/reader035/viewer/2022081504/56813407550346895d9afa4a/html5/thumbnails/20.jpg)
20
Environment and Configuration Variables (continued)
![Page 21: Introduction to Shell Script Programming](https://reader035.vdocuments.us/reader035/viewer/2022081504/56813407550346895d9afa4a/html5/thumbnails/21.jpg)
21
Environment and Configuration Variables (continued)
![Page 22: Introduction to Shell Script Programming](https://reader035.vdocuments.us/reader035/viewer/2022081504/56813407550346895d9afa4a/html5/thumbnails/22.jpg)
22
Shell Variables
• Variables that you can define and manipulate for use with program commands in a shell
• Observe basic guidelines for handling and naming shell variables
![Page 23: Introduction to Shell Script Programming](https://reader035.vdocuments.us/reader035/viewer/2022081504/56813407550346895d9afa4a/html5/thumbnails/23.jpg)
23
Shell Operators
• Bash shell operators are in four groups:– Defining – Evaluating– Arithmetic – Redirection
![Page 24: Introduction to Shell Script Programming](https://reader035.vdocuments.us/reader035/viewer/2022081504/56813407550346895d9afa4a/html5/thumbnails/24.jpg)
24
Defining Operators
• Used to assign a value to a variable• Most common is = (equal sign)• Use quotation marks with strings• Backquote says execute the command inside the
backquotes and store the result in the variable
![Page 25: Introduction to Shell Script Programming](https://reader035.vdocuments.us/reader035/viewer/2022081504/56813407550346895d9afa4a/html5/thumbnails/25.jpg)
25
Evaluating Operators
• Used for determining the contents of a variable• echo $variablename will show the value of variablename• Double quotes can be used, but not single quotes
![Page 26: Introduction to Shell Script Programming](https://reader035.vdocuments.us/reader035/viewer/2022081504/56813407550346895d9afa4a/html5/thumbnails/26.jpg)
26
Arithmetic Operators
![Page 27: Introduction to Shell Script Programming](https://reader035.vdocuments.us/reader035/viewer/2022081504/56813407550346895d9afa4a/html5/thumbnails/27.jpg)
27
Arithmetic Operators (continued)
• Regular mathematical precedence rules apply to arithmetic operators
To store arithmetic values in a variable, use let statement
![Page 28: Introduction to Shell Script Programming](https://reader035.vdocuments.us/reader035/viewer/2022081504/56813407550346895d9afa4a/html5/thumbnails/28.jpg)
28
Redirection Operators
• The > redirection operator overwrites an existing file• -o noclobber option of set command will prevent
overwriting
![Page 29: Introduction to Shell Script Programming](https://reader035.vdocuments.us/reader035/viewer/2022081504/56813407550346895d9afa4a/html5/thumbnails/29.jpg)
29
Exporting Shell Variables to the Environment
• Shell scripts cannot automatically access variables created and assigned– On the command line– By other scripts
• Make variables global in your environment by using the export command
![Page 30: Introduction to Shell Script Programming](https://reader035.vdocuments.us/reader035/viewer/2022081504/56813407550346895d9afa4a/html5/thumbnails/30.jpg)
30
Modifying the PATH Variable
• PATH variable controls where your shell will look for shell scripts
• You can add directories to your PATH– Special directories for scripts– Your current working directory
![Page 31: Introduction to Shell Script Programming](https://reader035.vdocuments.us/reader035/viewer/2022081504/56813407550346895d9afa4a/html5/thumbnails/31.jpg)
31
More About Wildcard Characters
• Shell scripts often use wildcard characters• Wildcard characters are called glob characters and
are a part of glob patterns• Glob patterns are intended to match filenames and
words– Question mark (?) matches one character– Asterisk (*) matches zero or more characters – [chars] defines a class of characters, the glob
pattern matches any character in the class
![Page 32: Introduction to Shell Script Programming](https://reader035.vdocuments.us/reader035/viewer/2022081504/56813407550346895d9afa4a/html5/thumbnails/32.jpg)
32
Shell Logic Structures
• Four basic logic structures needed for program development are:– Sequential logic– Decision logic– Looping logic– Case logic
![Page 33: Introduction to Shell Script Programming](https://reader035.vdocuments.us/reader035/viewer/2022081504/56813407550346895d9afa4a/html5/thumbnails/33.jpg)
33
Sequential Logic
• 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
![Page 34: Introduction to Shell Script Programming](https://reader035.vdocuments.us/reader035/viewer/2022081504/56813407550346895d9afa4a/html5/thumbnails/34.jpg)
34
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
![Page 35: Introduction to Shell Script Programming](https://reader035.vdocuments.us/reader035/viewer/2022081504/56813407550346895d9afa4a/html5/thumbnails/35.jpg)
35
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 36: Introduction to Shell Script Programming](https://reader035.vdocuments.us/reader035/viewer/2022081504/56813407550346895d9afa4a/html5/thumbnails/36.jpg)
36
Looping Logic (continued)
The for loop repeats for however many values there are in the specified set of values
![Page 37: Introduction to Shell Script Programming](https://reader035.vdocuments.us/reader035/viewer/2022081504/56813407550346895d9afa4a/html5/thumbnails/37.jpg)
37
Looping Logic (continued)
• Program control structures can be entered from the command line
• Wildcard characters can be used in loops• The while loop is set up to test repeatedly for a matching
condition• The while loop is used when code must be repeatedly
executed an undetermined number of times
![Page 38: Introduction to Shell Script Programming](https://reader035.vdocuments.us/reader035/viewer/2022081504/56813407550346895d9afa4a/html5/thumbnails/38.jpg)
38
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
![Page 39: Introduction to Shell Script Programming](https://reader035.vdocuments.us/reader035/viewer/2022081504/56813407550346895d9afa4a/html5/thumbnails/39.jpg)
39
Using Shell Scripting to Create a Menu
• Often useful to create a menu that branches to specific shell scripts
• The tput command is useful when creating menus
• Can initialize the terminal display to place text and prompts in specific locations and respond to the user
![Page 40: Introduction to Shell Script Programming](https://reader035.vdocuments.us/reader035/viewer/2022081504/56813407550346895d9afa4a/html5/thumbnails/40.jpg)
40
Debugging a Shell Script
• A shell script will not execute if there is an error in one or more commands
• Running a shell script using sh enables quick debugging of problems
– 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
![Page 41: Introduction to Shell Script Programming](https://reader035.vdocuments.us/reader035/viewer/2022081504/56813407550346895d9afa4a/html5/thumbnails/41.jpg)
41
Customizing YourPersonal 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
![Page 42: Introduction to Shell Script Programming](https://reader035.vdocuments.us/reader035/viewer/2022081504/56813407550346895d9afa4a/html5/thumbnails/42.jpg)
42
Customizing Your Personal Environment (continued)
• An alias is a name that represents another command
• The .bashrc file in your home directory is 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
![Page 43: Introduction to Shell Script Programming](https://reader035.vdocuments.us/reader035/viewer/2022081504/56813407550346895d9afa4a/html5/thumbnails/43.jpg)
43
The trap Command
• The trap command causes a shell program to automatically remove temporary files created when shell scripts run
• Programmers often set up a subdirectory to store temporary files, and when a script file exits, trap removes the files
• Having files removed from a temporary directory like this is considered “good housekeeping”
![Page 44: Introduction to Shell Script Programming](https://reader035.vdocuments.us/reader035/viewer/2022081504/56813407550346895d9afa4a/html5/thumbnails/44.jpg)
44
Putting It All Together in an Application
• Applications require you to:– Assign and manage variables– Use shell operators– Employ shell logic structures– Use additional wildcard characters– Use tput for managing screen initialization– Use trap to clean up temporary files
![Page 45: Introduction to Shell Script Programming](https://reader035.vdocuments.us/reader035/viewer/2022081504/56813407550346895d9afa4a/html5/thumbnails/45.jpg)
45
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
• Linux shells are derived from the UNIX Bourne, Korn and C shells, and bash is the default
![Page 46: Introduction to Shell Script Programming](https://reader035.vdocuments.us/reader035/viewer/2022081504/56813407550346895d9afa4a/html5/thumbnails/46.jpg)
46
• UNIX/Linux uses three types of variables: configuration, environment, and shell
• Shell operators include defining, evaluating, arithmetic, and redirection
• Wildcard characters are used in shell scripts
• The logic structures supported are: sequential, decision, looping and case
Summary (continued)
![Page 47: Introduction to Shell Script Programming](https://reader035.vdocuments.us/reader035/viewer/2022081504/56813407550346895d9afa4a/html5/thumbnails/47.jpg)
47
• The tput command manages cursor placement on the screen
• Programmers and system administrators often customize the .bashrc file
• Aliases simplify common commands can be entered into the .bashrc
• Use the trap command to remove temporary files after the script exits
Summary (continued)