COP 3344-01 Introduction to Unix: Lecture 5
Learning objectives
After this class, you should be able to:
- Write simple shell scripts that use the Bourne shell, and run them. In particular, you should be able to use the following features: (i) use command line arguments, (ii) define and use variables, (iii) run a command and assign its output to a variable, (iv) use
set
to redefine the command line arguments, (v) write comments, and (vi) read input usingread
.- Use the following Unix commands:
echo
andexpr
.- Use quoting rules to enable or disable the special meaning of special characters.
Reading assignment
- JEA: Chapters 10.12, 33.1-33.10, Single Unix Specification for definitions of the commands.
- Lecture slides: Lecture 5.
Exercises and review questions
- Questions on current lecture's material
- Assume that you are in a directory with the following files:
file1
,file2
, andfile3
, and that you have also defined a variable calledtemp
to have the valuefile
. Give the output of each of the following commands: (i)echo Should I list each file?
, (ii)echo "Should I list each file?"
, (iii)echo 'Should I list each file?'
, (iv)echo Should I list each $temp?
, (v)echo "Should I list each $temp?"
, and (vi)echo 'Should I list each $temp?'
.- Write a shell script called
mult
which will take two command line arguments and output their product.- Write a shell script called
mult2
which usesread
to read in two numbers that are input, and outputs their product.- Write a shell script called
ndays
which will take two command line arguments. The first one is a number that denotes a month (for example,February
is2
) and the second one is a year. The script should output the number of days in that month in that year. You can make use ofcal
and other utilities that we have discussed in class.- Write a shell script called
ndays2
which will output the number of days in whichever month the command is run. For example, it should output the number of days inOct 2007
if it is run today. You can usedate
andset
to help you with this.- Questions on next lecture's material
- None. Please start preparing for the midterm.