Mcs 260, Spring 2008, Lowman, Quiz 1: First-MAC-Lab
=====================================================
Part I:
=======
Use your icarus account from any computer connected to the internet. You can
do this from Greece, the South Pole of even from home.

Note, lines beginning with ">" indicate that you should enter the given
command at the command prompt >.
0. Logon to a computer in the MAC-lab. Click the icon on the desktop to mount
 your Webdisk. You can use this to save any files that you created on the lab
  computer. Three useful ways to save your files from the lab computer are to
  (a) save your files to your mounted Webdisk, (b) ftp (use sftp) your files to
  your icarus account and (c) save your files to a flashdrive. Once you logoff
  the lab computer all files on it will be lost.

1. Open terminal window (click black icon) and connect to your icarus account.
Both icarus and Macs use a unix operating system.
    > ssh icarus.uic.edu    ---use the same netid and password you used to log
    into the mac you are now using.
    > pwd   ---unix command gives present working directory
    > ls    ---lists files (ls -l includes file details)
    > TERM=xterm   ---this changes the terminal type so you can use pine and pico

On your icarus account open a bash shell. This will allow you to use the up/down
arrows to recall commands you entered earlier.
    >bash   ---later you will need to exit the shell before you exit icarus
    >ls -l  ---note: lines beginning with "d" indicate a directory and "-" a file.

2. Make a directory mcs260 , cd into it , make a directory named firstlab and cd
into it.
    >mkdir mcs260
    >cd mcs260
    >mkdir firstlab; cd firstlab  ---on unix you can enter more than one command
    on a line if seperated by a ";"

3. Type the python code to test the gcd algorithm that we developed in the first
lecture. Use the pico (or vim or emacs) text editor to create the following file
named gcd.py

    > pico gcd.py

Now type the following python code between the ==== lines, save it and exit pico
===================================================
# file: gcd.py
# your name, mcs260, Spring 2008
# an example of using python to implement the gcd algorithm

def gcd(m,n):
    while n != 0 :
        r = m % n
        m = n
        n = r
    return m

def main():
    print "find gcd(m,n)"
    m = input("enter m: ")
    n = input("enter n: ")

    print "gcd(",m,n,") = ", gcd(m,n)

main()

====================================================
Now run the program at the command prompt. If you get any error messages use
pico to fix your typing errors and try again.

    > python gcd.py

4. Practice submitting your python program by sending it to yourself by using
the pine email program and including gcd.py as an attached file. Open the
received email, save the attached python program to a different directory (
use ~/tmp), cd into ~/tmp and run the program that you just sent to yourself.

    > cd ~    ---on unix "cd ~" or just "cd" takes you back to your home directory.
    "cd -" takes you back to the directory that you just left (very useful). "~"
    can always be used as the path to your home directory.

    > mkdir tmp
    > pine
After starting the pine email program select COMPOSE to create and send an email
to yourself with the file gcd.py attached. Try to figure out how to do this on
your own. If you get stuck ask for help.
Now exit pine and then start pine again. You have mail! Check your mail, save
the attached file to ~/tmp directory, quit pine, cd into ~/tmp and run the python
program that you just received. Try to do this on your own but ask for help if you
need it. Later, i.e. not now, try doing this with the email program that you
usually use.

If you want to remove the temporary file and directory then:
    > cd ~/tmp
    > pwd; ls    ---just to make sure you are in the right directory and the
    file is there.
    > rm gcd.py
    > ls     ----to make sure it is deleted
    > cd ..   ---go back to the parent directory
    > ls      ---just to make sure (always a good thing when deleting files and
    directories)
    > rmdir tmp   --- you might not want to delete ~/tmp so you can use it again

Now exit icarus.
    > exit    ---to exit the bash shell you started earlier
    > exit    ---to exit icarus and return to the lab operating system

Part II:
=======
5. It is often more convenient to work on the Mac in the lab and save your work
to either your icarus account or your mounted webdisk before logging off. In your
 next session you can copy the files saved on icarus or webdisk back to the Mac
 and continue working on your projects. After logging on in the Mac-lab and
 mounting your Webdisk open a terminal window:
    > cd; pwd    ---to make sure you are in your home directory and see its path
    > mkdir mcs260lab
    > cd mcs260lab
    > mkdir firstlab
    > cd firstlab
    > ls  --- for fun
Use a text editor that is available on the mac and create the source file gcd.py.
vim is a nice editor but you first need to learn how to use it. You can use Word but
make sure you save your program as a text file and not a Word file. You might
check if emacs is available. After you create your python program(s) you are ready
to save the directory mcs260lab and everything in it to a single file. Either
create a tarball somename.tar or a zipped tarball named  somename.tar.gz. The
advantage of .tar is that you can open the file in a text editor and see what is
in the file. The gzipped version is compressed and is a smaller file. As the
directory grows this might become important.

6. To tar the directory mcs260lab:
    > cd ~   ---go home
    > ls     ---make sure you see the directory mcs260lab
    > tar cvzf mcs260lab_backup_15jan08.tar.gz    mcs260lab
    > ls -l    --- you should see your new zipped tarball

You can now save your tarball on icarus, your mounted Webdisk, a flashdrive (plug
it in) or ftp it to your home computer. We will ftp (file transfer protocol) it
to icarus. We will use sftp (a secure version of ftp) instead of ftp:
    > sftp icarus.uic.edu     ----or use yournetid@icarus.uic.edu
    > ls    --- shows files on icarus
    > cd to the directory you want to save your file to or just save it to your
    home dir.
    > put mcs260lab_backup_15jan08.tar.gz
    > ls    ---just to check
    > bye   ---command to exit ftp and sftp
    You are now back to the lab computer.
7. To see how this works logoff the lab computer you are using then logon again
and mount your Webdisk (good habit but not using it now). Open a termial window
and :
    > cd ~  --- make sure you are home
    > ls    ---the directory mcs260lab is probably missing. It was deleted when
    you logged off. Not to worry!

Do not recreate the directory. Instead, use sftp to copy your tarred directory
back to the mac and recreate the directory with everything in it by first
unzipping it and then extracting the files from the tar archive:
    > sftp icarus.uic.edu
    > ls   ---see files on icarus
    > cd into the directory where you saved your tarball
    > ls   --- make sure file is there
    > get mcs260lab_backup_15jan08.tar.gz
    > bye   --- exit sftp
You are now back to the lab terminal:
    > ls -l    ---the .tar.gz file should be there
    > tar xvzf mcs260lab_backup_15jan08.tar.gz
    > ls    ---the directory mcs260lab should now exist
    > cd mcs260lab
    > ls -l
You can now pick up where you left off. Don't forget to save your new work. All
will be deleted when you logoff the lab computer.

Part III:
========
8. Try saving your .tar.gz file to your mounted Webdisk (it must be mounted ).
You can do this from the desktop or you can use a command window. Copy files
to /Volumes/your_netid. This should make sense if you cd into /Volumes and list
 files.
9. Investigate to find out what software is available in the lab and what is
useful to you. Also, you can save files to a flashdrive if you have one.

10. An alternate way to work with python in the Mac-lab is to find the applications
 menu and click MAC_Python. Now start the python ide IDLE. See you text for basic
 instructions on how to create and run python program. Try it.