Introduction to UNIX, Subversion, and DrRacket

Lab 1 is a self-guided set of exercises intended to increase your familiarity with the command line, subversion, and DrRacket. Even though you will commit a file to your subversion repository as part of this work, there is no official submission associated with Lab 1 and you will not be evaluated on it. You will be evaluated on all subsequent lab exercises.

Follow the steps below and ask questions as needed as you go along.

The filesystem

Files and directories (called folders on macOS) form a hierarchy, called the filesystem: directories can contain files and other directories.

The topmost, or root, directory is called /; everything else lives below (or inside) the root in the hierarchy.

Every directory contains two virtual directories called . (a single dot) and .. (two dots); these represent the directory itself and its parent directory (i.e., the directory that contains it), respectively.

Processes

The term process refers to a running instance of a program. The kernel (the core of the operating system) tracks processes, schedules them, and services requests they make.

For us, the most important thing the kernel tracks about a process is the current working directory.

The current working directory is the directory the kernel starts at when finding files mentioned by a process.

For example, if a process has a working directory of /home/janedoe and references the file notes/cs151.tex, this relative path is joined by the kernel with the working directory to form the absolute path (one which starts at the root directory) /home/janedoe/notes/cs151.tex, which is the full path to the file that is accessed.

The shell

The most important unix program for this class is the shell, which reads and interprets typed commands to run other programs.

We’ll be using a shell called Bash, which is one of the most common.

Each command interpreted by a shell starts with the name of either a program or a shell builtin (a directive handled by the shell), followed by some number of (space-separated) arguments. These are referred to as "argv" (the argument vector).

The shell builtin or program receives argv as an input and uses it to determine which particular function to perform.

A common convention among builtins and programs is to accept a number of options or flags, prefixed with hyphens, which alter the behavior of the command, followed by a list of paths indicating which files or directories to operate on.

Flags are usually available in short or long form; the short form (such as "-h" to get a short help summary) uses one hyphen and a single letter, while the long form (such as "--help") has two hyphens and multiple letters.

Note, however, that these are just conventions and not all programs follow the same convention.

There are many programs installed and accessible via the shell, but some of the most useful for this class are as follows:

  • "pwd" is a builtin that prints the full path to the current working directory of the shell. It expects no additional arguments.

  • "cd" is a shell builtin that changes the current working directory of the shell. If given no arguments, it changes the current working directory to your home directory; otherwise it expects one argument, the relative or absolute path to change to.

  • "echo" is a builtin that simply prints each of its arguments as output. Try running echo a b and echo 'a b'; note that in the latter case the command gets a single argument with included spaces.

  • "ls" lists files in its working directory (which is inherited from the shell that runs it) or the directories listed in its argv.

  • "man" opens program manuals, providing help with using most of the programs accessible through the shell.

  • "cat" concatenates the files whose paths are given as arguments (or the data entered by the user, if given no arguments), and prints the resulting data.

  • "svn" (subversion) is the program we’ll be using to keep track of revisions of the programs we write in CS151.

Try the following shell commands:

  • Change to the root directory by typing cd /.

  • Type pwd and ls and have a look around.

  • Type cd with no arguments. Type pwd to see where you are.

  • Try cd .. and pwd.

  • Try cd - and pwd.

Here is another experiment that you can try:

  • Type cd with no arguments.

  • Type ls to see what’s there.

  • Type ls -1 (that’s the numeral 1) to display the contents of the working directory, one per line.

  • Notice the difference between the output of ls and the output of ls -F.

  • You can combine the effects of the options 1 and F in several ways: try ls -1 -F, ls -F -1, ls -1F and ls -F1.

  • Try ls -lFG.

  • There are many options to the ls command. Type man ls and read about some of them. Type q to exit man.

Interacting with the shell

Typing into the shell is not the exact same as typing into the text boxes you are used to.

Shells provide tools for more quickly entering commands than typing the entire line; they also permit some interactions with programs that have not explicitly requested input.

When typing commands, pressing the Tab key at any time will attempt to automatically finish typing the current word. If there are multiple valid words, the shell may display the possible options. You can type additional letters to disambiguate, then hit Tab again to type the unambiguous completion.

Previous commands run in the shell can be scrolled through with Up/Down or by pressing Ctrl+R and typing to search.

Computers are time-saving devices (anything they can compute, you can compute), and these shortcuts will save you a lot of time in the shell.

