Lab 0: Introduction to the CS Lab

The first order of business is to get with a partner. If the number of people in this lab section is odd, we’ll need to have one group of three. Introduce yourselves.

You’ll be working in pairs today, taking turns driving and navigating. But before doing that, each of you should log in to your own account to make sure that works. If the screen is dark, you can wake it up by pressing a key: I suggest using the shift key, since that doesn’t actually type anything. You should see a box with a blank field where you can type your username; it is a good idea to hit the backspace key a couple of times before you start to clear out any junk that might have been typed previously. Your username is all lowercase.

After you type your username, you’ll need to hit the Enter key, then type your password and hit Enter again. If you are successful, you get a display of your desktop—though there may be a little extra delay the first time.

Talking about the mouse. Our mouse has several buttons; most of the time you’ll be using the left button. When the instructions say to ‘click’ you should press and release once; ’press’ means to press and hold down the button while you go on to some other action. Occasionally you’ll need to ’right-press’ or ’right-click.’ If you ever encounter instructions about a middle button, you press the scroll wheel. Most of the time, the rolling the scroll wheel will move what you are pointing at up or down.

Logging out. Pick one member of your pair to remain logged in; the other should log out. To do that, look for the gear-like icon at the upper right. Press the mouse button and select “Log out.” Caution: Lots of people aim for logging out but pick something else out of the menu. If you manage to log out, you will first have a dialog pop up that asks if you really intend to log out, and after you confirm the display will go back to the login screen.

You and your partner will now be working together on one computer. Be sure that the navigator knows what is going on each time the driver acts; the navigator will probably be the one who is usually looking at the instructions more closely.

Remember that the purpose in doing the lab exercises is to know how to do similar things in the future. I recommend that you take notes during each lab; you will also find it helpful if, over the semester, you keep a sheet of notes on the Unix commands you learn and of editor operations.

Exploring the desktop. After you log in, you should find yourself looking at a GNOME desktop—and most of it will be blank space. But it has menus at the top, and some buttons and other useful things will show up at the bottom. One of you just logged out by selecting that option from the menu at the top right.

Select Firefox in the Applications, Internet menu to launch a web browser. Then type the URL for this class’s main page in the address box. You probably want to make a bookmark for that, because you’ll be coming back to that page in the future. Navigate down to this lab; an easy way to get there will be to click on “Labs” near the bottom of the page, to bring up a listing of labs and projects. Then click on the link for this lab. What you see should look a bit like what you are reading right now.

Launch a Terminal window, too; you’ll find it under Applications, Accessories. You can move a window around by pressing in its top bar and moving the mouse.

Now look at the bar at the bottom of your screen. You should see a button there for each window that is open (one for Firefox and one for Terminal). Clicking on one of those buttons will do one of three things:

Typing tips In this and future labs, text that is typed into the computer will show up in a typewriter-like font, like this. References to special keys look like Esc, which refers to the ‘escape’ key (at the top left of the keyboard). A special case is the Ctrl key (lower left), which you hold down (like the Shift key): you’ll see ^c (pronounced “control c”) to mean “hold down Ctrl and type c”. (In some programs, you might see control-c spelled as C-c or ctrl-c, too.)

You’ll do a lot of your work by typing commands to your shell; in the instructions, those lines will be in typewriter font and begin with a ‘$’; you won’t type that character.

As a final caution, notice that the letter ‘l’ (ell) and the digit ‘1’ (one) can look quite similar. So pay close attention.

Looking at your files In the Places menu, select Home folder. That will launch a file browser, similar to the Mac OS Finder or the MS Windows Explorer. It will be looking at your home folder (directory).

Now bring your Terminal window to the top. You’ll see a line that starts with your username, has a bunch of other stuff, and ends with a ‘$’; that is the prompt from your shell.

For a first command, type

$ ls

and hit the Enter key. This command lists the contents of the current directory (folder). You should see the names of the same things that you can see in the file browser window.

Most commands have three parts:

  1. the command name,
  2. options and switches, which usually begin with one or two dashes (‘-’ or ‘--’), and
  3. arguments (usually the names of files or directories).

We could ask ls for more information with one or more of several options:

Names that start with a period (often pronounced ‘dot’) are normally hidden; the last of these options says to show them, too. Try giving ls various combinations of options; each option should be separated from what comes before it by one or more spaces.

When you use -a, you’ll see that there are some names starting with dot. Two of these names are special:

After the options, you can give ls the name of a directory/file (or more) that it should list; if you don’t supply a name, it will list ‘.’. So try listing the contents of ‘.’, ‘..’, and other directories that you can see. Combine those with options, too, as in

$ ls -l .
$ ls -a -F ..

Notice that Unix commands tend to be short and a bit cryptic. It may be a little intimidating, but you should be able to remember the ones you need most often.

Making directories and wandering about You might use these computers for more than one course; so it would be a good idea to make a directory for this class. You do that with the command mkdir:

$ mkdir csci235

If you now list the current directory, you should see the one that you have made. Notice that Unix commands typically do not print anything when they work; mkdir will complain if it doesn’t work, but if all you get is your command prompt, the command worked.

You can change the current directory with cd. But before going somewhere else, it is good to know where you are; the command

$ pwd

will print the current working directory. You should see something like /homes/mortmer.snerd, except with your user name instead of mortimer.snerd.

Now change into the directory you’ve made, and print where you are:

$ cd csci235
$ pwd

If you want to go up to the containing directory, use the special name dot-dot:

$ cd ..

It is also good to remember that you can always go home by giving cd without an argument. It is also convenient that they shell will treat a name that starts with ~/ as starting in your home directory, no matter where you are.

