Lab01, CISC181 honors, Spring 2006

Goals for Lab 01

In this lab, you will accomplish several things:

  1. You'll learn how to set up your default shell and your default group on the http://www.udel.edu/network page.
  2. You'll learn how to set up a file that will help you get the right versions of the compilers in this class, a file called .localenv, and how to test whether it is working or not.
  3. You'll review subdirectories and file management in Unix. (Chapters 6, 7 and 8 in Anderson will be helpful here, so you may want to read over those before coming to lab). In particular, under your home directory, you will learn how to create the following subdirectories:

    ~/cisc181
    ~/cisc181/lab01

    You'll also review how to put yourself in your ~/cisc181/lab01 subdirectory before creating any files for this lab.

    This way, you can keep your directory nice and tidy: everything for cisc181 is all in one subdirectory (folder) called cisc181, and under that subdirectory (folder), there will be a folder for each lab (one for one for lab01, one for lab02, etc.)
  4. You'll further test whether what you did in step 2 is working by using the C++ compiler to turn a program written in C++ into an executable file in machine language. Your goal will be to show that you can access two different C++ compilers, one called "CC" and another called "g++". However, it is only necessary to show that you can access one of these to get full credit for the lab.
  5. You'll learn how to change directory into your ~/cisc181/lab01 directory, copy a C++ program from Prof. Conrad's web site, compile it, and run it. The program you copy will be one that converts Fahrenheit to Celsius. You'll then use emacs (or vi) to change that program into a C++ program that converts Celsius to Fahrenheit. You'll script the results, and submit them to your TA via WebCT and on paper. (If you aren't sure how to use WebCT and how to print, take a look at lab00.)

    All the files you create in this step will be stored in your newly created ~/cisc181/lab01 directory.

Goal 1: Setting up your default shell and default group.

On the SunRays, bring up a web browser. (If you are not sure how to do that, consult the instructions for lab00.)

Now, access the web page: http://www.udel.edu/network. On that page, type in your UDelNet username and password.

You should see a screen that looks something like the following.

UD network page

Click where it says "Change your Unix Default Group". That should take you to the following screen:

change default group

On the example screen above, you will see one of the items on this page is listed as CISC181010-18, which means CISC181, sections 010 through 018. What you should on your screen is the following:

2081 Cisc181

That is the group you should choose if you are in Prof. Conrad's sections for Spring 2006. (If you are in a different class that is using Prof. Conrad's lab files, your instructor will guide you as to which group to choose.) The dollar amount listed might be something like $300 (in my case, it is $54,000, which is the total for everyone in the course.) We'll talk about the "money" in a minute; for now, just know this: it's not real money. It's just pretend money used to track computer usage. You wont get a bill for it. Ever.

In the drop down box shown, select "2081" and hit submit. You are now done with this step, which you should only have to do once per semester.

Let's come back to that $54,000. Because this example is from the instructor account, the "dollar amount" is huge. Your dollar amount as a student will be much smaller, but that's ok. The dollar amount will go down throughout the semester as you use strauss. It should not get below $50. If it does, contact your instructor immediately: he/she can request more "dollars" for your account. You can track your dollar amounts remaining by typing "chdgrp" when you are logged into strauss.

Now we need to change your default login shell. Click where it says "return to the main page". Then click on the option for "Change your Unix Login Shell". That should take you to the following screen:

change login shell to tcsh

If your current Unix Default shell is csh, you should change it to tcsh by selecting /bin/tcsh from the drop down box, and clicking submit. You can then logout of the UDel Network page. This gives you command line editing (the ability to recall commands with the up and down arrows), as well as nifty things like file name completion (all of which your instructor will demonstrate in lecture.)

If your current Unix Default shell is tcsh, you don't need to do anything; you can log out of the UDel network page.

If your current Unix Default shell is bash, then you are probably already a confirmed "bash" user. The following paragraph is for you. Everyone else can ignore it.

Prof. Conrad writes: is is my intention to eventually migrate this course to "bash" rather that "tcsh". I acknowledge that "bash" is a better shell to teach. We'll eventually cover some "bash" related items in this class. That being said, for the first part of the Fall 2005 semester, you might be better off using tcsh. The reason has to do with the "path" variable you need to get to the compilers (this is new as of January 2005.) They updated the compilers during January 2005. We've tested the commands to access them from tcsh, but not yet from bash. So consider switching your default login shell to tcsh, or "typing in" tcsh after you login to strauss each time. The remainder of this lab, and all further labs (until future notice) will assume tcsh. Until I figure out the new environment as it relates to bash, if you use bash (or any other shell other than tcsh), and your commands don't work, you are on your own to fix it.