Individual or multiple filenames can also be specified with a simple shorthand called globbing. In globbing, a question mark (?) can stand in for a single letter, and a star (*) can stand in for any number of letters. A word that uses globbing is replaced by all matching filenames before the command is run.

Shells also allow moving the cursor around with Home/End or Ctrl+A/Ctrl+E (for beginning and end of line), Alt+Left/Alt+Right (to move by word), Alt+Backspace (to delete an entire word), Ctrl+T (to transpose the two characters around the cursor), and a number of other shortcuts.

In addition, Ctrl+C and Ctrl+\ (with the latter being more forceful) may be used to terminate the running process. Ctrl+D may be used to signal "end of input"; programs like shells will exit after their input ends.

Practice shell operations

  • Use "pwd" to see the current directory. Enter "cd /" to move to the root directory, then use "cd" and "ls" to find your way back to the directory you started in.

  • View the manual page for the "bash" shell. "man" opens the page in the local pager, a program called "less", which splits the output into pages and allows searching with / and exiting with q.

  • Run the "cat" program. Type some words, hit enter, and also play with the behavior of Ctrl+D in "cat".

  • Run the "cat" program again, and exit it with Ctrl+C.

  • Consult the help for "ls" to figure out how to show hidden files. Pass "ls" the flag to show these files, then see the contents of one of them by passing its name to "cat".

  • Type "bash" to run a second instance of the shell inside the first one. Try out a few commands, then enter "exit" or hit [.key]#Ctrl+D#to exit to your original shell.

  • Hit Ctrl+R and start to retype a command you remember typing earlier. Before typing the entire command, it should appear completed for you; hit Enter to re-run it.

  • Learn about the "whatis" program by looking at its manual entry. Then use "whatis" to find out what the "rm", "mkdir", and "rmdir" commands do.

Subversion (svn)

This class uses the Subversion version control system to manage programming assignments, including the distribution of sample code, the collection of assignments, and the returning of graded work.

Version control systems are a class of powerful tool that are used to track different revisions of files, allowing one to consult old revisions, undo mistakes, and make changes to a multi-person project without accidental overwriting of others' work.

Our use of subversion will be much more limited.

Subversion is a distributed system. There is a server machine, called phoenixforge.cs.uchicago.edu that holds the master copy of your work. A project tracked with version control is called a repository, and may consist of many files and folders. A collection of changes to the repository is called a revision and will have a unique number assigned to it.

To complete an assignment, you will need to checkout a local copy of your work (called a working repository), add and edit files in this local copy, and then commit your changes back to the server.

The "svn" program is used to interact with the server. This program has many subcommands, but the most important ones are these:

  • "svn checkout" downloads a copy (called a working copy) of the repository from the SVN server.

  • "svn commit" commits any changes to the local repository to the server. The state of the remove repository after a commit is called a revision. Each revision is accompanied by a message describing how it differs from the previous revision, which is passed with the "-m" flag.

  • "svn add" adds a file to the local working repository; set of those tracked in the SVN repository.

  • "svn update" updates the local repository with any changes that have been committed to the server from other machines/repositories.

  • "svn status" shows the state of the working copy relative to the last revision. It is useful to double-check which files have been changed before committing.

  • "svn log" shows the history of all past revisions of the repository for the working directory.

  • "svn delete" removes a file from the local working repository (but needs a commit to save this removal).

Note that most of these commands have to be run from inside your repository (the checkout command is the one exception).

Each registered student has a repository on the UChicago PhoenixForge SVN server already. You’ll check out your repository and commit a file for Lab 1. Unlike later labs, this work will not be graded and is just an exercise to become familiar with the process.

In the shell, change directory to your home directory (the command cd without any arguments will do this). Then checkout a local working repository by using the command:

svn checkout https://phoenixforge.cs.uchicago.edu/svn/CNET-cs151-win-19 cs151

where you replace CNET with your CNet ID. For example, if your UChicago email address is janedoe@uchicago.edu, then you would use the command

svn checkout https://phoenixforge.cs.uchicago.edu/svn/janedoe-cs151-win-19 cs151

This command will create a directory named cs151 (specified by the last argument to svn). Change directory into your repository (cd cs151) and list its contents (ls). You should see a single subdirectory called include.

The next step is to create a directory for Lab 1. Use the shell command mkdir lab1 to create the directory. Now if you do an ls, you should see to subdirectories: include and lab1. Note that lab1 has not yet been added to the repository. If you run the command svn status, you should see something like the following as a response:

