CSC15 Lab 1: Using Linux and Running Python
Due one week from date assigned
This lab is intended for you to:
- Learn basic Linux "survival skills"
- Learn how to enter and execute a couple of simple Python programs.
- Take a glimpse at the joys and pains of programming by modifying some
simple program lines.
Like most operating systems, Linux has a graphical interface and many
aspects of it are self-explanatory. But to study computer science in
depth you must become fluent with a very different way of
communicating with the operating system. In fact, most server-class
machines don't even have graphical interfaces. There are several
differences between Linux and PC or Mac operating systems that you need to
understand. For this class, you have the choice of using the pre-configured
virtual machine image (see blackboard), or log in to one of the Adams 204
workstations (you may also use your own installation of Linux, but only
if you are sufficiently advanced).
To log in on the virtual machine, use "csc15" as the username
and "betteryear" as the password. To log in to a Hofstra Linux
workstation, your username is in the form h70- (your hofstra id number
preceded by the letter h), with default password 70- (your hofstra id
number without the h). If you cannot log in yet, you can temporarily
use one of the shared student accounts (username csc001 or csc002 or
csc003... with password hofstracsc).
- Once you've logged in, explore the desktop environment. Try to
bring up the Firefox web browser. You can run most ordinary software from
within the virtual machine. However, applications like Zoom and video
playing should still be executed on your native operating system. Be familiar
with how to switch between the virtual and native machines (If you're using
full-screen mode, it's right-control+F, or command+F).
- Bring up a "terminal" window. This is the main interface you
will be using. The first step in becoming an advanced computer user or
professional is to learn how to use the computer without a graphical
interface.
- The command prompt will read something like "csc15@debianVM:~$" (make sure there's a $ at the end).
- If you are using a Hofstra workstation,you should change your password
(but don't change the password on the virtual machine or if you're using one of the public csc00x accounts!). type 'passwd' at the command prompt and follow instructions. Please
remember your password. At the end of every week students who have
forgotten their passwords will be publicly shamed.
Now let's learn some basic survival skills...
- Type "pwd" - this command shows you what directory you're currently
in. Directory names in Linux look like /home/username/subfolder/subsubfolder/... The directory (or folder) / is called the root directory. The /home/ directory contains folders for all users. When you log in, you are automatically placed in your own user directory. This is where you will organize all your class materials. Note that the slash "/" is in the opposite directory as the MS Windows directory slash "\".
- Type "ls" - this command shows the contents of the current
directory.
don't be surprised if there's nothing there - that's why we're here!
You can also use "dir" instead of "ls".
- Type "ls -a" - As with Windows, there are some hidden systems files
that are usually not visible. This command will display all files and
directories in the current directory.
- Type "ls -l" - This is the same as ls but gives you additional information
for each file and folder.
-
Basic Command Summary
- cd (directory name): change directory to named directory
cd .. : go to parent directory
cd /(path) : absolute directories begin with "/", the root
directory.
For example, if you are in directory /shared/csc/local,
typing "cd .." will take you to the /shared/csc directory.
- Typing "cd" by itself will take you to your home directory.
- pwd : tells you the current directory you're in.
- cp file1 file2 : copies the contents of file1 into a file called file2.
- ls : lists directory contents
- mkdir dirname : creates a new directory called dirname in the current directory
- mv file1 file2 : renames file1 into file2
- rm file1 : deletes file1
- rm -rf dirname : deletes the named directory and all its contents. Use with care!
- more filename : a quick way to view the contents of a text file.
- passwd : change password
Many of these operations can also be performed using the mouse in the
"file manager" window. However, you'll need to learn these commands if
you want to use the workstations remotely.
Let's now practice these commands (please do not deviate from instructions)
- Make sure you're in your home directory (type "cd")
- Type "mkdir labs15" - what does this do? You should creat a directory
for this class because you'll be using your account for other classes as well.
NOTE: if you're using one of the public accounts (csc00x), create a directory with your username instead, so you can keep your files separate.
- Type "cd labs15" - you're now in the directory you just created.
- Type "cd .." - you've moved up one directory level (to your home directory).
Please note: The "cd .." command does NOT always return you to the last directory you where in: it always moves you up one level in the directory hierarchy.
- Now use a command to go back into your csc15 directory
- Using Python in interactive mode.
Type "python3" in the virtual machine (or "python3.4" on an Adams 204
workstation).
Make sure
that you're using Python version 3, and not Python 2: these versions
of Python are not compatible. The interactive mode allows you to
quickly experiment with the range of valid syntax in Python.
Yuu should see ">>>" as the python prompt: this prompt is different
from the $ Linux operating system "shell" prompt: the ">>>" prompt understands Python, not Linux commands (don't use cd, ls, etc. here)
When you're done, type exit() or Control-D to exit the Python
interpreter.
AGAIN: while you're inside the python interpreter, you'll
see a prompt like ">>>". This is NOT where you can enter linux commands
like ls, cd, etc ... To do that, you need to be at the "shell" prompt
(with $).
- Time to write a program.
To do that you need to learn how to use an editor. A very powerful
(though not easy for beginners) editor used by programmers is "emacs"
- type "emacs &" - you'll see a window come up. The window may
be split into two halfs: click on the top half and type Cntrl-x 1 (control-x, then 1).
You can also tell emacs to avoid splitting the screen by starting it with "emacs --no-splash &". You should recognize a toolbar with
familiar buttons such as "file", "edit" and "help". Select "file" and
open a file called "firstp3.py". As a shortcut, you can also type
"emacs firstp3.py --no-splash &" to open the file in one step.
- Type in the first program provided on the first attached sheet.
You do not have to type in all the comments marked with "#", but
put your name in comments at the top. Comments are lines that begin
with a "#".
- save it under the name "firstprog.py" in your csc15 directory.
- Make sure you're in the
csc15 directory. Execute the program (from the $ prompt) with "python3 firstprog.py"
Did you get any errors? If you did,
that means you had a typo while copying the program, so go back and
edit the program, save it, and try again.
- You're encouraged to modify the program experimentally, such as
changing the number of times your name is printed at the end.
- Also type in the second program (secondprog.py). For this program,
first get the part before it gets "more weird" to work. For those
of you with some programming experience, you can take on the challenges.
- Quit emacs (click the "file" menu option).
- You can also start emacs and open a file at the same time by typing
emacs (filename) &.
The "&" is important: without it, you'll lose the ability to use the
terminal window while emacs is running.
- Some additional emacs commands. You'd be surprised how much easier it
is to use keyboard commands instead of the mouse sometimes. Here are a few
you can use in emacs (Cntrl-xs means hold the control key and type xs):
- Cntrl-xs: save file
- Cntrl-xf: find/open file
- Cntrl-xb: switch between buffers
- Cntrl-x2: splits window (Cntrl-x1 makes it a single window again)
- Cntrl-g: reset (do this when something goes wrong)
- Cntrl-p: move cursor up
- Cntrl-n: move cursor down
- Cntrl-k: erase line of text, saves in buffer
- Cntrl-y: pastes text saved in buffer:
- Esc-x, then type 'goto-l' at the bottom to jump to a line number.
- Cntrl-xc: quit emacs
The editor you use to write the source code of programs isn't so
important, but you should be using one that's suitable for
programming. An alternative to emacs is gedit (type gedit filename to edit or
create a file). If you use this graphical interface, however, you
should go to its Preferences menu and select "display line numbers"
and "highlight matching braces". There are also non-graphical editors
including vim, nano and "emacs -nw". These are preferred by the real geeks.
Some additional Linux (not Python or emacs) commands for program management. You may wish to terminate or suspend a running python program. Usually Cntrl-C (from the terminal
window) would work. If that doesn't work, tryp Cntrl-Z. This command will
suspend the running process, but does not kill it (it becomes a zombie).
The command "jobs" will list the current running or suspended jobs. Each job
will have a number in [] brackets. To kill the job permanently, use the command kill -9 %n, where n is the number of the job.
For further self-learning, start reading the online textbook "How to
Think Like a Computer Scientist", as well as go to the "python
documentation" link on blackboard, and try to follow some of the
examples in the online tutorial. You're encouraged to experiment.
You are to turn in the source code of the program you typed in and
modified. Include your name in #comments. Submit to
hofstra.blackboard.com
Final Note: you are expected to remember what you learned today,
for these skills are required throughout the semester. Practice these
skills further on your own time. An interactive quiz on basic linux commands will
be given shortly.