by Elizabeth Patitsas, University of Toronto, December 2012
A command-line interface is a way of interacting with a
computer through textual commands, rather than using the
point-and-click graphical approach you are probably used to.
Command-line interfaces are actually much older than graphical
approaches, and despite the higher learning curve, are much more
powerful. You can do more things on the command-line, you can
automate them, you can be more precise, and you receive more
precise feedback from the computer in response.
In CS 190, we'll be using command-line interfaces throughout the
term; this document serves as a beginner's guide. As your code
is expected to run on the ECF machines, we will begin with how
to access the ECF machines remotely.
Before starting, ensure that your ECF account works by
logging in here.
All of the code you will be asked to write for your
labs/assignments is expected to compile on the ECF machines.
Because C is a platform-dependent language, it is often the case
that C code written on your personal computer will not behave
the same as it would on the ECF machines.
Fortunately, you can access the ECF machines remotely, through
a protocol known as ssh (or "secure shell"). To use ssh,
however, you will need a Unix terminal available, so that is
where we'll begin.
Depending on your operating system, this will either be
straightforward (Linux/Mac), or a take a bit of time (Windows).
Linux is a great system for programming on, and probably the
best for coding C on. Our first step will be to open up a
terminal. If you've never done this before, hop on over to Starting
a Terminal on Ubuntu.
Once you have it open, it's time to roll. At the command prompt, you want to type
ssh -X USERNAME@remote.ecf.utoronto.ca
Getting a terminal open in Macintosh is fairly straightforward.
If you've never done it before, the Terminal application is
located in the Utilities folder within Applications.
A little command-line prompt will appear when you open up the terminal. In there, you want to type
ssh -X USERNAME@remote.ecf.utoronto.ca
There are numerous ways to set up Unix utilities such as a
terminal -- but perhaps the most robust way is to install a
virtual Linux distribution on your computer. This requires
creating a virtual machine on your computer.
A virtual machine is a simulation of your own machine's
hardware, and this simulation runs on your computer. So, when we
install Linux on a virtual machine on your computer, Windows
continues to be the system operating on your actual machine. The
Linux installation is running on a simulation of your computer.
To set this up, you will need to download two things: VMPlayer,
which is free software that creates and runs virtual machines
for you; and a Linux distribution for you to install on your
virtual machine. I recommend Ubuntu for
those new to Linux.
To get your virtual Linux installation working, there is a step-by-step
guide available here.
Once you have your virtual Linux machine working, head over to
Section 1.1 and follow the information there.
Now that you're logged into the ECF machine, there are a number
of things you can ask the machine to do for you.
Let's start with typing:
ls
You'll get something that looks like:
Desktop ECF_BACKUP_DIRS
This is a list of all the files and directories within the
directory that you are currently in. You can move around between
directories with the command cd (change directory); try
entering
cd Desktop
ls
You'll now see the list of files and directories on your
Desktop. To go back to your home directory, which is where we
started, we need to go up one level in your filesystem. To do
so, type:
cd ../
The ../ refers to the directory one level higher than you
currently are in.
We can make our own directories. Type:
mkdir cs190
This will create a directory named "cs190". Now type:
cd cs190
ls
To see the contents of your cs190 folder; you'll see nothing
gets displayed! There are currently no files in this directory.
Let's make one now by opening a text editor; type:
gedit hello_world.py &
What this does is it opens a graphical file editor called
gedit, to edit a new file called "hello_world.py". The &
symbol tells the computer to run this process in the background
-- without it, we cannot run other commands on the terminal.
When the editor starts up, type
print "hello world"
and save the file. Close gedit.
On your terminal, you can now run this Python programme with
the following command:
python hello_world.py
It will display "hello world" on your terminal.
You can do a number of things to files on the command-line; for
example:
Any program you can run on your computer can be opened through
the terminal. For example, to run Matlab, type:
matlab
And Matlab will boot up for you. This can be quite slow! To
open Matlab's text-based interface, you can instead type:
matlab -nojvm
Te text-based interface-looks like this; to make a variable a
and set it to zero, you enter it like:
< M A T L A B (R) >
Copyright 1984-2010 The MathWorks, Inc.
Version 7.10.0.499 (R2010a) 32-bit (glnx86)
February 5, 2010
----------------------------------------------------------
Your MATLAB license will expire in 44 days.
Please contact your system administrator or
The MathWorks to renew this license.
----------------------------------------------------------
To get started, type one of these: helpwin, helpdesk, or demo.
For product information, visit www.mathworks.com.
>>
>> a = 0
a =
0
>> quit()
The quit() command closes Matlab and returns you to the
command-line.
GCC is the GNU C compiler, which we'll be using in this course.
Type now:
gedit hello_world.c &
And paste into this new file:
#include <stdio.h>
int main()
{
printf("Hello, World!");
}
Save the file. We can now compile it at the command line like
so:
gcc hello_world.c
Which creates a binary file named "a.out" which, when executed,
will run our code. To execute the file, type:
./a.out
Note the "./" in front of the name of the binary file -- this
is how you execute a binary file in Unix! The program will then
print "Hello, World!" for us.
Unix has many great features available to you -- we could spend
an entire course learning all of its command-line utilities! But
for now, we will only look at one more: the time utility.
Adding "time" before any command will tell your computer to
time how long it takes to execute the command. For example:
[patitsas@remote ~/cs190]$ time ./a.out
Hello, World!
0.000u 0.001s 0:00.00 0.0% 0+0k 0+0io 0pf+0w
This can look a bit cryptic since there's a lot of information
here. It begins by telling us how long the CPU spent on this
command.
The time utility shows us two things: "user time" and "system
time". Whenever you execute a program, some time will be spent
setting up the process, sending it to the CPU, getting the
output, and so on. That is known as "system time" -- if you
remove that, you have "user time" -- the time that was spent
actually running the program.
In this case, we spent less than 0.000 seconds setting up the
program, and 0.001 seconds actually running it. (To learn more
about the time command, type "man time" to open up the manual on
the time utility.)
Python will be slower since it has to load an intepreter. To
compare:
[patitsas@remote ~/cs190]$ time python hello_world.py
hello world!
0.006u 0.004s 0:00.01 0.0% 0+0k 0+0io 0pf+0w
Here it took 0.006 seconds to get Python running and to send
the code over to the CPU -- and then the CPU spent 0.004 seconds
chugging away on it. This seems pretty fast -- faster than human
perception can notice -- but when you have large programs,
you'll see that C performs a lot faster than Python!
So now we have some files on the ECF machines -- how can we
transfer them to our own computers?
To do so, download Filezilla.
(On Ubuntu you can open a terminal and type "sudo apt-get
install filezilla.)
Once you have it running, enter the following details:
Host: ecf.toronto.edu
Username: your username
Password: your password
Port: 22
and then click "Quick Connect". Once connected, it will show
you a listing of your files and directories on the ECF machines;
you can now drag and drop files between the two computers.