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 additional problems regarding shell scripting

1. Write a "glob" expression (filename wildcards in the shell, e.g. patterns like "*.c") which matches all filenames in the current directory with the following properties. Do not worry about the issue of filenames beginning with a dot.

a) filenames beginning with an 'x'

b) filenames ending with an 'x'

c) filenames containing an 'x', anywhere in the filename (multiple x's also match, e.g. xqcxnkxe is a match)

d) filenames beginning with a lowercase letter

e) filenames containing one or more digits, anywhere in the file name

[solutions]


2. Write a for loop to be executed in sh which goes through your directory "/usr/wilma/letters" (you are Wilma) and for each file in the directory, executes the command "grep Fred file". (Don't use xargs or find.)

[solution]


3. The file "/etc/passwd" lists all user accounts on the system, one per line. The command "wc -l" counts and reports the number of lines in its input. Write a single pipeline which outputs (only) the count of how many users have an 'e' in their user information somewhere (anywhere in their line in /etc/passwd).

[solution]


4. Write shell commands to perform the following actions. You may not use perl. You can assume that no files in the affected directory/directories have names beginning with a dot, except of course for dot and dotdot ('.' and '..').

a) Delete all files in /tmp with an 'x' in their name.

b) Delete all files in /tmp with an 'x' in the file (the contents of the file, as opposed to the file name).

c) Make subdirectories named "even" and "odd", and move all other files in the current directory into one of the subdirectories in accordance with whether the size of the file in bytes is even or odd. (Don't move a newly-created "even" or "odd" directory itself into a subdirectory! Also, you might want to know that 'expr' has a '%' operator (although there is also a good answer which doesn't use expr).)

[sample solutions]


5. Write a pipeline which produces a word-frequency count for a file (not an arbitrary file; just put a filename where applicable in your command). Non-words are stripped, words are down-cased, and "uniq −c" is used to get a count, then sort again into numeric order by count.
This problem was solved in an early lecture, but at this later date, reconstructing it without consulting the original solution should be a good problem.


6. Write a shell script to delete all files in the directory /home/ajr/q6 which have exactly three lines in them (in the opinion of "wc -l"). There may be subdirectories and symlinks and other special files in this directory, which you must skip over (you must not attempt to read their contents in any way, such as invoking "wc" on them).

[solution]


7. There is a plagiarism-checking program called "cheating", where "cheating file1 file2" outputs the probability, as a percentage value between 0 and 100 inclusive, that file1 and file2 are programs of students cheating off of each other. Write a shell script to compare all pairs of files in the directory /u/submit/csc209/a1 (which contains a single plain file per student) and output the ten most likely cheating cases, one pair per line, in the format:

	98 c4abcdef c4ghijkl
That is, "98" is the percentage probability output by the "cheating" command, and "c4abcdef" and "c4ghijkl" are the file names.

[solution]


8.
a) The file "letters" contains one letter per line. Considering upper and lower-case letters to be the same, output the number of occurrences of each letter, in descending order by number of occurrences. If a letter does not occur, it is acceptable to output "0 x" or not to output anything for that letter, whichever is easier. You may wish to use any or all of tr, sort, and awk (or other tools).

b) The file "text" does not contain only one letter per line; it is free-format text. Write a command whose output would be suitable as input for part (a), instead of the file "letters"; that is, your command outputs one letter per line and removes all non-letters.


9. Write the Towers of Hanoi program as a shell script. There are two basic strategies: run the shell script recursively as $0, or write a recursive shell function.

Here are solutions for the recursive sh invocation version and the shell function version.


10. Write a shell script to perform Euclid's greatest common divisor algorithm. The two input values will appear on the command line. You can assume that they are integers (if present -- you still have to check the argument count). Note that the "expr" command has a '%' operator.

Sample interaction, where '$' is the shell prompt:

	$ sh gcd 12 15
	3
	$ sh gcd 12 15 26
	usage: gcd x y
	$ 


11. Write a shell script to play a guessing game as follows, where the user's number is between 0 and 100 inclusive. Your shell script does a binary search by keeping track of a "low" and a "high" value such that the user's number, x, is always such that lowx<high. On each iteration, the guess is (low+high)/2, and this new number will be assigned either to low or high in accordance with the user's response.

	$ sh guess
	Is your number >=50 or <50?  Enter 'g' or 'l'.
	l
	Is your number >=25 or <25?  Enter 'g' or 'l'.
	g
	Is your number >=37 or <37?  Enter 'g' or 'l'.
	l
	Is your number >=31 or <31?  Enter 'g' or 'l'.
	g
	Is your number >=34 or <34?  Enter 'g' or 'l'.
	l
	Is your number >=32 or <32?  Enter 'g' or 'l'.
	g
	Is your number >=33 or <33?  Enter 'g' or 'l'.
	g
	Your number must be 33.
	$ 
(Once low==high-1, you exit the loop and conclude that the user's number must be low.)


12. Write a shell script to perform the same function as the Python "range" built-in function. Note that it can take one, two, or three arguments.

[solution]


Nearly any introductory programming problem would be a possible programming problem in sh, so long as it doesn't use extremely interesting data structures. (Although some interesting-data-structures programming problems are also good sh programming problems.)

Just keep in mind that which programming problems are easier or harder to solve in, for example, the Python programming language is a separate question than which programming problems are easier or harder to solve in sh. Some things are a lot easier and some are a lot harder.

Now, of course, a problem you have from first year which says to do something in a particular way might be Python-specific (or C-specific). Some of the early CSC A08 assignments probably don't have analogues in a non-object-oriented programming language, although the problem they solve when viewed from the outside can still be done in sh, but then it's often too trivial to be of interest.


Larger programs: