Professional Documents
Culture Documents
Objectives
Understand how shell scripts operate Collect user input and write to the screen from within a script Control command execution using tests and loops Debug shell scripts Understand other types of programming available on Linux
Scripting Basics
Shell script
Executable file Contains lines of text as would be entered at command line Executed like any other program Used on every Linux system
Computer language
Programming language Set of words and syntax rules Can be arranged in predefined ways to cause computer to perform tasks
Source code
Human readable computer program written by programmer
Binary file
Executable file Source code converted to numeric code that computers CPU can process
The Complete Guide to Linux System Administration 6
Interpreted language
Source code converted into numeric codes at instant user runs program
Shell script
Interpreted program
Programs define list of statements that are executed once, many times, or not at all depending on what happens at some other point in program
Nesting
One statement block contains another statement block
File must have execute file permission set File must contain valid commands that interpreter can recognize
10
11
12
13
14
Initialize variable
Assign initial value
Positional variable
Takes value based on information user includes on command line Indicate using dollar sign and number
$0 first variable on command line
The Complete Guide to Linux System Administration 15
16
17
18
test command creates type of selection statement to determine if condition true or false
19
Using if Statements
if command followed by then command
Lists commands to be executed if test succeeds
fi command
Marks end of if statement
Test succeeds
All commands between then and if executed
Test fails
No commands executed
20
if-then-else statement
If test returns value of true
One set of commands executed
22
23
exit command
Stops execution of script immediately Returns exit code provided Avoid using exit within loop
24
elif keyword
Used in place of else when using if-then-else When first command in else block another if-thenelse statement
25
26
27
28
$@ variable
Contains all parameters included on command line when script executed Used as <list of items> in for loop
29
continue command
Skip immediately to next iteration
31
To define function:
Follow name with ( ) Statements enclosed in braces { } considered part of function
32
Logic error
Error in what commands are trying to accomplish
33
-v option
Shell displays each line of script as it reads it
34
35
36
37
38
39
40
41
Compiled Languages
Linux kernel and utilities written using compiled languages Most widely used:
C C++
42
43
45
46
Summary
Shell scripts
Execute complex set of commands by entering single script name Uses keywords from programming language
Environment and positional variables often referenced in scripts if-then-else statement tests condition and executes statements if condition is present
47
Summary (Continued)
Loops using for and while
Repeatedly execute statement block
Complex scripts make extensive use of functions Shell scripts can be debugged using shell options Many scripting languages used on Linux systems Linux includes support for many computer languages
48