The purpose of this lab is to help you develop a working knowledge of the Unix operating system and the vi editor. Unix is the operating system that you will use to compile and run your C++ programs, and vi is the recommended Unix text editor for CS110 lab. In this lab, you will learn:
NOTE: There are NO lab assignments for CS110 labs, but (there's always a "but"), there are three good reasons (probably more) for you to do the lab exercises.
In order to use the PC's in CL135, you must logon with your HYPERION user name and password. For detailed instructions of logging on lab PC's, please refer to this page: http://www.cs.uregina.ca/Technical/department/usepc.html
Double click on the Telnet icon on the Win95 desktop, then choose HERCULES from the Host window and click on Ok. Enter your Hercules username and password when prompted to do so.
Your username and password are on the account sheet you got from your lab instructor.
Unix commands are cryptic, often only two or three characters in length. However, they are representative of the function they perform. For example, ls is the command to get a directory listing.
The general format for Unix commands is:
command -options other arguments
is the name of some command followed by a number of options or other arguments.
Options are generally single characters that represent specific options. The minus sign "-" should be entered before an option.
Type in ls and then type in ls -l
Notice that the "-l" option results in a 'long' listing showing file permissions.
A Unix command summary is stored in HTML form on the CS Department's
WWW site at:
Please be aware that you are responsible for learning all of these basic commands.
Unix commands descriptions are all kept on-line in the form of "manual" entries. If you do not know the specific command to perform a function, you can enter the "man " command, followed by the "-k" option, and then a keyword that relates to the command you are looking for. For example, if you wanted to find the command to change your password, you could use "password" as a keyword.
Type in the following:
man -k password
There will be several related Unix manual entries cited. Look for the one most relevant to what you want to do. i.e.
change login password and password attributes
What is the 5 letter Unix command to change your password?
This document will present other Unix commands in the context of tasks
that you will need to perform for your CS110 class. It is assumed that
you have no prior experience with Unix and are using your Hercules account
for the first time.
The vi Editor
A standard editor in Unix operating systems is "vi", pronounced vee-eye. Like Unix, vi is cryptic, designed to minimize typing. You can use other editors if you wish, but you will be tested on vi in your class quizzes and exams.
to call up vi. If the file does not exist, it will be created. If the file does exist, it will be read into the edit buffer.
A few notes on vi before we get into the commands:
To enter insert mode, position your cursor at the desired location
and enter one of the following characters. The letter you type will not
be displayed because you are not yet in insert mode.
|i||insert before cursor position|
|a||insert after cursor position|
|O||insert before current line|
|o||insert after current line|
To end any insertion, type the Esc key.
If you are not sure, type the Esc key anyway, it won't hurt.
To move around in the file:
|Arrow keys.||You can use the keyboard arrow keys.|
|Ctrl f||moves you forward one 'screen' at a time|
|Ctrl b||moves you backward one 'screen' at a time|
|G||moves you to the end of the file|
|/search_string/||If you want to go to the location in a file where a specific word or phrase is located, you can put that word inside slashes "/" and vi will take you to that spot. Note that it will take you to the first occurrence of that search string. If that's not the one you want, enter n and vi will take you to the next occurence of the string.|
To replace character(s):
|r||replace the current character|
|R||replace text starting from the cursor position|
Replacement initiated by R must be terminated by typing the Esc key.
To delete character(s):
|x||delete the current character|
|[n]dd||delete the current line(s)
- the line(s) are moved into the edit buffer.
e.g. dd deletes the current line
e.g. 3dd deletes 3 lines starting at the current line.
In other words, precede dd with a number to specify the number of lines to delete.
To "cut/copy & paste" within the vi editor:
Use vi commands to cut or copy lines into the edit buffer and then move to the desired location in your file and enter the vi paste command.
|[n]dd||delete (cut) the current line(s) (See the description just given.)|
|[n]yy||yank (copy) the current line(s)
- a copy of the lines is placed into the edit buffer.
e.g. yy yanks the current line
e.g. 3yy yanks 3 lines starting at the current line.
In other words, precede yy with a number to specify the number of lines to yank.
|p||paste the line(s) from the edit buffer after the current cursor position.|
|P||paste the line(s) from the edit buffer before the current cursor position.|
To "cut/copy & paste" from outside the vi editor:
When you are in a browser like Netscape or another PC application,
use your mouse to highlight the text that you want to copy
into the clipboard.
Select Edit -> Copy from the application's menu.
Now activate your Telnet window, with vi active.
This vi command translates to "set no autoindent".
You'll find that if you don't set this, when you do your paste, the text will become very messy as vi attempts to automatically indent lines.
|Get into insert mode. (First make sure you are at the desired location in your file.)|
|Select this option from the Telnet menu. It takes the contents of the Windows clipboard and pastes it into your file.|
To exit the VI editor:
|:q!||exits vi without saving changes|
|:wq||saves the file and then exits|
|ZZ||saves the file and then exits|
Start by entering: vi header.txt
In vi, you are either in command mode or insert mode. You are now in command mode so that every letter you type is interpreted as a command.
To start entering text, type the letter "i" to get into insert mode.
You will remain in insert mode until you type the ESC key; otherwise, everything
you type is entered into your file.
NOTE: do not use the arrow keys while you are in insert mode.
While in insert mode, enter the following lines. Do not worry about mistakes, you can correct them later.
/*********************************************************** Name: Student Number: Assignment Number: Program Creation Date: Purpose of the Program: ***********************************************************/
Type the Esc key to get out of insert mode. You are now in command mode and can make corrections/additions.
For example, you could use the arrow keys to move the cursor to the
colon ":" following "Name". Then type the "a" key to get into append mode
so that you can add your name to that line.
Remember to type the Esc key to get out of insert mode.
When you are finished making all changes, be sure to write the contents of the edit buffer and quit vi by entering:
You will notice that as soon as you type the colon, the cursor will move down to the bottom left corner of the screen. If this does not happen, perhaps you are still in insert mode. Just type the Esc key again to get back to command mode.
For more information on the vi editor, refer to the 5.2. VI EDITOR section of the CS Dept Users Manual.
The Users Manual can also be accessed from the CS Dept Home page, following
the links: Computing Information - User's Manuals.
In some sections of your labs, you will be asked
to transfer some documents, for example, sample C++ applications from
Mercury -- the CS Department instructor's machine, to Hercules -- the machine
where you have your accounts.
To do this you use the command-line FTP
program located on Hercules.
The following is a sample session showing
how to transfer
First, from Hercules, change to the directory where you want to store this file, and then follow the following procedures.
Note: The commands in red are those that you are supposed to enter. The instructions for responding to particular prompts are in bold.
The following is a sample session showing how to transferhello.cpp, from Mercury's pub/class/110/ftp/cpp directory to your Hercules account.
hercules% ftp ftp Connected to MERCURY.CS.UREGINA.CA. 220- 220-Access to this system ... 220 mercury FTP server (Version wu-2.4(3) Tue Jan 16 10:25:00 CST 1996) ready. Name (ftp:pvh): anonymous 331 Guest login ok, send your complete e-mail address as password. Password: (JUST PRESS THE "Enter" KEY HERE) 230- 230- Welcome to ... ftp> ascii (NOTICE YOU MUST ENTER THE ascii MODE) ftp> cd pub/class/110/ftp/cpp 250 CWD command successful. ftp> get hello.cpp ftp> quit hercules%
The answer is that in order to use the printer in CL135, you must print directly from the PC, not from a Telnet to Hercules session. Let's suppose you have the file hello.cpp in your Hercules account space and have made some changes to it. You want to get a printout of it.
To do this you use the graphical FTP
program located on the PC.
Go to the PC desktop and double click the FTP icon.
You will then see a "connect" screen in which you need to select "Hercules" and enter your username and password. Click on the OK button when you have this set up.
When you have established connection, set up the "Remote" side of the window, (the right side), to the desired location in your Hercules account. On the "Local" side, (the left half of the window), you should be located in C:\Workarea. This is fine, you just want a temporary location to hold the file. Make sure you have selected Ascii for a text file, and then click on the left arrow in the center of the screen.
When you have completed your transfer you can go to
Notepad in the Tools folder to read
in your file and print it out.
hercules% CC hello.cpp -LANG:std -o hello hercules% hello Welcome to the world of C++ What is your name? Ada Hi Ada! Welcome to CS110 class. hercules%
Actually, when you enter the compile command, there will be a delay as Hercules processes the file, then you should see something like this on your screen:
hercules% CC hello.cpp -LANG:std -o hello --- C++ prelinker: hello.o --- CC -c -DEFAULT:abi=n32:isa=mips4:proc=r10k -LANG:std -n32 hello.cpp --- C++ prelinker: hello.o --- CC -DEFAULT:abi=n32:isa=mips4:proc=r10k -c -DEFAULT:abi=n32:isa=mips4:proc=r 10k -LANG:std -n32 hello.cpp hercules%Let's have a look at those commands in a little more detail.
|CC||This is the command that says "Compile my C++ program."
Notice that this is capital CC not lower case. Unix is very "case sensitive" i.e. there can be a huge difference in meaning between characters in upper case and those same characters in lower case.
|hello.cpp||This is the name of the C++ program.
Notice that you need to use the extension .cpp
|-LANG:std||This option is needed to is needed so that the compiler will recognize the using namespace std line in your C++ program. namespace std is the area where identifiers such as cout and endl are defined. Actually there can be compatibility issues with some C++ compilers. Refer to your text or any C++ reference; look up namespace or header files in the index.|
|-o hello||This is called the "minus-oh" option.
It tells the linker to create an executable file as specified by the name following -o. It is a convention to name your executable file the same name as the source file without the .cpp extension.
In this example: source file is hello.cpp and executable will be hello.
What happens if you don't use the minus-oh option?
Then your executable file will, by default, be called a.out which is ok, but everytime you compile another C++ file, you would overwrite your a.out file.
Two Unix commands that are useful in association with running a program are the more command and the script command. The more command is handy if your display is longer than a single screen. The script command allows you to save the contents of a program run into a file, which you can then print out. (Useful for putting together an assignment that needs to be handed in for class.)
Refer to the Unix Command Summary for details on the more and the script commands.
Tuesday, 17-Apr-2012 08:57:04 CST