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:
- Learn how to turn in your assignment.
- Use several UNIX commands discussed in the Lab
1.
- 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:
If you are working on one of the
department machines (e.g. tiglon, jaguar in lab or from home using
putty) do the following:
cd to the directory that contains your
program (chart.cpp for this assignment)
copy this file to your turnin directory
$ cd ~tyson/112/p1
$ cp chart.cpp /user/projects/csci112/p1/tyson
In the above example, I have my program in a directory called p1 which
is in a directory called 112 which is in my home directory.
Make sure you replace the tyson with YOUR USERNAME
If you are working from home using
Cygwin or Linux
NOTE: scp comes with Linux. For
Cygwin you have to check the openssh box in the Net directory.
cd to the directory that contains your program (chart.cpp for this
assignment)
copy this file to your turnin directory
$ cd ~tyson/112/p1
$ scp chart.cpp
tyson@tiglon.ecst.csuchico.edu:/user/projects/csci112/p1/tyson
answer yes when asked "Are you sure you
want to continue"
now enter your password for your ecst account
In the above example, I have my program in a directory called p1 which
is in a directory called 112 which is in my home directory.
Make sure you replace the BOTH tyson with YOUR USERNAME
For late assignments use p1_late instead of p1.
Making sure that it worked:
Log on to one of the department
machines (tiglon.ecst.csuchico.edu or jaguar.ecst.csuchico.edu) from a
University machine or using Putty from your home machine
Perform the ls command for your turnin directory:
$ ls /user/projects/csci112/p1/USERNAME
where USERNAME is your ecst username
You should see chart.cpp listed in this directory. If it is not
there, you have not turned it in.
Unix editors:
- pico (try typing "man pico" at the UNIX prompt) (a
similar editor available in Cygwin and Linux is nano)
- vim (new version of vi editor) Vim Homepage
works on most UNIX and microsoft platforms.
- emacs GNU's
emacs
page works on most UNIX and microsoft platforms
pico is like riding a tricycle. It is easy to learn,
but
neither fun or powerful.
vim and emacs are like a bicycle. Hard to learn at first, but one
you
know it, much faster than a tricycle.
Many students never learn vim or emacs, and spend years editing at
tricycle
speeds.
For now, you might want to stick to pico, but I recommend that at some
point
this semester you learn to use either emacs or vim.
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 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
Q1: Why doesn't chmod use, 1,
2, 3 instead of 1,2,4? What is special about 1,2,4? answer
Thus if you want a file to be readable and writable by the owner, you
set
the owners protection to 6 (2 + 4) using the chmod command.
$ chmod 600 lab1.cpp
The three integers passed to chmod represent the owner, group, other
numbers.
In the above example, the group and world have no access to the
file
lab1.cpp.
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
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).
- ~/classes/112/labs/lab1 ~/classes/112/labs/lab2 ...
- ~/classes/112/labs/1 ~/classes/112/labs/2 ...
- ~/112/labs/lab1 ~/112/labs/lab2 ...
- ~/1 ~/2 ... (this is a joke, these names in
your home directory would
not be very meaningful)
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.
(2) Protect your new directories so no one else can look at them
In UNIX, if you protect a directory,
all the files in that directory and the
subdirectories are also protected
In the first example above, you could change the protection of
~/classes
You will need to use the command
chmod (see above)
.
Recall that you must set the permission of a directory to executable if
you want access.
Q2: What number do you want to pass
to chmod so only you can read this directory? answer
Q3: What command can you use to
find out how to use chmod? answer
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.
Q4: 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 211 entries, sometimes this trick doesn't work?
How do you think I figured out there were 208? There is a program
that
counts lines, words, and characters called wc.
Q5: 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: Why doesn't chmod
use, 1, 2, 3 instead of 1,2,4? What is special about 1,2,4?
There are three type of protection
(read, write, execute) for each category (owner, group, other).
Each type of protection is either true or false. Instead of
having a variable for each type of protection, a single integer is used
for each category and the three types of protection are encoded into
that integer. The easiest way to encode boolean values into an
integer is to use the individual bits.
The lowest order bit is used for execute (000001). This equals
the number 1.
The second order bit is used for write (0000010). This equals the
number 2.
The third order bit is used for read (0000100). This equals the
number 4.
Each of these three bits are independent. In other words, no
matter what number you have, these bits are either 1 or 0.
Consider the number 6. It is represented by 0000110. That
corresponds to the 2nd and 3rd bits set to 1. The second bit
means write, and the third bit means read so 6 == read and write but no
execute.
If you execute "man chmod" you can learn about setting protections
without knowing about these numbers. I use the numbers because it
is important to know this technique of encoding many variables in a
single integer.
Q2 Answer: What
number do you want to pass to chmod so only you can read this directory?
You want to be able to read (00001)
write (00010) and execute (00100) your directory. In order to
read a directory you have to have execute permission.
00001 + 00010 + 00100 = 00111 = 7
You want to pass 7 as the "owner" value of chmod.
You don't want the "group" or "other" to have access to your directory,
so pass 0 for each of them.
$ chmod 700 directory_name_here
Q3 Answer: What command can you use
to find out how to use chmod?
$ man chmod
Q4 Answer:What
UNIX command deletes a file?
$ rm <filename>
Q5 Answer: How could you take the
output from man and pass it to wc?
$ man -k remove | wc