Introduction to UNIX, Subversion, and DrRacket
The filesystem
Files and folders (directories) form a hierarchy,
called the filesystem: directories can contain files and other directories.
The top directory is called /.
Everything is inside this directory or its contents (macOS hides the root directory;
press Apple+Shift+G in Finder and enter /
to reach it in the graphical file manager).
Every directory contains two virtual directories called .
(a single dot) and .. (two dots); these represent the directory
itself and the directory that holds 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.
For convenience, short flags may usually be combined as well as passed separately,
such as -tf instead of writing out -t -f.
Note, however, that these are just conventions and not all programs follow the same convention.
- cd is a shell builtin that changes the working directory of the shell; it expects one argument, the relative or absolute path to change to.
- pwd is a builtin that prints the present working directory of the shell. It expects no additional arguments.
- 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.
- help is a builtin that lists the known shell builtins and can give help on each if passed as an argument. Note that "help" only provides documentation on shell builtins, not other programs.
- type is a builtin that indicates whether a command name refers to a shell builtin or a separate program.
There are many programs installed and accessible via the shell, but some of the most useful for this class are as follows:
- 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.
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.
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 Ctrl+Dto 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 to students, 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 that holds the master copy of your work.
To complete an assignment, you will need to checkout a local
copy of your work, add and edit files in this local copy, and then commit
your changes back to the server.
The local svn program is used to interact with the
server.
The svn 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 add adds a file to the set of those tracked in the SVN repository.
- svn mkdir creates a new directory tracked in the SVN repository.
- 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 commit saves the current tracked files and directories as a new revision. Each revision is accompanied by a message describing how it differs from the previous revision; this is passed with the -m flag.
- svn log shows the history of all past revisions of the repository for the working directory.
- svn delete removes a file from the repository (but needs a commit to save this removal).
A project tracked with version control is called a repository, and may consist of many files and folders.
Each revision can include changes to multiple files.
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.
- First, change working directory to your Desktop directory, so the working copy of the SVN repository will be easy to find.
- Run svn checkout https://phoenixforge.cs.uchicago.edu/svn/CNET-cs151-win-18 (replacing CNET with your CNET ID) to check out the repository.
If you are asked to verify a TLS certificate, accept it temporarily or permanently.
This will create a working copy of your SVN repository, which you can then cd into. - Once inside your CNET-cs151-win-18 directory, create a lab1 directory by running svn mkdir lab1.
- To make sure your SVN repository is working, we'll create and save an empty file. cd into your lab1 direcory, and run touch lab1.rkt to create an empty file.
- Add the file to your repository with svn add lab1.rkt, then commit it with svn commit -m "add empty lab1.rkt file". Note the quotation marks around the multi-word commit message.
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.
Dr. Racket is an integrated editor for Typed Racket and related languages; we'll use Dr. Racket to write, run, and debug programs.
Open your lab1.rkt file in Dr. Racket (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:
Click the "Run" button at the top of the DrRacket window.9 (+ 9 1) (+ 1 9) (- 9 1) (- 1 9)
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:
to commit the directory and its contents (the lab1.rkt file).svn commit lab1 -m "committing lab1"
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:
Anywhere below, type:(require 2htdp/image)
After running, and hence registering this definition, you can view the illustration by typing(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")))
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.