In this course, you will be programming on a remote UNIX machine, which some of you might not be familiar with. This tutorial provides information about some very basic UNIX commands, so you can get started.
Needless to say, to do the assignments you will need to learn a lot more about UNIX and the various tools. For additional details, please read the online documentation accessible from the course web site as well as make liberal use of the man pages on the system itself. Details about the latter are provided below.
First, you will need a terminal emulator into which you can type in the command to log on to the UNIX machine. Because of the name of the protocol used, the command of interest is called telnet. You can type the command in a command prompt window on Microsoft Windows or use the bundled telnet client application that comes with Windows.
If you are running a PC-UNIX such as Linux or FreeBSD, it comes with a full-fledged terminal emulator, so you can just use that. If you are already using a UNIX system, this tutorial probably repeats a lot of the things you already know.
If you are running another operating system, you might be able to use its built-in telnet program. But if none exists, you may have to install the telnet client application and (if required) a terminal emulator program.
The rest of this tutorial will assume that you are using Microsoft Windows 95/NT4 or later.
In order to log on to the server we will use, you start a telnet session with the address of the server (in our case, this is i5.nyu.edu)
1. On the Windows Start menu, click the "Run.." button and enter telnet i5.nyu.edu. This is also the command you will enter if you are using a separate command prompt window.
2. You will get a login: prompt at which time you should type in your username. Upon hitting the Enter key, you will be prompted for your password.
Your username and the password should be the same as you use to access your NYUHome account.
All actions on a UNIX machine happen because of commands you type in on the command line (following the prompt). You can abort a mistyped command by pressing Ctrl-C.
As soon as you log in, type the following four commands (for now, just copy them verbatim):
km309 @ i5 /home1/k/km309 % setenv DISPLAYkm309 @ i5 /home1/k/km309 % setenv TERM vt100km309 @ i5 /home1/k/km309 % resizekm309 @ i5 /home1/k/km309 % stty erase ^H
For the third command, do not type in "^" and "H" separately. Instead, use the Backspace key, and this should automatically create the "^H" character. If pressing the backspace key just moves the cursor back, then you should just abort this command (by typing Ctrl-C).
On UNIX, when you get an account, you are allocated some space, called your home directory, within the enter file system. Think of this as having a folder that you can use at your discretion in Windows. Similar to Windows folders, you can use this space to create subfolders and store all of your files (both the source files and any executables they generate). However, unlike Windows, you cannot use the mouse or menu buttons to open up folders and subfolders or perform other file operations (e.g., copying, deleting, renaming). You can do all of these operations and more but you will need to type in commands, some of which we describe below.
Note that in addition to this space, you also have access to some temporary space on the file system (usually referred to as the /tmp or /usr/tmp directory). However, unlike in your own space, any files you create in the temporary space are not guaranteed to be there once you log out of the system.
When you log on, you're placed in your home directory --- the space allocated to you. There, with the following commands, you can do basic file/directory manipulations.
Note: When you specify a directory or file for the following commands, you have two options. You can either specify an absolute path (for example, /home1/k/km309/test is an absolute path to the test subdirectory) or a relative path using symbols that indicate a location in terms of the current directory: "." (one dot) indicates the current directory, and ".." (two dots) indicate the directory one-level above the current directory. An example of a relative path is ../canna/README. Note the use of the forward slash to delimit directory and file names (unlike the reverse slash in Windows). Keep in mind also that file names and directory names in UNIX cannot have spaces in the middle of them.
pwd displays the absolute path of the current directory.
If you do this as soon as you login, you will obtain the path of your home directory.
ls display files and directories in the current directory excluding hidden files.
ls -l display files and directories in the current directory with their attributes.
ls -a display files and directories in the current directory with their attributes.
cd directory_name move into the specified directory.
mkdir directory_name create a subdirectory in the current directory
rmdir directory_name remove the specified directory (the directory has to be empty, including hidden files)
rm filename remove the specified file
mv filename1 filename2 rename filename1 to filename2
cp filename1 filename2 copy filename1 to filename2
The following screen snapshot shows usage of these commands:
In a UNIX file system, you can set up a permission for each file or directory, which governs who all can access the file or directory and in which mode. In the screen snapshot above, look at the response of the "ls -l" command. The strings "-rw- r--r--" and "drwx------" represent the permissions associated with the file file1 and the directory mail.
The basics about reading this notation are as follows:
The string can be divided into 4 areas: the starting location and 3 areas of three locations each, shown colored for clarity. The first location is set to the letter d for directories and is left empty (-) for files. The three areas themselves represent what permissions are associated with the file or directory for (reading from left to right) the owner, others in the owner's group, and everyone else (all) in the world. In each area, read/write/execute permissions are represented by using r, w, x respectively (- is used when an operation is not permitted). Thus, for file1, the owner is permitted to read and write it, and no permissions are given to anybody else (either in the owner's group or outside it).
In order to change the permissions associated with a file, use the following command:
chmod W+P filename to add permission(s)
chmod W-P filename to remove permission(s)
In the above commands, W must be one of the following letters: o (for owner), g (for group), and a (for all), and P must be one or more of the following letters: r (for read), w (for write), and x (for execute). Thus, the following command:
chmod g+rx filename
gives other members of the group permission to read and execute the file filename.
Similarly, the command
chmod o-rwx filename
removes any permissions previously available to users that are not the owner or not a member of the owner's group.
The commands introduced so far have many more features than we have explained. In order to find out all of the gory details about these and other commands, you can consult an online documentation feature available on every UNIX system. This system, called the man pages (short for manual pages), is accessible using the following command:
man command name (e.g. man chmod)
To scroll forward and back in a man page, use the Space Bar and the b key respectively.
In order to know how to use man command, you can also do: man man
There are 2 widely used text editors in UNIX. One is vi and the other is emacs. In general, vi is more efficient, but harder to use until you get accustomed to it. On the other hand, emacs is a huge application, but easier to use. Here, we introduce very basic operations of these editors. You should consult the links on the web page for additional details.
vi is quite different from text editors like you normally use on Windows such as notepad in that there are separate command and input modes. You enter commands to control the editor in the command mode, and text for the file you are editing in the input mode.
You start vi with the following command:
This opens up a file as shown below.
The vi program starts off in the command mode. Here is a basic list of commands you can use:
To enter input mode:
i the characters you type get inserted before the original location of the cursor
a the characters you type get inserted after the original location of the cursor
To go back to command mode:
To move the cursor:
k move the cursor up
j move the cursor down
l move cursor to the right
h move cursor to the left
To delete a character:
Backspace or Delete works in input mode
x deletes the character at the cursor
:wq write your changes and exit
:q! discard any changes and exit
Emacs will not work properly unless you run the "setenv" commands listed in the To Start With section. You can also use a different terminal emulator (as shown below) that can do VT100 terminal emulation.
You shouldn't have too many problems using emacs. It's pretty much like a Windows editor in that you are by default in the input mode. You have to type two or three key command sequences (usually involving the Ctrl or Meta (Esc) keys) to control the editor. The following is a list of the basic commands:
To start emacs:
To insert characters:
Just type the characters you want to insert
To move the cursor:
The arrow keys should work. In case they do not, you can use the following commands: (Ctrl-f means that while keeping the control key pressed, press the f key)
Ctrl-f Move the cursor one space to the right
Ctrl-b Move the cursor one space to the left
Ctrl-n Move the cursor to the next line
Ctrl-p Move the cursor to the previous line
Ctrl-a Move the cursor to the start of a line
Ctrl-e Move the cursor to the end of a line
To delete a character:
Either the Delete or Backspace key should work to delete individual characters.
Ctrl-d Deletes the character at the cursor location
Ctrl-k Deletes the rest of the line starting from the cursor position
To undo an operation:
Ctrl-x, u (First do Ctrl-x, release both keys, press u)
To abort a command in the middle:
To save the file:
(Ctrl-x, Ctrl-s means first do Ctrl-x, then while keeping the control key pressed, press the s key)
Ctrl-x, Ctrl-s saves the file under the original name
Ctrl-x Ctrl-w prompts for the name under which the file should be stored (in the one-line command input region at the bottom of the emacs window)
Some more advanced operations:
To select a region:
Move the cursor to the start of the region and type the following command:
(Typing Esc-x will cause M-x to appear in the one-line command input region. Here you can type the characters making up the command "set-mark" and hit Enter)
Then move the cursor to the end of the region, at which point you can type the following commands:
Ctrl-w Deletes the selected region (into an implicit clipboard)
Esc-w Copies the selected region (into an implicit clipboard)
To paste a region previously put into the clipboard:
Ctrl-y Pastes the region starting at the current cursor position
Now let's write a program with emacs, compile it, and run it.
First, start emacs with a file name hw.cc.
Second, write a very simple program by just typing in the appropriate characters. Save this by typing Ctrl-x, Ctrl-s and exit from Emacs by typing Ctrl-x, Ctrl-c.
Then you compile the source code as follows:
g++ source_filename -o executable_filename
Let's call the executable as hw. The compilation command looks like:
g++ hw.cc -o hw
At this point, your directory should contain a file called hw. You can check this by using the ls -l command described earlier. Note that at least one of the executable permission bits of the file will be set.
To run the file, simply type the executable name followed by any arguments (in this case there are none). The ./hw ensures that the file being executed is actually the one in the current directory.
Here is a screen shot:
For your programs, the compilation procedure may not go as smoothly because of various errors. To figure out what an error message means, refer to the g++ documentation and/or a C++ book. Having generated an executable, if it does not run as intended, refer to the gdb documentation available from the web site for information about how to run the program under a debugger.
To log out, simply type logout