If your current Unix Default shell is anything else, then read the paragraph above about bash, because it applies to you as well. Please considering switching to tcsh for the time being.

There is nothing to hand in for this step; your TA can check that you've switched your default login shell by using a script that checks this directly (via the finger command.) We'll also include some steps in a script later on to verify that you completed this step successfully.

Goal 2: Setting up your .localenv file (to manage your path).

Open a terminal window on strauss. In that windows, type the following:

> echo $PATH 

You should see output such as the following. Yours wont' match the following exactly, but it will be in the same form:

> echo $PATH
/opt/sfw/bin:/opt/tex/bin:/opt/texutils/bin:/opt/bin:/opt/SUNWspro/bin:/usr/open
win/bin:/usr/bin:/usr/ccs/bin:/usr/ucb:/opt/X11R5/bin
> 

The command echo $PATH tells the shell to print the value of a so-called environment variable called PATH. This variable contains a "colon-separated" list of directory locations where Unix will look for executable programs when you type in a command. (Windows has a similar concept, and I imagine Mac OS X probably does as well.)

It is important for your path to contain the right list of directories so that you can access the compilers we will use in this course. The locations of the compilers change periodically as the compilers are upgraded to newer versions (to fix bugs, and to incorporate new features into the C and C++ languages). If your account was set up a while back, it may not have an up-to-date path.

There are several files that you can customize in your directory in order to modify your path. These files are typically stored in your home directory on unix (the one you are in when you first login, and the one you return to if you type "cd" at the Unix command prompt.) These files have names that start with a period, which is how Unix implements the concept of "hidden files"; files that start with a period (e.g. .cshrc, .bashrc, .localenv, .emacs) do not typically show up in directory listings unless you specifically request to show hidden files.)

To list the files in your directory including hidden files, use the following command. Note that there is a space between ls and -al:

> ls -al
-rw-------   1 pconrad  1173        3391 Oct 19 09:42 %backup%~
drwx--x--x  36 pconrad  4000        8192 Feb  8 20:51 .
drwxr-xr-x 226 root     other       8192 Feb  8 04:08 ..
-rw-------   1 pconrad  4000        2090 Dec  7 10:39 .ICEauthority
-rw-------   1 pconrad  0376        2401 Feb  7 16:40 .TTauthority
drwx------   2 pconrad  2265        4096 Mar 11  2004 .Trash
-rw-------   1 pconrad  0376       13100 Feb  7 18:05 .Xauthority
-rw-------   1 pconrad  4000         814 Jun 11  1996 .ab_library
-rw-------   1 pconrad  1173        1894 Nov  9 15:21 .acrorc
-rw-------   1 pconrad  1173         237 Nov  9 15:21 .acrosrch
-rw-------   1 pconrad  4000         303 Feb 12  1998 .addressbook
-rw-------   1 pconrad  4000        2399 Feb 12  1998 .addressbook.lu
-rw-------   1 pconrad  4000        3071 Sep 20  1995 .article
etc....

The files we are most interested is the file .localenv. Type the command:

> emacs .localenv

or

> vi .localenv

 

You are going to add the following lines to your .localenv file. Be sure to type them carefully; if you are off by even one character, this will not work:

if -d /opt/sfw/bin then
  setenv NEWPATH /opt/sfw/bin:$NEWPATH
  setenv MANPATH /opt/sfw/man:$MANPATH
  setenv LD_LIBRARY_PATH /opt/sfw/lib:$LD_LIBRARY_PATH
endif


if -d /opt/SUNWspro then
  setenv NEWPATH /opt/SUNWspro/bin:$NEWPATH
  setenv MANPATH /opt/SUNWspro/man:$MANPATH
  setenv LD_LIBRARY_PATH /opt/SUNWspro/lib:$LD_LIBRARY_PATH
endif

These lines are written in the "C shell command language", and the .localenv file is what is known as a startup script for your shell.

