Comp11
Using the Shell

The Programmer's View

Overview

Long before the slick graphical user interfaces we all use today (with windows, the mouse, folders, drag-and-drop, all that great stuff), most computers were accessed from simple text-only terminals. Here's the kind of set-up I used when I took introduction to computer science:

Not pretty.

Today, the terminal still exists as an alternative way of interacting with the computer. Rather than a physical device, it is a program that presents a text-only command-line interface. This method is still prefered by many programmers, however, for several good reasons. First, typing commands is fast and accurate. Second, we can easily specify very complex instructions (e.g., "copy all the files whose name starts with comp11 to a folder named Stuff") that would otherwise involve carefully clicking on a bunch of icons. Third, we can store commonly used sequences of commands and reuse them (like macros in Microsoft Word or Excel).

The computers in the CS lab are running an operating system called Linux. An operating system is a special piece of software that manages the underlying devices and allows ordinary programs (web browsers, word processors, spreadsheets, etc) to run properly. You are probably familiar with two other operating systems: Microsoft Windows and Apple's MacOS. MacOS is actually quite similar to Linux, and supports most of the features I describe below. Two other names you might have heard -- iOS and Android -- are the operating systems that run smartphones and tablet computers.

0. Logging in

The computers we will use are in Halligan Hall rooms 116 and 118. Room 116 is reserved for labs (but is free all other times). You can use room 118 any time, and it is where we will hold all office hours.

When you sit down at a computer you will need to type in a username and password. Your username is your UTLN. To get your password you will need to do one of two things:

• Existing students (started 2009 or earlier): follow this link, log in using your Trumpter password, and choose "Enable or reset your existing ECE/CS UNIX account"

• Freshman or students new in 2010: you will need to visit the IT staff office in Room 231 in Halligan Hall (on the second floor all the way at the end of the long hall.)

Log in to any lab machine using your username and password. You may be asked to change your password right away (a good idea anyway). Please choose something that you will remember, but no one else could guess. For example, do not use your username as your password! When you are done working in the lab, make sure you choose "Logout" from the menu.

1. The shell

The shell is a special program that runs in the terminal window and provides the command-line interface. Using the shell is a bit like a dialog or script: the shell prompts you for a command, you type one and hit return, it runs the command and prints the result. Rinse and repeat.

Commands are just regular programs (or applications) on the computer; many of them are small "utility" programs that only perform one or two specific tasks. Commands, like functions, can take arguments that control their behavior. Command line arguments follow the command name and are separated by spaces (no need for parenthesis or commas). The prompt is typically the name of the computer, followed by a "%" sign. When you see the prompt you can start typing commands.

Here is an example using a few simple commands (the name of the computer I'm on is lab118d, my typing is in red, each command's output is in blue):

lab118d% use comp11
lab118d% date
Fri Feb  4 20:42:20 EST 2011
lab118d% echo "Hello there"
Hello there
lab118d% whoami
sguyer
lab118d% logout

Start the terminal application. You will see a new window with a command prompt at the top. The shell is waiting for you to type commands.

IMPORTANT: for your first command, please type use comp11 and hit return. This command will give you access to our programming tools.

2. Working with files

Most shell commands operate on files -- creating, editing, compiling, renaming, moving, viewing, etc -- so the arguments to these commands are often file names. File names are similar to what you might see in Windows Exporer or Mac Finder, except that they include an extension of the form .xyz that indicates what kind of file it is. For example, a song file might be named yellowsubmarine.mp3, indicating it is stored using the mp3 encoding. Photo files usually end in .jpg or .jpeg; regular text files end in .txt; web pages often end in .html; C++ source code files end in .cpp; and many, many more. Explorer and Finder usually hide the extension from you, and instead show you an icon that depicts the file type.

