Here is a general outline of the key concepts and commands (arranged by topic) that you should know for Exam 02.
The exam will have the following format:
Commands: Identify which command performs a certain task.
Short Answers: Briefly answer questions about the Unix Philosophy and Software Installation.
Debugging: Fix some simple shell scripts.
Code Evaluation: Determine the results of commands involving cut
,
tr
, grep
, or sed
.
Filters: Use Unix filters to answer some questions.
Scripting: Write a shell script that utilizes command line arguments and filters.
Parts 5 and 6 can be done with the aid of your laptop and the Internet (but not other people). Parts 1 through 4 is to be done first on paper.
What is the purpose of an installer? What sort of things does this type of program do (identify at least three things)?
What is a package manager and why do many Unix systems use them?
When do you need administrator priviledges to install software?
How would you use configure
and make
to install software to your
$HOME
directory? What environment variables would you need to update
to ensure you can execute the program you installed? Use the libraries?
What is a shell script? What is a she-bang and why do we need it (or what does it do)?
You should know the basics of the shell programming language:
variables
capturing STDOUT
if statement
case statement
for loop
while loop
function
trap
How do you debug a shell script? What different modes do we have to help us figure out what a script is doing?
The following scripts contain errors. Identify and correct all the bugs.
This script checks each file passed via command line arguments and reports if the file is readable.
for $file in $@; do
[-r $file] | echo file is readable!
done
This script checks each file passed via command line arguments and reports if the file is readable.
is_readable() {
test -r $@
}
while ( -n "$@" ); do
is_readable($@) | echo file is readable!
done
Write the following shell script:
$ ./column.sh -h
usage: column.sh [-n N -d D -S -R] column
-n N Number of items to display (default is 5).
-d D Delimiter to use (default is ",").
-S Sort output.
-R Randomize output.
This script selects a column (default is 1) from the CSV input and then
displays the first N items.
Examples:
$ ./column.sh -d : < tmnt.txt
Leonardo
Donatello
Raphael
Michelangelo
$ ./column.sh -d : -n 1 < tmnt.txt
Leonardo
$ ./column.sh -d : -n 1 3 < tmnt.txt
katana
$ ./column.sh -d : -S 3 < tmnt.txt
bo
katana
nunchucks
sai
$ ./column.sh -d : -R 3 < tmnt.txt
nunchucks
katana
sai
bo
Note: The tmnt.txt
file is described below.
What are the three tenets of the Unix Philosophy?
What are regular expressions?
You will need to know the basic metacharacters such as
.
*
[]
[^]
^
$
()
|
\n
{m, n}
You will also need to be familiar with the basic character classes and their character set equivalents such as:
[:alpha:] = [a-zA-Z]
[:lower:] = [a-z]
[:upper:] = [A-Z]
[:space:] = [ \t\r\n\v\f]
[:digit:] = [0-9]
What are filters? What do they do and how are they related to the Unix Philosophy?
Given the file tmnt.txt
, which contains:
Leonardo:blue:katana
Donatello:purple:bo
Raphael:red:sai
Michelangelo:orange:nunchucks
What is the result of the following shell commands?
cut:
echo "Leonardo,Donatello,Raphael,Michelangelo" | cut -d , -f 1
echo "Leonardo,Donatello,Raphael,Michelangelo" | cut -d , -f 1,3
echo "Leonardo,Donatello,Raphael,Michelangelo" | cut -d , -f 2-
echo "Leonardo,Donatello,Raphael,Michelangelo" | cut -b 1,15,39
tr:
echo "Pizza! Pizza!" | tr '!' '?'
echo "Pizza! Pizza!" | tr 'iaz' '145'
echo "Pizza! Pizza!" | tr -d '[n-z]'
echo "Pizza! Pizza!" | tr -d '[:punct:]'
grep:
cat tmnt.txt | grep '^[LM]'
cat tmnt.txt | grep 'e.*o'
cat tmnt.txt | grep -Eo '^[^: ]+'
cat tmnt.txt | grep -Eo '[a-zA-Z]+[al]{2}[^:]*'
sed:
cat tmnt.txt | sed -nr 's/Leonardo/Splinter/p'
cat tmnt.txt | sed -r '/lo/d'
cat tmnt.txt | sed -nr 's|.*:o([^: ]+).*|O\1|p'
cat tmnt.txt | sed -nr 's/.*([LD][^:]+)[: ]+([^:]+).*/\2/p'
Given the tmnt.txt
file above, write Unix pipelines that perform the
following tasks:
List only the names of the turtles in sorted order.
List only the colors of the turtles in sorted order.
List only the weapons of the turtles in sorted order.
List only the turtles whose names in lo
.
List only the turtles with last names with two consecutive vowels.
Count how many colors begin with a b
.
Count how many colors end with an e
.
List only the turtles names whose name ends with a vowel and whose weapon ends with a vowel.
List only the turtles names with two of the same consecutive letter (i.e.
aa
, bb
, etc.)
List the colors that don't end with a vowel.
List the weapons that contains a repeat letter anywhere in the string.