Hello everyone, Here are the tutorial notes for this week. Remember, feel free to contact me if you have any questions about the material. (You don't have to email me after your tutorial this week, but if there are any problems please let me know about them.) Reminders ========= - Assignment 1 should be submitted by 6:00pm on Wednesday (i.e., before your tutorial), however, students that wish to use grace days have until Friday at 6:00pm. Keep this in mind in case people ask you about the assignment. Tutorial 3 Notes -- June 8 ========================== This week is kind of a strange week for tutorial material. We finished shell scripts last week, but haven't started C programming yet (we will this Wednesday). So, I'm asking you to cover a few more shell script examples, and then talk a bit about Makefiles (even though we haven't yet seen a C program in class :). (1) Shell scripts - The first shell script, "fact", calculates N! (factorial) recursively, where N is specified as the first argument of the command line: #!/bin/sh if [ $1 -eq 0 ] then echo 1 else a=`expr $1 - 1` f=`fact $a` # recursion echo `expr $1 \* $f` fi e.g., $ fact 0 1 $ fact 5 120 $ fact 8 40320 The main purpose of this script is for the students to see a simple example of shell script recursion. A good question to ask your class is: why does the final echo line only result in one line being printed on the screen? - The second script responds to a question I was asked during the last lecture: #!/bin/sh echo Before func defn func func() { echo In function: \"func\" return 5 } echo After func defn func echo Status: $? First, you could ask your students what they think the output will be they execute this script. The main concern will be whether or not the first call to "func" will work or not (it won't). It turns out that we get the following output (or something similar; the script name is "fn1"): Before func defn fn1: func: command not found After func defn In function: "func" Status: 5 So, the important point here is that the function should be defined before it can be used (this responds to the specific question I was asked). - I also had a question last week about whether or not the "return" was necessary in a function, and what the status value would be if the "return" was missing. Using the same "func" example as above, remove the "return 5" line from the function and ask your students what they think the output will be. In this case, the final value of the status variable $? will be set to 0 so the output would change to "Status: 0". The question, however, is why? In the absence of a "return", the status is set to the exit status of the last command executed (in this case the built-in "echo" command). Furthermore, a built-in command that run successfully has an exit status of 0, hence the result. To illustrate this property, present the following modified script: #!/bin/sh func2() { echo In function: \"func2\" return 5 } func1() { echo In function: \"func1\" func2 } func1 echo Status: $? In this case the output will be: In function: "func1" In function: "func2" Status: 5 (the important part being that the final value of $? is 5). (2) Makefiles - In the second part of your tutorial I'd like you to go over the basic structure of Makefiles. The easiest thing to do is to present a simple example, and then explain what the various parts of the Makefile do. Karen Reid has a nice (short) document that covers the basics, with a good example that you can use: http://www.cs.toronto.edu/~reid/csc209/02f/notes/make.html For your example you can ignore the contents of the source and header files (i.e., the actual contents are unimportant). You can tell your students to simply assume that you've written your code in a number of files, and that there are certain dependencies between these files. Some important points to emphasize: - The "make" utility is usually pretty strict about the makefile syntax. In particular, a tab must precede the command line that follows the dependency rule. - Make normally looks for a file named "Makefile" or "makefile" by default. To run make, simply type "make". To run make with a different Makefile, use "make -f makefilename". - When you are learning to write your first Makefile it is often easier to simply use an existing Makefile and modify it. - We will be using "gcc" for the C compiler in this course, and the standard make utility on CDF is GNU make (gmake). - Don't worry if you don't have time to finish this section. There will be time to revisit Makefiles next week. (Once we start writing C programs the students will be seeing a lot of Makefiles.)