Introduction to UNIX and g++ Compiler

Lab 2
CSCI 112


UNIX Account

If your UNIX account is not working, see Elbert Chan the system administrator.  His office hours are M-F 3:30 - 5:00 in OCNL 249


Goals of today's lab:
  1. Learn how to turn in your assignment.
  2. Use several UNIX commands discussed in the Lab 1.
  3. Create, compile, and run a C++ program.

About labs:
This is not a test.  Try to do the exercises on your own, but if you get stuck, ask for help.

How to turn in assignments:

See the turn in instructions.
    


Unix editors:

When writing programs in Unix/Linux you use a stand alone editor to create your program (vs. using the editor built in to Visual C++).  This means you can pick from hundreds of available editors.  Here are some common choices:
  1. pico & nano:  a very simple text based editor (no mouse), all the commands are always on the screen (on some machines it is called nano, and on some pico)
  2. gvim: a graphical version of one of vi (one of the early UNIX editors)  NOTE: broken on jaguar today
  3. vim (new version of vi editor)  Vim Homepage  works on most UNIX and microsoft platforms.
  4. emacs GNU's emacs page works on most UNIX and microsoft platforms
All of these are available on Linux and Cygwin.  Vim and Emacs are hard to learn, so for now I suggest you stick to one of the first two.  In a couple weeks I will give lab on learn vim.

If you plan to use Putty to do your assignments at home, then vim or pico is a better choice (you can't use gvim over putty).


Unix file permission & chmod:

There are 3 levels of file protection in UNIX: owner, group, other

You can set 3 types of permission for each level: read, write, execute
read == can read the file
write == can write the file
execute == if it is a directory, can cd to the directory
                  if it is a file, can execute a file (not all files can be executed)

An single integer is used to describe each level of protection.  Thus file permissions can be described by three integers

    one integer for the owner
    one integer for the group  (each file has an attribute which names which group it belongs to)
    one integer for the other

Since there are 3 levels of permission (read, write, execute) described by each of these integers, we need an encoding method.  That method is as follows
if the file is executable, add a 1 to the number
if the file is writable, add a 2 to the number
if the file is readable, add a 4 to the number

Here are the most common file protections:
$ chmod 644   the owner can read/write, the group and world can only read
$ chmod 755   the owner can read/write/execute, the group and world can read and execute
$ chmod 700   the owner can read/write/execute, the group and world have no access
$ chmod 600   the owner can read/write, the group and world have no access
If you want to make sure no one can read ANY of your files, do the following:

$ cd
$ chmod 700 .

The first command takes you to your home directory.
The second command sets the protection on the current directory (.) so that only you (the owner) have access.

Exercise 1: Creating directories
(1) Create a directory for your 112 labs.  You may choose the name and structure.  Here are some suggestions (remember that the ~ stands for your home directory).
      Hint:  use mkdir to create a directory.  You have to create each directory along the way.   For example,
      if you choose ~/classes/112/labs/lab1 as your naming convention, you have to call mkdir 4 times.  



Exercise 2: Creating and compiling a C++ program
In your directory for lab 2, create the file hello.cpp (using any text editor, pico is the easiest to use).  Edit hello.cpp so it contains the following text.

#include <iostream>
using namespace std;

int
main()
{
cout << "hello world" << endl;
return 0;
}

Don't use cut & paste, I want you to practice using the editor.

Compile this file using the command:

$ g++   hello.cpp

if you got an error, use the editor to fix the error

This should have created the file a.out, use "$ ls -l" to find out if a.out is in your directory.  Notice that a.out automatically has executable protection.

Run your hello program.  You can execute the program by typing a.out at the UNIX prompt. 

$ a.out

     If a.out doesn't work, try ./a.out and ask for help fixing your environment so you don't need the "./".

Run your hello program and redirect the output to the file hello.out using the shell's redirection symbol > (see lecture notes from lab1 for the redirect systax)

You can view hello.out using an editor or the cat command (cat simply writes the files passed to it to standard output).

    $ cat   hello.out


Exercise 3: Redirecting standard input

Modify your program to prompt for and read a user's name as follows.

#include <iostream>
#include <string>

using namespace std;

int
main()
{
string name;

cout << "What is your name? ";
cin >> name;
cout << "hello " << name << endl;
return 0;

}


Compile and run this program (using the same g++ command above).

Now create a file with a name in it (call it name_file).  You can do this using an editor such as pico.

Using redirection, read from this file and write to another

$ a.out < name_file > hello.out

Check the hello.out to make sure it is correct.

Q1: What UNIX command deletes a file? answer

Delete the file: hello.out


Exercise 4: Using man -k, pipes, less, wc

Execute the following command:

$ man -k remove

What happened?  Too fast?  try:

$ man -k remove | less

There are 88 entries, sometimes this trick doesn't work?

How do you think I figured out there were 88?  There is a program that counts lines, words, and characters called wc.

Q2: How could you take the output from man and pass it to wc? answer

How many entries are there for permission

      $ man -k permission

Extra time:

    Try the following on-line UNIX tutorial UNIX Tutorial for Beginners




Q1 Answer:What UNIX command deletes a file?

$ rm <filename>

Q2 Answer: How could you take the output from man and pass it to wc?

$ man -k remove | wc