CPE 101 Lab 2 - Getting Started on the Command Line

Ground Rules:

You must work on this lab individually.

Due: End of lab, Thursday, April 7

Goals:


Orientation:

If you are actually enrolled in CPE 101 (not waitlisted or added) you should have a computer account that allows you to logon to most of the Computer Science Department computers. If you are adding the class, we will need to request an account for you using this form.

This lab helps you login, edit a file, and handin the file. The file you create will be handed in to me electronically in the same manner you will hand in all your labs and programs this quarter.


Part 1: Logging in

That being said, you can attempt to log in following these instructions to log on to server named vogon. You can do so in the Computer Science Lab (CSL) in 14-235 or from any other computer with an internet connection running UNIX, Linux, OS-X, or Windows. Being able to do this from home will increase your level of success in this class since it will enable you to work when and wherever you want. If you are using Windows at home you will have to download and install the SSH client described in the instructions linked above.

If you logged on to a machine in the lab you are still not quite on vogon. You'll have to start a terminal and connect to vogon using the command:

ssh vogon

Later, when you are done working on vogon, you will leave the machine by typing:

exit

Part 2: Create a new file using an editor

Once you are successfully logged on to vogon, you will need to create a file from the command line.  You should see a prompt that will look something like this:


        vogon: ~$

Now, enter the following command exactly as specified. I have made all of the commands that you are to enter in bold face.


        vogon: ~$ mkdir cpe101  (create a directory named 'cpe101')
        vogon: ~$ cd cpe101     (change to the cpe101 directory)
        vogon: ~$ vim my_sample (edit my_sample using vim)

Part 3: Editing the file using VIM

  Once the VIM screen appears, you can now start editing the file.  Remember, vim is modal editor - there are 2 modes:  command mode and insert mode.  You can refer to the reference card for all of the keys, but here are some common commands:

i (command mode)
switch to insert mode
Esc (insert mode)
switch to command mode
j
move down
k
move up
h
move left
l
move right
x
delete character
:w
save file
:q
exit vim


     Your file should contain the following 2 sentences

        This is my lab 1 file.
This class is CPE 101.

     Save the file and quit out of vim.

Part 3: Edit, compile, and run a first program

Copy the miles2feet.c and checkit.h files into your directory.


       
vogon: ~$ cp ~djanzen/www/courses/101S11/labs/lab1/miles2feet.c .
       
vogon: ~$ cp ~djanzen/www/courses/101S11/labs/lab1/checkit.h .

Note that the ~djanzen means in djanzen's home directory, and the . means in the current directory.

Type ls to confirm that two files are now in your directory.

Use vim to edit miles2feet.c. Add a variable miles, use printf and scanf to read a value into miles, then use the function to convert miles to feet and print the result.

  Compile your program with the following command:
        vogon: ~$ gcc -Wall -Werror -ansi -pedantic miles2feet.c -o m2f

  Run your program with the following command:
        vogon: ~$ m2f

Part 4: Using makefiles

You can simplify your compilation by using a makefile. First, download the sample makefile. Once you place this file in your lab directory, edit it so the compile command is the same as above. Then, you can compile your code just by typing:

        vogon: ~$ make  

NOTE: If editing the second line in the makefile (after the line default), you must insert a Tab character before the gcc command. You can do this in vi by entering Ctrl-V, and then entering the Tab key.

Part 5: Input/output redirection

In this lab, you should also test your program with input and output redirection. Create an input file named sample_input. This file should contain one line with the input you want read by your program (e.g. 3). Input redirection allows you to send input from a file instead of reading it from the keyboard. You can test your program with the following command:

        vogon: ~$ m2f < sample_input  

You will notice that when using input redirection, the input from the file will not be shown to the screen. To save the output of your program to a file, use output redirection:

        vogon: ~$ m2f < sample_input > sample_output  

The previous command will read input from the file sample_input and save the output to a file name sample_output. You can look at the contents of program_output with the following command:

        vogon: ~$ more sample_output  

Part 6: Using diff

The diff program compares 2 files and prints out any differences between the files. This is useful for comparing your program output with the expected output. For example, the command:

        vogon: ~$ diff -w -B sample_output ref_output  

If the 2 files are exactly the same, diff will print out nothing to the screen. If the files are different, diff will print out the differences. For those interested, here is a how to read diff output. Make a small change to your sample_output file, then rename it to ref_output. Run your program again using the input/output redirection. Do a diff to see the differences in the two files.


  If everything works, show your instructor and turn in your program using the following command:

       
vogon: ~$ handin djanzen 101_lab2 miles2feet.c makefile  

When you are done, leave vogon by typing the following:

      vogon: ~$ exit

   
  Complete this survey.

   Congratulations, you are done with lab 2!  If you would like more practice with vim, I suggest running through the vim tutorial link on Blackboard.  I have also posted a table of some useful UNIX commands:


Command
Usage Example
Description
cd
cd my_dir
Change to my_directory
more
more testfile
Look at the contents of testfile
pwd
pwd
Show the present working directory
ls
ls
List the files in the current directory
mkdir
mkdir test
Make a new directory called 'test'
rm
rm testfile
Delete the file named 'testfile'
hostname
hostname
Show the name of the computer
exit/logout
exit
End the current shell session