Lab 1 : Getting Started

Goals for this lab
  • Learn what these labs will be like
  • Learn how to log in to a remote Linux computer
  • Learn how to use a Linux command shell
  • Learn how to manipulate a Linux filesystem
  • Use a text editor to create a file
  • Write your first Perl program!

 

Summary

Most (if not all) of the work we do this semester will be in a Linux command environment. While Linux has a graphical user interface much like Windows does (where you can use a mouse to click on buttons and move windows around), we'll be dealing mostly with its command line interface. The idea of a command line interface is simple - we type commands, as text, and Linux evaluates them (perhaps by running a program) and tells us the results. However, as we'll see, the command line is very powerful.

In the lab instructions, you'll see several recurring visuals:

  • In later labs, you may see Perl code blocks that look like this:
    #!/usr/bin/perl -w
    # this simple program greets the world
    
    print "Hello World!\n";
    

    When you see these, take notice! You may have to copy or adapt the code, or at least understand how it works.

  • Additionally, you'll see Action Blocks:
    Example 1:
    This is an example. Often you will be asked in these blocks to answer a question or something. Follow these directions carefully, the contents of these items will often be important to your grade!

  • Sometimes, you'll see Command-Line blocks:
    jpr@sulu (~) > mkdir blah
    jpr@sulu (~) > cd blah
    jpr@sulu (~/blah) > ls
    jpr@sulu (~/blah) >

  • These represent things you should type at the command line, or examples of command line techniques. The items after the > are the things you type; what comes before the represents the prompt. Yours may look different.

Hopefully, these common elements will help you complete the labs quickly, and understand them well.

 

Answering Questions
Some labs will involve creating and running a Perl program to solve a problem, but many also involve answering some written questions. We'll mix these in with the lab in convenient spots. Write down the answers using the editor of your choice (such as Nano, which we'll talk about soon). When you are done with the lab, you'll be asked to submit these answers along with any code you write. Make sure your name is in any file - text or code - that you create. Here's an example of what your answers should look like:

 Buck Rogers
 Lab 1
 April 1, 2009
 -------------

 1. Answer to first question goes here

 2. Answer to second question ...
      

This file should be called lab1.txt.

 

Using Linux

Okay, enough talk. Time to get started using Linux!

Do This:
  • Open SSH (Start -> Programs -> Communications -> SSH Client)
  • more instructions go here

You're now connected to sulu.informatics.indiana.edu, a Linux server deep in the Informatics building. The text you see on screen is the command prompt, which indicates sulu is waiting for instructions from you. You've learned about the command prompt and some simple commands this week, so let's try some out. We'll start by creating some directories to keep your labs and assignments organized.

Do This:
Type the commands in the following command line block. The output you should see is also shown.
jpr@sulu (~) > mkdir i211
jpr@sulu (~) > cd i211
jpr@sulu (~/i211) > mkdir lab1
jpr@sulu (~/i211) > cd
jpr@sulu (~) > ls -R
i211/

./i211:
lab1/

./i211/lab1:
jpr@sulu (~) >
    


Now your account is set up so you can keep your work this semester organized. Let's practice with some advanced shell commands.
jpr@sulu (~) ls -R > listing
jpr@sulu (~) >
    

The > is a redirection operator; it takes the output of any command and puts it in a file instead of printing it on the screen. < lets a command take its input from a file, and | (pronounced "pipe") lets a command send its output (or take its input) from another command.

Take a look at the file you just created...

jpr@sulu (~) > cat listing
i211/

./i211:
lab1/

./i211/lab1:
jpr@sulu (~) >

This file is in the wrong place; let's move it to the i211/lab1 directory. To do this, use the mv command...
jpr@sulu (~) > mv listing i211/lab1
jpr@sulu (~) > ls -l i211/lab1
total 8
-rw-------   1 jpr  jpr  40 Aug 29 18:25 listing
jpr@sulu (~) >


Exercise 1:
Using a Linux text editor such as nano, edit the listing file you just moved into lab1 so that it has a line of text at the top (such as "Here is my directory structure"). Save the resulting file. You can launch nano by nano listing.

Now, use Nano to create a file in your lab1 directory called lab1.txt. Type the answer to the following exercise in that file.

Exercise 2:
  • What do the columns of the output of ls mean? Here's a form for your answer.
    -rw-------   : what?
    1 : what?
    jpr : what?
    jpr : what?
    40 : what?
    Aug 29 18:25 : what?
    listing : what?

Now, let's experiment with a few more commands.
Exercise 3:
Choose three more Linux commands. Describe them and give examples of their use; type the answers in your lab1.txt file.

 

Your first Perl program!
Time to write your first Perl program! Use nano hello.pl to create a file called hello.pl in your lab1 directory. Then type the following code in it:

#!/usr/bin/perl -w

print "Hello, World!\n";

Now try running your brand-new program:

jpr@sulu (i211/lab1) > perl hello.pl
Hello, World!
jpr@sulu (i211/lab1) >


If your program prints Hello, world! then congratulations! You've written your first Perl program. Now let's improve it so that it prints out an HTML page. To do this, we can use the handy module CGI.pm. Later, we'll talk more about how this works, but for now, use Nano to change your hello.pl file so that it looks like this:

#!/usr/bin/perl -w
use CGI qw( :standard );
use strict;

print header(), start_html( 'Hello!' );
print "Hello, World!";
print end_html();


Okay, that's a lot to understand all at once, but don't worry. We'll discuss it in detail later. For now, just run your new program and watch how it prints out all the HTML you need for a web page, automatically. Now, let's put the script in the right place to be viewed by a browser.

jpr@sulu (i211/lab1) > cp hello.pl /var/www/cgi-bin/jpr/hello.cgi
jpr@sulu (i211/lab1) > chmod 755 /var/www/cgi-bin/jpr/hello.cgi
jpr@sulu (i211/lab1) >


Those cryptic commands accomplished two things:
  1. Copied hello.pl to a file called hello.cgi in the directory /var/www/cgi-bin/jpr/ (of course, you should have replaced jpr with your own username).
  2. Changed the new hello.cgi file to have permissions that would allow a web browser to access and run it.
Great, now point your browser to http://sulu.informatics.indiana.edu/cgi-bin/jpr/hello.cgi (where jpr should be replaced by your username) to see if your script works. If it does, then you are done! Time to submit.

 

Turn in your assignment
This semester, we'll be using Oncourse to submit assignments. Since you need to use a web browser to access Oncourse, you'll need to download your files onto your desktop computer before submitting. To do this, we'll first create a "tarball" of your lab directory, then download that and submit it to Oncourse.

jpr@sulu (i211/lab1) > cd ..
jpr@sulu (i211) > tar cz lab1 > lab1.tar.gz
jpr@sulu (i211) >


Now the directory lab1, together with all the files that were in it, is contained in the lab1.tar.gz file. Use the "File Transfer" menu option to download this file to your desktop, and submit it to the "Lab 1" assignment in the assignments tab of Oncourse.

Okay, that's it! Go home and have a good weekend.

 

Thanks to Jacob Ratkiewicz for lab content, Sid Stamm for CSS stylesheets, and Rob Signorelli for original syntax-highlighting JavaScript.