Introduction
Announcements

Schedule
Labs
Assignments
TA office hours

Tests, exam

Topic videos
Some course notes
Extra problems
Lecture recordings

Discussion board

Grades so far

CSC 209 midterm

2 March 2011, 18:10

Aids permitted: Any paper. Calculators are not permitted.

There are five questions and they are equally weighted.
Time allotted: 45 minutes.

Since time is short, be careful not to get stuck on one question to the exclusion of others. Not everyone will necessarily be able to finish this test within the 45 minutes. The amount of marks or answer-space allotted does not indicate how long it will take you to complete the question, nor does the size of the answer-space indicate the size of the correct answer.

Answer all questions. Answer questions in the space provided.


1. a) The command "speak" outputs something to the standard error. Write an sh command to capture this into a file named "speech".

b) The command "powercord" outputs something to the standard output. Write an sh command to run this command but with the output going instead to the standard error output (of the shell script in which your command appears).

[solutions]


2. What is the output on stdout from typing the following commands to sh?

a)

	a=3
	b=4
	c=5
	expr $a + $b$c

b)

	echo hello | wc -c
	(echo hello; echo goodbye) | wc -l
	(echo hello; echo goodbye) | sort
(recall that "wc -c" counts characters (bytes) and "wc -l" counts lines)

[solutions]


3. Write shell commands to output the following:

a) all lines from the file "staple" containing both an 'x' and a digit

b) either the word "happy" or "sad", depending upon whether or not the number in the file "nail" is greater than 50 (greater than 50 is happy) (assume that the file "nail" contains just one integer and no other data)

[sample solutions]


4. Write a 'while' loop in sh to add the squares of the integers from 20 to 50, inclusive, and output the total.

[sample solution]


5. Recall the version of the 'for' syntax in which you type "for" and a variable name, but nothing else on the line: This goes through all command-line arguments. Write a shell script in sh which goes through its command-line arguments (which are supposed to be file names) and deletes (with 'rm') all files which contain (according to grep) the string "spatula".

[sample solution]