IT 244: Introduction to Linux/Unix
Class Review Final
Today's Topics
Review
Final Exam
The final exam will be given on Wednesday, April 16th from 6:30pm - 9:00pm.
The exam will be given in McCormack 3-430.
There will be 50 questions, which will be similar to the questions on the mid-term and
the quizzes.
As with the Mid-term, 60% of the questions will come from the ungraded Class Quizzes.
The questions will cover all of the material for this course.
Today's class will be a review session.
You will only be responsible for the material contained in the Class Notes for
today's class, and the Class Notes for the review session for the Mid-term,
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.
You will not be able to leave the room until you turn in your exam paper
so you should visit the restroom before you take the test.
The final is a closed book exam.
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 ...
- after running a command that will take a long time to finish
- 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
- 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 ...
- like 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 ...
- enter an ampersand, & , at the end of the command line ...
- just before hitting Enter:
$ sleep 5 &
[1] 17895
$
Jobs
Moving a Job from the Foreground into the Background
- There can only be one foreground job ...
- though you can have 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
- Once the job is suspended ...
- you can place it in the background using the
bg
command
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 job from the background to the foreground ...
- using the
fg
(foreground) command
- Once you have the job in the foreground ...
- you can abort it using Control C
- When there is more than one job in the background ...
- you must specify the job number when using
fg
- 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
- If you don't remember the process ID ...
- run
ps
(process status) to get the process ID (PID)
- 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
allows you to specify a file or directory ...
- without typing the full name
- It also allows you to specify more than one file or directory ...
- with a single string of characters
- 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
- When the shell sees one of these characters on the command line ...
- it replaces the pattern with a sorted list ...
- of all pathnames that match the pattern
- The shell then runs this altered command line
- The pattern is called an
ambiguous file reference
- 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 pathname character must match one of the characters ...
- within the brackets
- No matter how many characters are within the bracket ...
- the pattern can match only a single character
- You can use the bracket meta-characters with any program
- You can use a range to avoid listing all characters
- A range is specified by listing the first and last characters of a sequence ...
- separated by a dash, -
- The sequence is specified in alphabetical order
- The square brackets provide another shortcut
- If you insert an exclamation mark, ! ,
or a caret, ^ ...
- immediately after the opening bracket ...
- the shell will match any single 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 ...
- when you run these programs
- 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
tty
- 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
- Because the shell has to read the contents of the script ...
- you need read permission
- You need execute permission so the script can actually be run ...
- 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 shell ...
- inside the process that will run the script
- Normally this sub-shell
will be the same version of the shell ...
- as your current shell
- A script can use the hashbang line ...
- to specify which shell version to use ...
- when running a 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 clear what is happening inside a program ...
- use comments
- Anything following a hash mark, # , is a comment ...
- except for the hashbang line
Separating and Grouping Commands
| (pipe) and & (ampersand) as Command Separators
- The pipe, | , and ampersand, & , characters are also command separators
- When we separate commands with the pipe character, | ...
- each command takes its input from the previous command
- We use an ampersand, & , after a command ...
- to run that command in the background
- When we do this, two things happen
- The command is disconnected from the keyboard
- The command will run at the same time
as the next command you enter
- But the ampersand is also a command separator
- So we can use it to run many commands at the same time
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 are no spaces on either side of the equal sign ...
- when setting Bash variables
- 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
- If 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
Important Keyword Shell Variables
- There are a number of keyword variables that affect your Unix session
- Some of the more important are
Variable | Value |
HOME |
The absolute pathname of your home directory |
PATH |
The list of directories the shell will search when looking for the executable
file associated with a command you entered at the command line |
SHELL |
The absolute pathname of your default shell |
PS1 |
Your command line prompt - what you see after entering each command |
PS2 |
The secondary prompt - what you see if you continue a command to a second line |
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
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 variable exactly as you entered it
- Double quotes also preserve spaces and tabs ...
- in the strings they contain
- But you can use a $ in front of a variable name ...
- to get the value of a variable ...
- inside double quotes
- Quotes affect everything they enclose
- The backslash, \ , only effects the character immediately following it
Removing a Variable's Value
- There are two ways of removing the value of a variable
- You can use the
unset
command
- Or you can set the value of the variable to the empty string
Processes
- A process is a running program
- Unix is a multitasking operating system
- 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
- When you run a shell script ...
- your current shell creates a sub-shell to run the script
- This sub-shell runs in a new process
- When each command in the script is run ...
- a process is created for that command
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 ...
- depandending on what technology is used to start other programs
- init or systemd then creates other processes
- These new processes are child process of init or systemd
- These child processes can create other processes
- init or systemd has PID (Process ID) of 1
- init or systemd is the ancestor of every other processes ...
- that ever runs on the machine
Process Identification
Executing a Command
- When you run a command from the shell ...
- the shell asks the operating system to create a process ...
- to run the command
- Then it sleeps ...
- waiting for the child process to finish
- When the child process finishes ...
- it notifies its parent process of its success or failure ...
- by returning an exit status ...
- and then it dies
History
- The history mechanism maintains a list of the command lines you type
- These command line entries are called events
- To view the history list, use the
history
command
- If you run
history
without an argument ...
- it will display all the events in this history list
- By default, this list contains 500 values
- To restrict how many lines are displayed ...
- run
history
followed by a number
- You cannot have a - in front of the number ...
- as there must be when using
head
or tail
Using the History Mechanism
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 running under Bash and written in C can use the readline library
- Here are some of the more useful commands for the emacs version of the readline library
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
Pathname Completion
- The readline library provides pathname completion
- You begin typing a pathname, then hit Tab
- If there is only one pathname that matches ...
- the readline library will provide the the rest of the pathname
- If there is more than one possible completion ...
- the readline library will beep
- You can then enter more characters ...ยด
- before hitting Tab again ...
- or you can hit Tab right after the first beep ...
- and the readline library will give you a list of possible completions
- If the second Tab still gives you a beep ...
- there are no possible completions
Command Completion
- The readline library will complete the name of a command for you
- Begin typing a command ...
- then hit Tab ...
- and the readline library will try to supply the rest of the command
- If there is more than one possibility ...
- you will hear a beep
- If you hit Tab a second time ...
- you will see a list of possible completions
Variable Completion
- When you type a dollar sign, $ , followed immediately by some text ...
- you are entering a variable name
- The readline library knows this
- and will attempt to complete the name of the variable
- If there is more than one possibility, you will hear a beep
- If you then hit Tab another time
- you will see a list of possible completions
- If no list appears after the second Tab ...
- there are no possible variable name completions
Aliases
Functions
Shell Modification of the Command Line
- But before the shell executes the commands ...
- if first looks to see if it needs to make changes ...
- to the tokens on the command line
- The shell actually rewrites the command line before executing it
- It does this to implement features of the shell ...
- like command substitution
and pathname expansion
- These are features that make the shell more powerful ...
- but they require the shell to change what you typed on the command line ...
- before executing it
- There are 10 different ways in which the shell can modify the command line
- You are only responsible for the following
History Expansion
Alias Substitution
- After history expansion,
bash
performs alias substitution
- The shell replaces the name of the alias ...
- with the value of the alias
- Aliases allow you to run complicated commands ...
- by typing only a few characters
Brace Expansion
- Braces, { }, allow you to specify 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 many strings ...
- one 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
- Whenever
bash
sees a tilde, ~ , by itself ...
- it substitutes the absolute pathname of your home directory
- Whenever
bash
sees a tilde, ~ , followed by a Unix user name,
- it substitutes the absolute pathname of the home directory ...
- of that account
Parameter and Variable Expansion
- After tilde expansion,
bash
performs parameter and variable expansion
$ echo $SHELL
/bin/bash
- bash notices the $ in front of a string ...
- and looks to see if that string is the name of a variable
- If the string is a variable, bash subsitutes the value of the variable ...
- for the dollar sign and variable name
Command Substitution
- In command substitution, a command is run in a sub-shell ...
- and the output of that command ...
- replaces the command itself
- Command substitution uses the following format
$(COMMAND)
- Where COMMAND is any valid Unix command
Pathname Expansion
- Pathname expansion is where you use meta-characters ...
- to specify one or more pathnames
- The metacharacters are used to create patterns ...
- that are called ambiguous file references
- 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 ...
- given to it as an argument ...
- and returns a status code of 0 ...
- if the expression evaluates to true
- It returns a status code of 1 ...
- if the expression evaluates to false
- In an
if
statement, a status code of 0 means true ...
- and a status code greater than 0 means means false
The test
Operators
test
has a number of operators
- The operators test for different conditions
- When used with two arguments, the operators are placed between
- Some operators work only on numbers
Operator | Condition Tested |
-eq | Two numbers are equal |
-ne | Two numbers are not equal |
-ge | The first number is greater than, or equal to, the second |
-gt | The first number is greater than the second |
-le | The first number is less than, or equal to, 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
- There are two additional operators ...
- that
test
uses when evaluating two expressions
- It is placed between the two expressions
Operator | Condition Tested |
-a | Logical AND meaning both expressions must be true |
-o | Logical OR meaning either of the two expressions must be true |
- The exclamation mark, ! is a negation operator
- It changes the value of the following logical expression
- It changes a false expression to true
- And a true expression to false
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
...
- in each pass through the loop
- But sometimes, you want to skip all or part ...
- of the commands in the loop block ...
- under specific conditions
continue
causes the shell to stop running the rest of the code ...
- between the
do
and done
keywords
- 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
- When you start a loop, you specify the conditions ...
- which will cause the loop to end
- With
for ... in
and simple for
loops, the code leaves the loop ...
- when every value in the argument list has been used
- In the
while
, until
and three expression for
loops ...
- the code exits the loop when a logical condition is met
- But what if you encountered some unusual condition ...
- and wanted to break out of the loop entirely?
- To do this, you would have to use
break
- When bash comes across the
break
keyword ...
- it jumps out of the loop
read
Command
Attendance