The organization of files also looks different in the shell. Folders are refered to as directories. A file is uniquely identified by its name and the sequence of directory names leading to the file. The full path of a file is the sequence of directory names separated by "/". The top-most directory is just called "/". My mp3 file, for example, might have the full path name /h/sguyer/Music/Beatles/yellowsubmarine.mp3. File names, like variables in C++, are case-sensitive -- foo.mp3 is a different file from Foo.mp3 and FOO.MP3.

Our files are stored together in one giant set of directories on a central file server. To enable us to peacefully coexist, directories and files have permissions associated with them: who may read them, modify (write) them, or look at the contents of a directory. Each of us is given a home directory in which we have permission to freely create, read, and write our own files. The name of your home directory is the same as your log in name. Other parts of the system, for example the shell commands, are readable to everyone (so you can use them), but only writable to IT staff in Halligan.

Here is an abridged view of the overall file system structure:

Directory Description
/  Top-most directory
/h  Contains all user home directories
/h/sguyer  My home directory
/comp  Directory for all CS courses
/comp/11  Directory for comp11
/bin  Directory for some commands
/usr/bin  Directory for more commands
/usr/lib  Directory for code libraries
/usr/include  Directory with information about what functions are in the code libraries
/etc  System configuration

Notice: commands (programs) are also stored in files (for example, in /usr/bin), just like anything else. These files are called binaries because they contain low-level instructions for the computer. On our lab system, program files do not have any file name extension; in Microsoft Windows most program files have the extension .exe.

Some interesting files to look at: /etc/cpuinfo (all the details about the processor in your computer) and /etc/meminfo (memory information).

3. File commands

Here are some of the most useful file manipulation commands. Each one takes one or more file names as arguments:

Command Description
ls directoryname List contents of a directory
cat filename Print contents of a file to screen
more filename Print contents of a file, but one page at a time
cp from to Copy a file ("from" must be an existing file; "to" will be the name of the copy)
mv from to Move a file (also used to rename a file)
rm filename Remove (delete) a file
mkdir directoryname Make a new directory
rmdir directoryname Delete a directory (note: it must be empty)

4. Current working directory

If we had to type in full paths for each of these arguments, the shell would be very tedious to use. Instead, the shell keeps track of our current working directory, and any commands we type are run in that directory. Two special commands allow us to ask where we are (what is the current working directory), and move to a different directory:

Command Description
pwd Print current working directory
cd directoryname Change current working directory

All file names are interpreted relative to the current working directory. So if my current working directory is /h/sguyer/Music, then I can specify my mp3 file by typing Beatles/yellowsubmarine.mp3. On the other hand, if my current working directory is /h/sguyer/Music/Beatles, then I can just type yellowsubmarine.mp3.

There are also two special directory names: . (period) refers to the current working directory (whatever it is), and .. (two periods) refers to the directory above it. You can use .. to navigate up to higher-level directories. The ls command, with no arguments, lists the files in the current working directory. This is equivalent to typing ls .

When you first log on to the computer your current working directory will by your home directory. Here are some examples of me typing navigation commands (and the computer's response):

lab118d% pwd
/h/sguyer
lab118d% ls
Academic          Letters         Research
Classes           Papers          Reviews
Documents         Projects
Funding           Public
lab118d% mkdir comp11
lab118d% ls
Academic          Letters         Research
Classes           Papers          Reviews
Documents         Projects        comp11
Funding           Public
lab118d% cd comp11
lab118d% pwd
/h/sguyer/comp11
lab118d% mkdir assignment1
lab118d% cd assignment1
lab118d% pwd
/h/sguyer/comp11/assignment1
lab118d% cd ..
lab118d% pwd
/h/sguyer/comp11
lab118d% cd ..
lab118d% pwd
/h/sguyer
lab118d% cd ../hescott
lab118d% pwd
/h/hescott
lab118d% ls
ls: .: Permission denied

TODO: navigate around the file system using cd, pwd, and ls. Try using different combinations of full paths and relative paths. Notice which directories you have permission to view and which you don't.

In case you forget where your home directory is, typing cd with no arguments takes you back to your home directory. In addition, the ~ character is shorthand for your home directory path (in my case ~ is the same as /h/sguyer).

5. Command line options and getting help

There is a huge number of commands available in the shell. In addition, commands often have many special command line arguments that you can use to alter their behavior. The typical naming scheme for these options is a single letter preceded by a - (dash). The ls command, for example, gives you a detailed listing when you specify -l (for "long" listing). It can also color the names of the files based on their type using the -G option. You can also combine options by adding them one-by-one on the command line.

TODO go to a directory (like /comp/11/files) and try ls, ls -G and ls -l.

It can be difficult to remember all the different options, so the system provides a detailed system of help documents called man pages (short for manual pages). To read the description of any command type man command. Many man pages are long -- to see the next page, hit the spacebar; to exit hit the q key.

TODO: try typing man ls -- notice how many options there are just for this one command!

6. Searching with grep

grep is one of the most powerful utility programs available. Its name stands for "general regular expression parser" and it is used to search the contents of files for particular patterns of text. For example, if we had a file containing the lyrics to Yellow Submarine, we could use grep to find places in the file that mention the word "sea". Grep uses a pattern language called regular expressions, which we will not go into in great detail here, but is certainly worth learning about. For now, we will use grep in the following form:

grep "some text" filename

Grep prints out the lines in the file that contain the text specified. You can also give it the -n option to make it print the line number of each line that matches. Here is an example:

sunfire32% cd /comp/11/files
sunfire32% ls
examples  include  lib  yellowsubmarine.txt
sunfire32% grep "sea" yellowsubmarine.txt
Lived a man who sailed to sea,
Till we found the sea green,
Sky of blue,(sky of blue) and sea green,(sea of green)
sunfire32% grep -n "sea" yellowsubmarine.txt
2:Lived a man who sailed to sea,
7:Till we found the sea green,
35:Sky of blue,(sky of blue) and sea green,(sea of green)

You can specify as many files as you like on the command line and grep will search them all. Like ls, grep has many, many options, which you can learn about by typing man grep.

7. Editing C++ programs

All of the programs we write will consist of one or more ordinary text files containing C++ source code. There are many different programs for editing text files, but we will use one called JEdit because it has a very nice interface, and because it has a number of powerful features specifically for programmers:

All of these features will help you write code that works in less time.

Start up JEdit using the command

jedit filename.cpp &

Note that the name "jedit" is all lowercase, and that the last thing on the line is an ampersand.

IMPORTANT: The first thing I'd like you to do is set a few configuration options in JEdit.
Please follow these directions carefully:

Most of JEdit's interface should be familiar: you can create, open, save, and close files. The edit menu includes the usual cut, copy, and paste. Try creating a new file, and experiment with various menu items.

Remember: all C++ source files should end with the .cpp file name extension.

8. Compiling and running

As I mentioned in class, the computer cannot run C++ programs directly in the form we write them. Instead we use a compiler to turn C++ source code into a form that the computer can run.

The compiler performs two main tasks. First, it checks that the files you give it contain correct C++ source code, and will report any errors it finds. The compiler we are using has very good error messages, which should help you quickly diagnose and fix these errors. Please don't feel discouraged, though -- all beginning programmers spend a lot of time fixing errors reported by the compiler.

When you have fixed all the compile errors, it will perform the second important task: translate your program into machine code, the low-level language that the computer understands. The machine code will be stored in a new file. You can then run this program as you would any other command.

To run the compiler, use the following command:

compile source-files -o programname

The program name can be whatever you like, but it is conventional to give it the same name as one of the source code files, but with no extension.

To run the program type ./programname.

9. Submitting programs for grading

When you have completed an assignment you will submit your code for grading using another command called provide. When you run provide, you tell it the course name (comp11), the assignment, and the files you want to submit. You can submit as many times as you like up to the deadline.

provide comp11 assignment cpp-files

Labs are not graded, but we would like you to submit whatever you write by the end of the lab session.


Back to Comp15.