You might want to try the same moves in your File Browser: you can move into a folder by double-clicking on it. The upward-pointing arrow will take you into the parent of your current directory.

Working with files. In your terminal window, change back into the directory you made for this class, and pwd to make sure that you are there. Then make a directory lab0 for today’s work, and cd into it. Print the directory to make sure that is where you are.

Warning: Unix commands usually act as if you mean what you said; so they won’t normally ask you if you really meant to do something. It is a good idea to pause and think about it before you hit Enter when a command might do some damage. If you want to be cautious, the rm, mv and cp commands described below can be given a -i option (right after the command name) to make them ask before they overwrite or delete anything. (When it asks, type ‘y’ or ‘n’ and hit [Enter] to give your answer.)

Now you are going to copy a file to use in this lab; use the command

$ cp /cslab/class/csci235/labs/lab0/PrimeFactors.java .

Notice that there is a dot (period) as the second argument; this command says to copy a particular file from the class directory to dot, the current directory. If you use ls, you should see it now.

If the second argument is a file name, you’d copy the file to that name. So you could try

$ cp PrimeFactors.java Silly.java

If you list the directory, you should see both of them.

You rename a file by moving it with mv. So try

$ mv Funny.java Silly.java

That will not work, because there is no file named Funny.java. If you use -i, you can try

$ mv -i Silly.java PrimeFactors.java

it will ask you if you really want to, because the move would cause the old PrimeFactors.java to be discarded.

To delete a file, you remove it with rm. Be careful, because what is removed is gone for good. You could try

$ rm -i *

The * is a wildcard; it matches any filename. If you say yes, you will remove all of your files in this directory—which is O.K., because you can copy PrimeFactors.java from the class directory again.

Handouts for this class Use Firefox to visit the page for the class, and read what it says about how to find handouts and files from class meetings. Follow the instructions there to find and view the handout you received on Monday and the prep assignment for today.

Inside a file If you want to just look at what is in a file, the easy command to use is less.1 So, if you have a copy of PrimeFactors.java, you can look at it with

$ less PrimeFactors.java

You’ll be able to page through the file; the keys you want to know are that q will quit and space will page forward. If you want to know more, try h for help.

Edting For editing, we recommend that you learn at least a little about working with a program called Emacs. You’ll usually start it up with the name of a file to work on; you’ll also add an ampersand to the end of the command. So try

$ emacs PrimeFactors.java &

You’ll notice that Emacs creates a new window to work in; the ampersand tells the shell to prompt you for a new command as soon as Emacs has started, instead of waiting for Emacs to finish.

Emacs is a very powerful, customizable editor. If you’re going to do very much work, it will be worth taking some time to learn to do lots of things without taking your hands off the keyboard. But you’ll be able to manage for now using the mouse, the arrow keys, and the buttons and menus at the top of the window.

You don’t need to worry about what is in this file; it contains a Java program that we’ll come back to next week.

You can play around with the editor and make a few changes to the file. Emacs is designed to work even if you don’t have a mouse—and even if you don’t have arrow keys. You can do lots of things by holding down the Ctrl key (like a shift key) and hitting one of the other keys. In its instructions and tips, holding Ctrl and hitting G will be written ^G. Some Emacs commands use ^X or Esc as a prefix (typed before another key or sequence of keys). It is helpful to remember that ^G is the general-purpose “stop whatever I am doing” key. If you find yourself in a wierd state, hitting ^G once or twice is likely to get you out of trouble.

Your Emacs window is initially split into two parts. You can make one of them go away by typing ^X 1.

For practice with the editor, make a few changes to the file: add your name to what is there for the author, and change the part about it being an in-class example to “Lab 0”, and the date to today’s.2 Tip: emacs inserts what you type if it is a normal letter. If you want to delete the letter you are on, use ^D.

When you have made your changes, save the file. (I’ll let you figure out how.) Then go back to your terminal window. If you ls, you’ll see that there is now a file with a tilde on the end of its name: emacs makes a backup copy of a file when you edit it, which may save you sometime if you manage to make a mess.

Compiling and running a Java program For the computer to run the program, you must first translate it into a form intended for the computer; the command that does the translation is called a compiler. To compile your Java program, use

$ javac PrimeFactors.java

If you ls now, you should see a new file named PrimeFactors.class, which is the compiled (machine-oriented) version of your program. You can try to look at what is in that file with less, or you can open it in emacs. (To do so, you can type ^X ^F and fill in the file name, or click on “Open” and use pressing on the scroll-wheel to click on the filename you want.) However you look at the file, you will probably agree that it is not designed to be read by people.

You can run the program with the command

$ java PrimeFactors

Now use your editor to change the number to be factored, recompile, and run it again.

Note: The compiler javac works on files; so you specify a full filename for it. The java command works on the program, using whatever class files it finds; so the argument there is the name of the program (which is expected to match the first part of the name of the .java file).

That’s it for this week; there is nothing to hand in. Take a few minutes to review what you have learned today:

If you have time (it takes about thirty minutes), you might want to work your way through the tutorial built into Emacs. Once you have launched Emacs, you can start the tutorial by typing the three keystrokes ^H ? t. Or you might want to come back to the before the next lab meeting to do it. Whether you do the tutorial or not, it would be a good idea for you to keep a sheet of notes as you learn to use the editor.

Before you go, please exit Emacs and log out.

Cary Gray

1That is a strange name—intended as a joke. Back when terminals printed onto paper, there was a progame called more for displaying files; it got its name from the fact that it showed a page-full of text, then stopped with a line saying “More” at the bottom. Our program less is a successor to more.

2It would normally be bad form to change the author’s name to yours, because that would be claiming someone else’s work as your own. But today we’re making the change as an exercise in editing.