IT 244: Introduction to Linux/Unix
Class 28 - Final Review
Review
Microphone
Final Exam
The final exam will be held on Tuesday, May 14th
from 3:00 - 6:00 PM.
The exam will be given in this room.
If for some reason you are not able to take the Final at the time it will
be offered, you MUST send an email to me before the exam
so we can make alternative arrangements.
60% of the points on this exam will consist of questions from the Ungraded
Class Quizzes.
You do not need to study a Class Quiz question if the topic is not
mentioned in either the Midterm or Final review.
The other questions I will make up specifically for this exam.
For these questions you will have to know
- Absolute and relative pathnames
- The PATH system variable
- Access permissions
- Redirection
- Metacharacters
- Utilities
Today's will be a review session.
You will only be responsible for the material in today's class
and the review for the Midterm, which you will find
here.
Although the time alloted for the exam is 3 hours, I would
expect that most of you would not need that much time.
The final is a closed book exam.
To prevent cheating, certain
rules
will be enforced during the exam.
Remember, the Midterm and Final determine 50% of your grade.
Course Evaluation
At the end of each semester we offer you the opportunity
to say what you think about this course.
What have I done right?
What have I done wrong?
What can I do better?
You are not asked for you name.
So the submissions are anonymous.
I will not see your responses until after I have submitted grades
for this course.
We collect this feedback through Course Evaluations.
I will use what you say to make this course better.
To complete the course evaluation, use the following
link
.
You have until Friday, May 24th, to submit the evaluation.
No Class Next Week
A normal semester has 28 classes.
But this semester has 29.
I do not have materials for a 29th class.
Next Tuesday I will have Zoom Office Hours, from 9 AM to 5 PM,instead of
a class.
The Zoom session will be conducted from my home in Somerville.
Not my office on the UMB campus.
Questions
Are there any questions before I begin?
Attendance
Review
Running a Command in the Background
- Normally, when you run a command you have to wait for it to finish
- Such commands are said to be running in the
foreground
- Unix gives you a way to get the command prompt back right away
- You can run the command in the
background
- The background job loses it's connection to the keyboard ...
- and the shell will give you a prompt immedidately
- The shell will tell you when the background job has finished
- Every time a program runs, a
process
is created
- The process has access to system resources
- Such as memory (RAM) and a connection to the filesystem
- Unix, like most OSs, is a multitasking operating system
- This means you can have more than one process running at a time
- To run a command in the background ...
- type an ampersand, & ,
at the end of the command line
$ sleep 5 &
[1] 17895
$
- The number in square brackets is the job number
- The second number is the process ID
Jobs
Moving a Job from the Foreground into the Background
- There can only be one foreground job
- But there can be many background jobs
- Unix will let you move a job from the foreground to the background
- To do this, you must first suspend the foreground job
- A suspended job is not dead
- It is in a state of suspended animation
- You can reactivate it later
- To suspend a foreground job you must use the suspend key sequence
- On our systems you suspend a job by hitting Control Z
- After you do this, the shell stops the current process
- It also disconnects it from the keyboard
- To make a suspended job run in the background use
bg
bg
stands for background
- Once placed in the background, the job resumes running
- If more than one job is suspended you must give
bg
the
job number
Aborting a Background Job
- There are two ways to abort a background job
- You can bring a background job to the foreground ...
- using the
fg
(foreground) command
- Once you have the job in the foreground
- You can abort it using Control C
- If there is only one background job
- Run
fg
without an argument
- If there is more than one background job ...
fg
needs the job number as an argument
- You can also terminate any job using the
kill
command
- But to use
kill
you must tell it what to kill
- The usual way to do this is to give
kill
a process ID
- You can also use the job number with
kill
- But you must precede a job number with a percent sign, %
- You can get the job number by using the
jobs
command
Pathname Expansion
- Pathname expansion
expands a few letters into an entire pathname
- It also allows you to specify more than one file or directory
- Pathname expansion uses characters with special meaning to the shell
- These special characters are called
meta-characters
- Meta-characters are also sometimes called
wildcards
- They allow you to specify a pattern
- The pattern is called an
ambiguous file reference
- When you hit Enter the shell replaces the pattern with pathnames
that match
- The shell then runs this altered command line
- You can use as many meta-characters as you want to form a pattern
- Pathname expansion is different from
pathname completion
- Which you get by hitting Tab
- The square brackets, [ and
], are also meta-characters
- They work somewhat like ?
- They only match a single character in a pathname
- But the character must match one of the characters within the
brackets
- You can use the bracket meta-characters with any program
- You can use a range to avoid listing all characters
- A range consist of first and last characters separated by a dash,
-
- The sequence is specified in alphabetical order
- The square brackets provide another shortcut
- An initial exclamation mark, ! or a caret,
^ ...
- changes the meaning of the square bracket
- Now it matches any character that is not included within the
brackets
Built-ins
- Not all commands can be found on disk as executable files
- Some are actually written as part of the shell
- Such commands are called
built-ins
- When you run a built-in the shell does not have to create a new process
- Instead the command runs in the same process as the shell
- This makes execution faster
Creating Startup Files
- A startup file contains Unix commands that are run just before you
get a prompt
- The startup file normally used by Bash is
.bash_profile
- This file must be placed in your home directory
- The commands in this file are run after you enter your password ...
- but before you get a prompt
Redirecting Standard Error
Shell Scripts
- A shell script can use any shell feature ...
- that is available at the command line
- Ambiguous file references using the metacharacters
?, *
and [ ]
- Redirection
- Pipes
- But not those features which are provided by the Readline Library
-
Command line editing
(arrow keys, control key combinations)
-
Pathname completion (hit Tab to get more of a filename)
-
The history mechanism (up arrow to recall previous command line)
- Unix also provides control structures
Making a Shell Script Executable
- You must have both
read
and
execute
permission ...
- to run a shell script ...
- without calling Bash
- Normally you would give a shell script file 755 permissions
- The owner can read, write and execute
- The group and everyone else can read and execute
Specifying Which Version of the Shell Will Run a Script
- When the shell runs a shell script it creates a new subshell
- This subshell runs inside the script process
- Normally this
sub-shell
will be the same shell version ...
- as your current shell
- A script can use the
hashbang
line ...
- to specify which shell version to use to run the script
- The hashbang line must be the first line of the script
- The first two characters on the line ...
- must be a hash symbol, # ...
- followed by an exclamation mark, !
- After these two characters, you need to have the
absolute pathname
...
- of the version of the shell which will run the script
- Scripts have to be read by the people
- Who write the program
- Who maintain the program
- Who use the program
- To make it clear what is happening inside a program
- You need to add comments to the code
- Anything following a hash mark, # , is a
comment
- Except for the hashbang line
Separating and Grouping Commands
Continuing a Command onto the Next Line
Shell Variables
- A
variable
is a place in memory with a name ...
- that holds a value
- To get the value of a variable
- Put a dollar sign,
$, in front of its name
- Some variables are set and maintained by the shell itself
- They are called
keyword shell variables
- Or just keyword variables
- Other variables are created by the user
- They are called are called user-created variables
- The environment in which a variable can be used is called the
scope
- Shell variables have two scopes
Local Variables
- Local variables
only exist in the shell in which they are defined
- To create a local variable, use the following format
VARIABLE_NAME=VALUE
- There should be no spaces on either side of the equal sign
- Variables are local unless you explicitly make them global
- If the value assigned to a variable has spaces or tabs ...
- you must quote it ...
- or put a backslash in front of each whitespace character
- If you want to use the value of a variable inside quotes
- You must use double quotes
- When you run a shell script, the local variables will not be visible ...
- because the script is running in a sub-shell ...
- and the local variables are not defined there
Global Variables
Keyword Shell Variables
- Keyword shell variables have special meaning to the shell
- By convention, the names of keyword variables are always capitalized
- Most keyword variables can be changed by the user
- This is normally done in the startup file .bash_profile
User-created Variables
- User-created variables are any variables you create
- By convention, the names of user-created variables are lower case
- User-created variables can be either local or global in scope
Positional and Special Parameters
- Positional and special parameters are variables set by Unix
-
Positional parameters
allow a shell script ...
- to get arguments from the command line
- Whenever you run a script each token on the command line ...
- is assigned to a positional parameter
- The first token is assigned to positional parameter
0
- This parameter contains the
pathname
of the script
- Each succeeding token on the command line ...
- is assigned to the next positional parameter
$ cat print_positionals.sh
#!/bin/bash
#
# Prints the value of the first four positional arguments
echo
echo 0: $0
echo 1: $1
echo 2: $2
echo 3: $3
$ ./print_positionals.sh foo bar bletch
0: ./print_positionals.sh
1: foo
2: bar
3: bletch
- Positional parameters are the usual way input is given to a script
- The special parameter is # contains the
number of arguments
$ cat print_arg_numbers.sh
#!/bin/bash
#
# Prints the number of arguments sent to this script
echo
echo This script received $# arguments
$ ./print_arg_numbers.sh foo bar bletch
This script received 3 arguments
Quoting and the Evaluation of Variables
- Whenever the value of a variable contains spaces or tabs
- You must quote the string or escape the whitespace character
- There are three ways this
- Single quotes (' ')
- Double quotes (" ")
- Backslash (\)
- Everything surrounded by single quotes ...
- appears in the value of the variable exactly as you entered it
- But inside double quotes you can get the value of a variable ...
- by typing $ in front of a variable name
- The backslash, \ , only effects the character
immediately following it
Processes
- A process is a running program
- Unix is a multitasking operating system
- So many processes can run at the same time
- The shell runs in a process like any other program
- Every time you run a program a process is created
- Except when you run a built-in
Process Structure
- Processes are created in a hierarchical fashion
- When the machine is started, there is only one process
- This process is called either init ...
- or systemd
- init or systemd
then creates other processes ...
- which provide the services running on the machine
- These child processes can create other processes
- init or systemd
have a PID (Process ID) of 1
- Each is the ancestor of every other processes that ever runs on the
machine
Process Identification
Executing a Command
- When you type something on the command line
- The shell asks the operating system to create a process for the program
- Then it sleeps ...
- waiting for the child process to finish
- When it finishes it sends back an
exit status
- Then the shell wakes up and gives you a prompt
The Readline Library
- The readline library is a collection of procedures ...
- which let you edit the command line
- When you use Control key combinations on the command line ...
- you are using the readline library
- Any program written in C can use the readline library
- Here are some of the more useful commands
Command | Meaning |
Control A |
Move to the beginning of the line |
Control E |
Move to the end of the line |
Control U |
Remove everything from the text entry point to the
beginning of the line
|
Control K |
Remove everything from the text entry point to the end
of the line
|
→ |
Move the text entry point one character to the left |
← |
Move the text entry point one character to the right |
↑ |
Recall the previous command line entry in the history list |
↓
| Recall the following command line entry in the
history list
|
Readline Completion
- The readline library provides a completion mechanism
- Type a few letters of something ...
- and readline completion will try to supply the rest
- There are three forms of completion provided by the readline library
Aliases
Functions
Shell Modification of the Command Line
- But before the shell executes your command line
- If first looks to see if it needs to make changes
- The shell can rewrite the command line before executing it
- It does this to implement features of the shell
- Like
command substitution
and pathname expansion
- There are 10 different ways in which the shell can modify the command line
- You are only responsible for the following
Alias Substitution
- When Bash performs alias substitution ...
- it replaces the name of the alias ...
- with the value of the alias
Brace Expansion
- Braces, { }, allow you to create several
strings all at once
- Braces can appear with strings of characters in front or behind
- The braces contain strings of characters separated by commas
- The shell expands a brace by creating a string ...
- for each string contained within the braces
- If I wanted to create 5 foo files I could use braces expansion as
follows
$ touch foo{1,2,3,4,5}.txt
$ ls
foo1.txt foo2.txt foo3.txt foo4.txt foo5.txt
Tilde, ~, Expansion
- Bash replaces ~ with the absolute address
of your home directory
- If the ~ is followed by a user name
- Bash substitutes the absolute address of the home director ...
- for that user
Parameter and Variable Expansion
- After tilde expansion, Bash performs parameter and
variable expansion
$ echo $SHELL
/bin/bash
- When you type a $ followed by the name of
a variable ...
- Bash replaces this with the value of the variable
Command Substitution
- In command substitution, a command is run in a sub-shell
- The output of that command replaces the command substitution text
- Command substitution uses the following format
$(COMMAND)
- Where COMMAND is any valid Unix command
Pathname Expansion
- Pathname expansion is where a pattern using metacharacters
- If replaced with one or more pathnames
- The meta-characters are
Shell Script Control Structures
- Control structures are Unix statements that change the order of
execution ...
- of commands within a program or script
- There are two basic types of control structures
The if ... then ...
Construct
test
- The
test
command evaluates a logical expression ...
- and returns a status code of 0 if the expression is true
The test
Operators
test
has a number of operators
- The operators test for different conditions
- Some operators work only on numbers
Operator | Condition Tested |
-eq
| Two numbers are equal |
-gt |
The first number is greater than the second |
-lt |
The first number is less than the second |
test
uses the different operators when comparing
strings
Operator |
Condition Tested |
= |
When placed between strings, are the two strings the same |
!= |
When placed between strings, are the two strings not the same |
- Note that
test
uses symbols (=)
when comparing strings
- But letters preceded by a dash (-eq) when
comparing numbers
Using test
in Scripts
The if ... then ... else ...
Construct
The if ... then ... elif ...
Construct
for ... in ...
Loops
for
Loops
while
Loops
continue
- Normally, a loop will execute all the commands
- Between
do
and done
- If you want to skip some part of the code block ...
- call
continue
...
- and the following statements will be skipped
- The script then returns to the top of the loop ...
- and begins the next iteration
continue
does not cause the script to break out of the
loop
- It merely stops the execution of the loop code ...
- for one iteration
break
- If you want to jump completely out of the loop
- Call
break
read
Command