Return to lecture notes index
January 22, 2008 (Lecture 3)

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:



  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:


In class, we did the following a "Terminal" window:

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 c

One 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:

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


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: