Quiz 1
Today's quiz asked you to write a really simple shell script -- I promise next quiz will get more sophisticated. But, for today, we'll keep it simple:
- Write a program that lists the contests of a directory. The name of the directory should be supplied as a command-line argument.
Solution:
#!/bin/sh ls ${1}
Handing In Assignments
We spent a good bit of time going over the logistics of handing in assignments. The gist of the discussion and help session is this:
- The course directory is "/afs/andrew/course/15/123-kesden"
- The handin directory is "/afs/andrew/course/15/123-kesden/handin"
- Underneath the handin directory, you'll find a subdirectory for the assignment/quiz. If it doesn't happen to be there, you have the power to create it. For example, "/afs/andrew/course/15/123-kesden/handin/quiz1"
- Create a subdirectory for yourself, e.g., "/afs/andrew/course/15/123-kesden/handin/quiz1/gkesden"
- Copy your files, using the command line or file transfer client into this directory
- You have permissions to insert new files and to list the files -- but not permission to read files or to modify them once written
- You can verify the correctness of your submission using "ls -l" -- the file size should match the file size of the original and should certainly not be zero.
- Should you need to resubmit, you can do this as many times as you'd like, just create a new, sequentially numbered directory each time, such as "gkesden.1", then "gkesden.2", then "gkesden.3". We will grade only the most recent -- and throw away the rest.
Warnings:
- Please spell your andrewid correctly. Our grading system is script driven. If you misspell your andrewid, you won't get credited with your score.
- If you don't get feedback within a week, please let us know. There is likely a problem. We return all assignments within a week.
- Do not use Apple's Finder to Drag-and-Drop files -- they'll surely have a size of zero. Instead, try using "scp" as we did in class, or fetch, or some other ftp client or ssh-based tool. In fact, I'd be sckeptical of drag-and-drop, even on most windows systems.
In class, we did the following a "Terminal" window:
- ssh unix.andrew.cmu.edu
- cd /afs/andrew/course/15/123-kesden/quiz1
- mkdir gkesden {your userid, not mine, please}
- exit {now we are back to the local iMac}
- cd Desktop {or wherever you've got your files}
- scp quiz1.c gkesden@unix.andrew.cmu.edu:/afs/andrew/course/15/123-kesden/handin/quiz1/gkesden {again, use yourid, not mine}
Setting Positionals
Unlike other variables, positions can't be assigned values using the = operator. Instead, they can only be changed in a very limited way.The set command sets these values. Consider the following example:
set a b c # $1 is now a # $2 is now b # $3 is now cOne thing that should be noted about the set command. It accepts arguments, itself. These begin with the - sign. As a result, it can get confused and begin to interprete values that it should be assigning to positionals. To avoid this, the -- flag can be used:
set -- -a- -b- -c- # $1 is now -a- # $2 is now -b- # $3 is now -c-If there are more than 9 command-line arguments, there is a bit of a problem -- there are onyl 9 positionals: $1, $2, ..., $9. $0 is special and is the shell script's name.
To address this problem, the shift command can be used. It shifts all of the arguments to the left, throwing away $1. What would otherwise have been $10 becomes $9 -- and addressible. We'll talk more about shift after we've talked about while loops.
Quotes, Quotes, and More Quotes
Shell scripting has three different styles of quoting -- each with a diffent meaning:
- unquoted strings are normally interpreted
- "quoted strings are basically literals -- but $variables are evaluated"
- 'quoted strings are absolutely literally interpreted'
- `commands in quotes like this are executed, their output is then inserted as if it were assigned to a variable and then that variable was evaluated`
I think "quotes" and 'quotes' are pretty straight-forward -- and will be constantly reinforced. But, I do want to show an example using `quotes`:
day=`date | cut -d" " -f1` printf "Today is %s.\n" $day
expr
The expr program can be used to manipulate variables, normally interpreted as strings, as integers. Consider the following "adder" script:
sum=`expr $1 + $2` printf "%s + %s = %s\n" $1 $2 $sum
A Few Other Special Variables
We'll talk a bit more about these as we get into more complex examples. For now, I'd just like to mention them:
- $? - the exit status of the last program to exit
- $$ - The shell's pid