The first line, if -d /opt/sfw/bin then tests whether the directory /opt/sfw/bin exists or not; this directory contains files for the GNU C and C++ compilers known as gcc (for C programming) and g++ (for C++ programming.) The GNU compilers are "open source" compilers, written by volunteer programmers. If /opt/sfw/bin exists, three setenv commands are used to set the values of three environment variables called NEWPATH, MANPATH and LD_LIBRARY_PATH. These three variables control the path for executables, manual pages, and load libraries respectively (you'll learn more about all three of these over the coming weeks.)

The next few lines do exactly the same thing but for the directory /opt/SUNWspro which contains the latest version of Sun's C and C++ compilers. These compilers are produced commercially by Sun Microsystems, the manufacturer of the strauss computer system. Until recently, these were closed source, proprietary compilers (they cost money). I don't know if that is still true or not; that may have changed. In any case, the development history of these two compilers is quite different. Although correctly written programs should produce the same results from both compilers, incorrectly written programs can produce very different error messages or error behavior. Comparing the results from both compilers can be very helpful in debugging, so it is important to be able to use both.

Once you have made the changes in the .localenv file, save the file and logout, then log back in.

You should then be able to do the following commands, and get exactly the results shown. These results indicate that you are able to use the correct versions of both the GNU and Sun compilers for both C and C++. If your output does not match that in the box below, review the .localenv file and try again, and/or ask your TA or instructor for assistance before proceeding.

> which gcc
/opt/sfw/bin/gcc
> which g++
/opt/sfw/bin/g++
> which cc
/opt/SUNWspro/bin/cc
> which CC
/opt/SUNWspro/bin/CC
> 

The which command tells you the full path of the file that gets executed if you type in a particular command. For example, in the script above, you see that if you type in gcc, the actual file that gets executed is /opt/sfw/bin/cc. You'll include the commands above in a script later in this lab; for now, once you are successful in getting the output above, you are done with this step.

Goal 3: Managing files and directories

  1. Just as we organize documents in a "real world" filing cabinet into folders, when we create files on a computer disk, we organize them into files and folders. You may be familiar with the idea of "files" and "folders" from using Windows or Macintosh. On Unix, folders are called "directories". Chapters 6, 7, and 8 of your Anderson text ("Just Enough Unix") contain details about files and directories under Unix, and I recommend that you look over those chapters soon. You may find them to be a useful reference for this step.


  2. On the unix command line, type "cd" by itself. "cd" stands for "change directory". If you type "cd" by itself, this command will always return you to what is called your "home directory". When you type in the command, there will not be any output; it will appear that "nothing happened". But if you now type "pwd" on the command line (which stands for "print working directory"), you will see the name of your home directory. On the composers (strauss, copland, etc.) the name of your home directory will be some series of letters and numbers, such as "/home/usra/d9/55560".


  3. Now type "ls" to list the files in your home directory. Notice what files are listed.


  4. Next, we will create a directory for your cisc181 files. Type in

    mkdir cisc181

    This will create a directory called cisc181, in which you can store your files for this course. If you type "ls" immediately after doing this, you will see the new directory listed among your files. Since this directory is "under" the home directory, we call it a "subdirectory" of your home directory.



  5. To move into this subdirectory, type "cd cisc181". If you then type "pwd", you will see that you have moved into the cisc181 subdirectory. If you now type "ls", you will see no files at all. This is because when you create a subdirectory, it is initally empty.

  6. Now type "cd" again, and then "pwd", and "ls". You will see that you have moved back up to your home directory. Then, type "cd cisc181" to again move down into your new subdirectory, and type "pwd" and "ls". You should see the same files you saw back at step 3 above, except now you see the cisc181 subdirectory as well. (In Unix, a subdirectory is just a special kind of file).


  7. Now, type the following command:

    mkdir lab00
    mkdir lab01

    These two commands will create two new subdirectories under the cisc181 subdirectory. We call these subdirectories "cisc181/lab00" and "cisc181/lab01". These subdirectories are "under" cisc181, which is in turn, is "under" your home directory.

    To show more specifically that it is under your home directory, we can use the symbol ~ to stand for your home directory. So, your new directory would be referred to as ~/cisc181/lab00. (The ~ is sometimes called "squiggle", though the proper name for it is "tilde", pronounced"till-duh")


  8. You can use the tilde in cd commands. "cd ~" will change to your home directory (though in this case the ~ is redundant, since "cd" all by itself does the same thing.)

    The command "cd cisc181" changes to the cisc181 directory under the "current" working directory, however cd "~/cisc181" will change to cisc181 under your home directory (regardless of what the current working directory happens to be).


  9. (Skip this step if you did not do lab00). During lab00, you may have created a files called lab00.dat.. You probably created this file in your top level home directory. We are now going to move this file into your newly created ~/cisc181/lab00 directory.

    Type "cd" all by itself, to change to the directory where you created this file (probably your home directory). Then, to move this file into the directory ~/cisc181/lab00, you can use the following command ("mv" stands for "move"):

    mv lab00.dat ~/cisc181/lab00

  10. A special directory is the one called ~/public_html. Any and all files that you put into this directory become available on your web page. (If your user id is foobar, your web page is http://udel.edu/~foobar).

    To see if you have this directory, change your working directory to your home directory (use either "cd" or "cd ~") and then use "ls" to see if the directory "public_html" is listed. If you don't have this directory already, use "mkdir ~/public_html" to create it.

  11. Use "cd ~/public_html" to move into this directory. Type the command "pwd" to ensure that you are "inside" "~/public_html".

    Then, do another "mkdir cisc181" command, to create the directory "~/public_html/cisc181". This directory will store your "CISC181 web page". In a later step this semester, you will create a personal web page (unless you already have one), and a web page specifically for this course.

    The files for your personal web page goes into the directory "~/public_html", and will be accessed via the URL http://udel.edu/~userid (where userid is your UDelNet ID).

    The "CISC181 web page" goes into the folder "~/public_html/cisc181", and will be accessed with the URL http://udel.edu/~userid/cisc181. You can use this same technique to create as many web pages as you like under your main web page, just by creating new subdirectories to store the content. We'll add content (HTML files) to the web page, and do the commands to make it available (the "chmod" commands) in a later step.

Goal 4: Compiling C++ programs on Strauss

In this step, you will enter the traditional "first C++ program" just to make sure that you understand how to create a C++ program, compile it, run it, and script it.

  1. Now, change directory into ~/cisc181/lab01 with the command:

    cd ~/cisc181/lab01

    For the next couple of labs, the instructions will remind you to create a new subdirectory for that lab, and change directory into it at the start of the lab. For example, next week, we'll do "mkdir ~/cisc181/lab02" to create the directory, and "cd ~/cisc181/lab02" to change directory into it before we start work. However, eventually, you'll just be expected to know to do that on your own.
  2. Create a file lab01a.cc that contains the following (except substitute your name and Unix user id for those of Jane Doe, and use today's date.)

    DO NOT include your student number (NEVER include that number in anything you submit in this class!)
    // lab01a.cc Jane Doe doej@udel.edu 09/01/04
    // traditional first program (Anderson, p. 383)
    // modified by P. Conrad
    #include <iostream>
    using namespace std;
    int main(void)
    {
    cout << "Hello, world!" << endl; return 0;
    }
  3. Once you've created this program, use the following command to compile it (compile means: translate from C++ into machine language, or it cannot be translated because of errors, report the errors) with one of the following commands. At this point in the semester, you may use either one (later on, I may tell you to use one or the other.) Note that the first command is CAPITAL CC, not lowercase cc.
    CC lab01a.cc

    OR

    g++ lab01a.cc
    

    The first command (CC) is a commercial compiler, supplied by Sun Microsystems. The second command, g++ is an open source compiler, supplied by the GNU project, which is staffed by volunteer programmers. From time to time, we may discover differences, but for now, I don't care which one you use. Sometimes if you have a tricky syntax error it is useful to try both, because sometimes one gives you more useful error messages than the other.

  4. In both cases, a new file is produced called "a.out". To execute this file (run your program, type the following:
    ./a.out

    You might be able to get away with just typing a.out without the leading ./, or you might not; it all depends on how your account is set up. (It has to do with the Unix concept of the "path"; you'll read about that in the Anderson text later on.)

    If all goes well, you should see something like the following:

    > CC lab01a.cc
    > ./a.out Hello, world! >
  5. You are now ready to use the "script" command to make a record of your work.

    Note that you must be careful when using the script command. The script command will wipe out your work if you are not careful!

    The thing to remember is: on the command line, type "script" followed by the name of a .txt file; for example:

    script lab01a.txt
    This is correct!!!!

    Never put "script" followed by the name of a .cc file. It will wipe out your .cc file!

    script lab01a.cc
    WRONG!!! WRONG !!!!
    WRONG!!!!

    Ok, now that we have that out of the way...

    Type "script lab01a.txt". Then go through the following steps:

  6. Now, use "more lab01a.txt" to make sure your script looks ok, and use the following command to print your .txt file. You'll be submitting this to your TA when you hand in this lab:
    qpr -q whlps lab01a.txt 


And finally, you are done with Goal 4! We'll return the file lab01a.txt and lab01a.cc files in the final step of this lab, where you'll submit those to WebCT along with the files you create in Goal 5:

Goal 5: C++ programming on Strauss

 

  1. If you are not already there, change directory into ~/cisc181/lab01 with the command:

    cd ~/cisc181/lab01


  2. Do a "pwd" command to be sure that you are in the ~/cisc181/lab01 subdirectory. Then, use the following command to copy a program from Prof. Conrad's directory into your current working directory. BEFORE YOU TYPE THIS COMMAND, look over it carefully. You will notice that there is a space between the "cp" and the "~". There is a space between the "lab01b.cc" and a period (.) which is at the end of the command. Those spaces are very important, and the period at the end is especially important. Be sure you type the command exactly as it appears here.

    cp /www/htdocs/CIS/181h/pconrad/06S/work/labs/lab01/lab01b.cc .

    The "cp" command in Unix is used to copy a file. The file is copied from a directory under Prof. Conrad's home directory. Note that ~ by itself refers to "your home directory", but "~pconrad" refers to "pconrad's home directory". Similar, you can refer to any user's home directory on strauss if you know that user's login name; e.g. jsample's home directory can be accessed via "ls ~jsample".

    The period by itself at the end is the "target" of the copy command, i.e. the place we are copying the file "to". A period, all by itself, refers to the "current working directory", the same one that comes up when you type "pwd" (print working directory). So, since the current working directory is ~/cisc181/lab01, we could also have done the same copy command using the following:

    cp /www/htdocs/CIS/181h/pconrad/06S/work/labs/lab01/lab01b.cc ~/cisc181/lab01

    but the command was already pretty long, so using the "." is a nice shortcut.


  3. Now, do an "ls" command, and you should see the lab01b.cc file in your directory. Your next step is to list the contents of the file, then compile the program, and run the program. Before you look at the list of commands below (which do those three steps), see if you can figure out what you would type to accomplish this. Then scroll down and see if you were right.

    scroll down for the answer

    keep scrolling down for the answer

    still further

    almost there

    ok here's the answer:

    cat lab01b.cc
    g++ lab01b.cc
    ./a.out

    Did you get it right? Note: you could also have done "more lab01b.cc" in the first step (if the file is too large to fit on the screen all at once), and "CC lab01b.cc" for the second step.

    An aside about the "more" program: note that you should never use "more" inside a script file; only use "cat" inside a script file. The "more" program is only for when you are looking at the file in "real time", not for scripts that you are going to print or submit electronically.


  4. Run the program a few times with different values, and look it over to see how it works.


  5. Now, the actual thinking work starts (though admittedly, this is still pretty darn easy.) This program, implements an algorithm to convert from Fahrenheit to Celsius (subtract 32, divide by 9, multiply by 5.) Your job is to edit the program (e.g. using "emacs lab01b.cc" or "vi lab01b.cc") so that it converts, instead, from Celsius to Fahrenheit.

    You will need to modify (at least) the following:


  6. Once you have finished editing, try compiling and running again. Test your program with several different values to be sure the conversion is working properly.
  7. Now you are ready to script your lab01b.cc program. We'll d that in the "finishing up" step.

Finishing up: What to turn in for this lab

  1. Change to the ~/cisc181/lab01 subdirectory. Make a script called lab01b.txt that contains all of the following steps. Note that we are showing that your program compiles with both CC and g++. Typically, it is only necessary to compile with one or the other.


    echo $PATH
    which g++
    which CC
    cat ~/.localenv

    cd ~/cisc181/lab01
    pwd
    ls
    cat lab01b.cc
    CC lab01b.cc
    g++ lab01b.cc

    ./a.out 
       repeat this step with several different inputs
    exit  to end the script


  2. Once you have created this script, print it out with:

    qpr -q whlps lab01b.txt

    (Note that this prints out the file in the lab, Willard 009. You can type this command from anywhere, including from home, but you have to go to Willard 009 to pick up your printout. You can also change whlps to smips to print in the basement of Smith Hall, or prsps to print at the Pearson Hall computing site. For the final version that you submit to the TA, you should always print in one of these places, not on your home printer.)
  3. Then, upload your lab01b.txt file and your lab01b.cc to WebCT. You should now have four files uploaded: lab01a.txt, lab01a.cc, lab01b.txt, and lab01b.cc. Now you can hit submit.

Grading:

Next Steps:

Move immediately on to lab02.html


Phillip T Conrad

Valid XHTML 1.1 Valid CSS!