?       lab1

The "?" means that svn does not know anything about the lab1 directory.

Next we are going to add an empty file to the lab1 directory. Change directory into lab1 and then run the command touch lab1.rkt. Change directory back to the parent (cd ..) and run the command svn add lab1. You should see the output

A         lab1
A         lab1/lab1.rkt

If you run the svn status command now, you should see the same message, which tells you that two files have been added to the local repository, but have not yet been committed to the server. The next step is to commit these local changes to the server; run the command

svn commit -m "add empty lab1.rkt file"

The output should be something like the following

Adding         lab1
Adding         lab1/lab1.rkt
Transmitting file data .done
Committing transaction...
Committed revision 2.

At this point, svn status should print nothing, since all local changes have been committed.

We can verify our commit by using phoenixforge’s web interface. Open a web browser and enter the URL

https://phoenixforge.cs.uchicago.edu

You should be able to login using your CNET id and password. Once you are logged in, you should be able to see the current state of your repository.

Some common svn mistakes to avoid:

  • remember to add new files.

  • remember to commit files after adding or editing.

  • if you are working on multiple machines (e.g., a CSIL machine and your personal laptop), make sure that you run svn update to get any changes you committed on the other machine before starting work.

  • if you already have a local working repository checked out, use svn update to get any new files that we might be providing for an assignment. Only use svn checkout when you want a fresh copy of the repository.

  • the message specified with the -m flag cannot be the same as a name of a file/directory in the current working directory.

Getting started with DrRacket

The programming language for this course is Typed Racket, a functional programming language that uses surrounding parentheses to denote the application of functions to arguments.

DrRacket is an integrated editor for Typed Racket and related languages; we’ll use DrRacket to write, run, and debug programs.

Open your lab1.rkt file in DrRacket (by finding it in your SVN checkout on your desktop).

When you finish working today, after saving lab1.rkt, be sure to run svn commit lab1.rkt again and verify its success (you should see the letter A at the left of SVN’s output). This is how you save your work for access on other machines and how you ensure your work is available to the graders.

DrRacket may at some point ask you to choose a "language level." For this exercise, select the "Beginning Student" level. When we start using Typed Racket you will select the "Determine language from source" option.

Type the following code into DrRacket’s definitions pane:

9

(+ 9 1)

(+ 1 9)

(- 9 1)

(- 1 9)

Click the "Run" button at the top of the DrRacket window. For each line entered above, consider how the printed result relates to the expression.

Type the following code into DrRacket’s definitions pane beneath your previous input, and click "Run" again.

(* 9 1)

(* 1 9)

(/ 9 1)

(/ 1 9)

Try these more complex expressions:

(+ (* 9 9) 2)

(- (* 9 9) (* 9 9 9))

Next, test some of Typed Racket’s relational operators:

(= (expt 9 1) (expt 9 2))

(< (expt 9 1) (expt 9 2))

(> (expt 9 1) (expt 9 2))

Finally, add these:

;; a : Integer
(define a 7)

;; b : Integer
(define b 8)

(* a b)

(- (* b b) (* a a))

At this point, you should feel free to add, remove or modify any of the expressions of definitions in the definitions pane.

Save your lab1.rkt file in whatever state it’s in. You need to use svn commit again on it to push the local copy to the SVN server.

If you’ve closed your terminal or want to double-check that you’ve committed the file, first open a terminal, then change working directory to your checkout of your repository, the CNET-cs151-win-18 directory.

Then run:

svn commit lab1 -m "committing lab1"

to commit the directory and its contents (the lab1.rkt file).

You will now use Racket to render a crude illustration of a local landmark, the Sears Tower (which I refuse to call by its other name).

At the top of your lab1.rkt file, type:

(require 2htdp/image)

Anywhere below, type:

(define sears-tower
  (above (beside (rectangle 2 11 "solid" "black")
                 (rectangle 3 1 "solid" "white")
                 (rectangle 2 11 "solid" "black"))
         (rectangle 10 30 "solid" "black")
         (rectangle 20 100 "solid" "black")))

After running, and hence registering this definition, you can view the illustration by typing sears-tower in the interactions pane.

Save the file. You don’t need to add lab1.rkt to the repository again, because you’ve already done so. You do need to commit it again, though, and do not neglect the message (with the -m flag and quotes) in your commit command.