If you are logged in on a workstation, in the terminal window, first of all run "/u/csc209h/summer/present". This records your attendance and is required for you to get the marks for the lab.
If you are logged in via ssh, please tell your
account name (UTORid) to the TA and they will mark you as present.
Then, please check that you are correctly marked as present by checking
the web page
https://wwwcgi.teach.cs.toronto.edu/~ajr/cgi-bin/auth/present
Please cd to the directory /u/csc209h/summer/pub/lab/02 (relevant to some but not all of the items below), and write a unix command to produce the requested output:
2. Examine the seq command. It's very like the python range() function, although the bounds are inclusive. For example, try "seq 5 10".
3. Examine the dc command. Its input is in "reverse-Polish
notation". There is a command "p" for "print" which outputs the top of
the stack.
Try typing input such as "3 4 + p",
or "3 4 5 * * p".
Whether or not spaces are required is pretty much the same rule as you've
learned to expect in programming languages; for example,
the second example above could be written as "3 4 5**p".
And any whitespace is equivalent (spaces, newlines, etc).
End-of-file exits dc, or the 'q' command.
4. A deck of 52 cards can be shuffled into 52! distinct permutations (i.e. the product of the integers from 1 to 52 inclusive). What number is this? Write the smallest sh command which outputs the number.
We can change the first 'o' to 'x' in each line with: sed s/o/x/
The "sed s/o/x/" can be followed by file names, or it can read from the standard input.
If the standard input is the terminal, we have to press ^D as the special
end-of-file-from-terminal signal; instead of doing that, in this question
we will use 'echo' and a pipe, like this:
echo Madonna Ciccone | sed s/o/x/
1. That only translates the first 'o' on each line. How do you translate them all?
2. Why aren't quotes needed around "s/o/x/" above?
3. Write the 'sed' command which translates names of the form "Madonna
Ciccone" to "Ciccone, Madonna" as demonstrated in the first lecture.
Tools to use: regular expressions (the normal kind, not the "glob" notation);
and backslashed-parentheses in the search string matched with backslashed
digits in the replace string.
If you can't reproduce this, it's here (you'll need that to
go on).
Why are quotes needed here but not in the earlier example?
4. Write a sed command which doubles the first 'x' on each line. For example, "h@x0r" would become "h@xx0r". Don't use quotes unless necessary. Put this in a file named "dblx" ("dbl" for "double").
5. Write a sed command which doubles the first digit on each line. For example, "h@x0r" would become "h@x00r". Don't use quotes unless necessary. Put this in a file named "dbld".
a)
foo=bar bar=baz read foo
b)
foo=bar bar=baz read $foo
Figure this out in your head (or on paper) without actually trying the commands, then try the commands on computer to verify your answer.
2a) Assuming that the variable 'i' contains a well-formed positive integer, write a command to output that number of lines containing your name. For example, if i is 3, my command would output
ajr ajr ajrJust use standard unix software tools for now, not a loop construct.
2b) As in (a) but put them all on the same line (with a newline character at the end, and no extra spaces (including at the end of the line)).
3) Experiment with the command
x=`expr $x + 1`(you'll need to assign a number to 'x' first!)
4) Suppose that the output from "ls -l myfile" looks like
-rwxr-xr-x 1 ajr instrs 8304 Apr 30 16:55 myfilewith only one space between each field. Write a shell command to set the variable "month" to the month value in the output of ls -l myfile (e.g. if the above is ls's output, month would be "Apr").
5) Experiment with the command
echo `seq 5 10`Understand its behaviour, and try variants.
seq 5 10When would you prefer one and when would you prefer the other?
6) We want to output the sum of x and y. The following command is needlessly complicated. What's a simpler way to write it?
echo `expr $x + $y`
$ sh hello What is your name? Sally Are you a girl or a boy? girl How old are you? 10After that exciting initial interaction, your program outputs the following story:
Once upon a time there was a little girl named Sally. Sally had a brother named Bill who was three years older. Bill was 13 years old.Note: use this this text verbatim, as this lab will be graded automatically! (Substituting the appropriate substitutions, of course. Note the computation to get that "13" in the example.)
You do not have to do any validity checks on the input. If the user types something strange (or just presses return) for the first two prompts, your program will output strange results. If the user types something strange for the third prompt, your program will probably misbehave. All of this is fine. Since you might not have done 'if' in shell scripts yet by this point, just assume that the inputs are valid.
Formulate a command-line to run your shell script with /u/csc209h/summer/pub/lab/02/input on the standard input, and diff its output with /u/csc209h/summer/pub/lab/02/output to make sure you got it right. For full marks for this lab, you must have no differences at all, not even spaces. Again, this is an exercise in having full control of the tools you are using (rather than being any sort of assertion that my choice of spacing is superior).
Note: You do not modify your shell script to read from the file /u/csc209h/summer/pub/lab/02/input; you just use the '<' operator in the shell to redirect the input to your shell script. Your shell script can still be used interactively (and probably usually would be). And of course your shell script must still work for other correct inputs (not just those in the sample input file).
if test $x -lt 3 then echo foo fiThere is also an 'else'.
2) Using seq, output the numbers from $x to $y inclusive,
starting with $x and ending with $y.
But you can't just say "seq $x $y" because y might be less than x,
in which case you need to say "seq $x -1 $y".
3) If you aren't familiar with the Towers of Hanoi problem, then (assuming that you're not in the lab any more) please watch a video at https://www.youtube.com/watch?v=TwLDf1Dv1M0
The recursive solution in code looks like this:
procedure hanoi(ndisks, from, to, scratch): if ndisks > 0: hanoi(ndisks - 1, from, scratch, to) print 'move disk', ndisks, 'from', from, 'to', to hanoi(ndisks - 1, scratch, to, from)
Write it as a shell script. It does recursion by running itself, assuming that it is in the current directory. You will invoke it (and it will recursively invoke itself) like this:
sh hanoi 5 A B Cwhich means to transfer five disks from pile 'A' to pile 'B', using 'C' as scratch space.
Within your shell script, you can access the first parameter (the number of disks) as $1, the second parameter (the 'from' pile) as $2, and so on.
What software tools principle does your friend's program violate? And give an example of a use for this tool for which your version is easier to use.
For bonus marks of about 10% of the lab value, answer this question in a short text file named "lab02p7". Your answer must be short and clear to receive the marks.
First of all, during the lab time, on the console of a lab workstation, you must run "/u/csc209h/summer/present", or get the TA to mark you as present.
Then, by midnight at the end of Friday May 20, you must submit solutions to part 3 exercises #4 and #5, part 5, and optionally the part 7 question, as follows.
First, put the solutions in separate files named dblx (part 3 question 4), dbld (part 3 question 5), hello (part 5), and optionally lab02p7 (part 7). You can test your part 3 solutions with commands such as
echo xylo6h0ne | sh dbldas well as
sh dbld <file(for some file named "file").
Make sure that your lab02p7 solution is a plain text file by typing "cat lab02p7".
Secondly, submit with the command
submit -c csc209h -a lab02 dblx dbld hello lab02p7
Note in this command that the "lab02" is the "assignment" name you are
submitting under, and the "dblx", "dbld", "hello",
and "lab02p7"
are the files to submit.
You can submit them simultaneously as above, or at different times.
You may still change your files and resubmit them any
time up to the due time.
You can resubmit a file by using the −f option, e.g.
submit -c csc209h -a lab02 -f dbldAs well, you can check that your files have been submitted with the command
submit -c csc209h -a lab02 -l(that's minus 'l' for "list", not a 'one')
If there are any problems with this, please ask by e-mail. Problems running the 'submit' command will not generally be accepted as a reason for lateness.
The lab work in this course is individual. You can discuss the items which are not for course credit with anyone, but the items which are submitted for course credit must represent your own work. It is also an offence to assist others in committing an academic offence (e.g. by providing your solution to them).
If you have questions about work to be submitted for course credit, ask a TA or the instructor (perhaps by e-mail). More information at the end of the course information sheet.