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
setto redefine the command line arguments, (v) write comments, and (vi) read input usingread.- Use the following Unix commands:
echoandexpr.- 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 calledtempto 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
multwhich will take two command line arguments and output their product.- Write a shell script called
mult2which usesreadto read in two numbers that are input, and outputs their product.- Write a shell script called
ndayswhich will take two command line arguments. The first one is a number that denotes a month (for example,Februaryis2) 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 ofcaland other utilities that we have discussed in class.- Write a shell script called
ndays2which will output the number of days in whichever month the command is run. For example, it should output the number of days inOct 2007if it is run today. You can usedateandsetto help you with this.- Questions on next lecture's material
- None. Please start preparing for the midterm.