Quiz for Monday September 27, 2004 and tips on debugging I can only assume that most of you are doing just fine in writing shell scripts; I held more than nine open office hours this past week and only a few of you came for help. You're going to need to know how to write shell scripts in order to complete you midterms projects. I want to remind you that this course does NOT assume any prior knowledge of programming or computer science. You will learn what you need to know about programming. Right now we're studying shell scripts because they provide the best means for you to leverage powerful libraries. Writing programs in a suitable scripting language is the most common way that professionals in many disciplines carry out routine computing tasks in their daily work whether their work involves bioinformatics, physics, mathematics, business, engineering and the social sciences. But you won't learn to write shell scripts if you don't practice. There is no reading for Monday, but THERE IS A QUIZ; you're to do the following two exercises. Print out your solutions and bring them to class on Monday morning. Here are the exercises: 1. Exercise 1 that we did in class on Monday required you to use a 'while' loop. I want you to redo the exercise but this time use a 'foreach' loop. Here's the statement of the original exercise: Write a script called 'divides' that takes two or more numbers specified as arguments on the command line and prints out each number that is evenly divisible by the first number in the list. % divides 3 5 7 9 11 12 3 9 12 If you want something a little more challenging, print out only those numbers that are equal to the first number raised to some (integer) power. % powers 3 5 7 9 11 12 3 9 And if that isn't challenging enough, write a sieve for primes: % primes 2 3 4 5 6 7 2 3 5 7 2. Write a script called 'sumrows' that takes two arguments corresponding to files containing columns of numbers, and then prints out the sum of rows in the two columns. Here's an example illustrating the script's behavior: % cat one 3 4 % cat two 5 7 % sumrows one two 8 11 Your solution should exit with an appropriate message if the lengths of the two columns are different. The keyword 'exit' will force a shell script to terminate. In debugging your scripts, here are some common errors to check. Make sure you DON'T put a dollar sign before a variable when you're setting it either using 'set' or '@'. Make sure you DO put a dollar sign before a variable anywhere else you want to refer to its value. Make sure that you use '@' and not 'set' when you want to do some arithmetic operations when setting the value of a variable. Don't confuse the different ways of quoting expressions. In particular don't confuse the backtick operator ` (which is used to force the evaluation of an expression as in "set x = `ls`" or "foreach file ( `ls` )") with the single quote which can be used when you want to prevent the evaluation of variables. Remember to end every script with a final carriage return and remember to end every 'while' or 'foreach' loop with the 'end' keyword. The 'end' keyword should appear on a line all by itself with no additional spaces. You can always use 'info csh' to learn more about the C shell. If you've carefully checked for all the above problems and your script is still buggy, the next thing to do is to strategically insert some diagnostic print statements so you can get some idea of what's going on in your program as it executes. For example, if you have a script that uses the variable 'foo' and the program crashes at some point where it refers to '$foo', insert 'echo $foo' right before the statement that you suspect is causing problems. You can put this sort of diagnostic print statement anywhere in you program, including the body of a loop.