Chapter 1

Introduction to Unix and X

Introduction

In this chapter, we see what Unix is and meet a few simple commands.

Unix

Unix is the only operating system which runs on any computer from any manufacturer. It is nearly thirty years old now but it is still worth learning; this is partly because it is so widely used and partly because many of its novel features are now accepted practice in modern operating systems. Perhaps the best thing about it is that it was written by two programmers for their own use. They wrote it because the alternatives of the time were inflexible, idiosyncratic and down-right unpleasant to use. Unix is a little idiosyncratic too, but many programmers today still find it a joy to use compared with any of its more modern counterparts.

The X Window System

The X Window System is the most powerful windowing system available. It isn't used in office equipment but is used in specialised, expensive applications and by computer professionals themselves. It is the de facto standard for windowing on Unix systems and it can be used on PCs. Later in the book we will see what facilities X provides, but to start with, we will just use the system without worrying about whether a facility is provided by Unix or by X.

The Motif window manager

This is the most widely used window manager that is used with Unix. Again, for now we will just use it.

The Bourne shell

We will see, later in this chapter, what a shell is. Of the many shell's available, the Bourne shell is the easiest to learn and find out about; it is also the most widely available.

This book assumes you will be using Unix through the Bourne shell via the X Window System and the Motif window manager. If you don't have access to all of these, you need to find another book or to persuade your system manager to provide them for you.

The big ideas

Many of what were the more novel features of Unix have become common practice in modern operating systems. We will refer to these features as the big ideas; none of them was invented by the authors of Unix but they did bring them together from various sources into the first versions of Unix.

The big ideas are:

They may seem meaningless now but they will help to provide structure as you read the book. We will examine each of them in more detail in later chapters.

Logging in

When you log in to a Unix system these days you see something like this on your terminal.


+------------------------------+
| Welcome to the Sun MPS - Ivy |
|                              |
| Login:                       |
| Password:                    |
|                              |
+------------------------------+

DIAGRAM OF LOGIN WINDOW

You type the login code that your system manager has given you, then you press the newline or enter or return key. Next you enter the password in the same way; the characters will not be echoed onto the screen. The only thing that is likely to go wrong is if a Sun reader has been using the terminal before you and has left it locked into upper-case (Caps Lock). Login codes and passwords are generally in lower-case letters but if yours has capital letters or digits or any other characters in it then you must type them exactly as written.

When you have logged in you should see a window like this:


+-+----------------------------------+-+-+
|-|                login_xterm       |.|O|
+-+----------------------------------+-+-+
| |                                      |
| |                                      |
| |                                      |
| |                                      |
| | $ O                                  |
+-+--------------------------------------+

DIAGRAM OF XTERM WINDOW

The window is called an xterm. At the bottom left corner of the xterm you should see a dollar sign ($) followed by a small rectangle. It is a prompt to show you that Unix is ready to execute a command. The small rectangle is a typing cursor and should not be confused with the mouse cursor which tracks mouse movements on the screen. The program that displays the prompt is called a shell. There are several different shells available and they have different prompts. The one whose prompt is a dollar sign is called the Bourne shell. We are learning it because it is described in all books on Unix and is the easiest to learn.

If you don't have a dollar sign but have something like this: [41]% instead it is because your system manager has given you another shell called the C-shell. You can try using the chsh command to tell Unix you want the Bourne shell in future. If that does not work, you will have to ask the system manager to provide you with the Bourne shell.

Shell commands

As well a showing a prompt, the shell executes Unix commands for you. You have to type the name of the command and press the return key. When you type the name of the command the characters will appear in the xterm after the prompt. If the characters do not appear it will be because the mouse cursor is not inside the xterm. If you move the mouse cursor inside the xterm you will see the typing cursor change from a hollow box to a solid one. You will also see the frame around the xterm window change colour. Try moving the mouse cursor in and out of the window and watch what happens.

Next let's try typing our first Unix command - the date command.

$ date
Fri Jun 16 15:37:56 BST 1995
$

The date command produces just one line of output. The dollar sign after it is the Bourne shell's indication that you can now enter another command.

Throughout the book, words that look like this: word are words that have been entered into Unix or displayed by Unix. Unix commands will look like that, whenever they appear in the text.

If nothing happened when you typed date it was because you did not press the return key which shows that you have finished typing the line. Most Unix commands can be typed on one line.

Now we enter another command:

$ cal
   June 1995
 S  M Tu  W Th  F  S
             1  2  3
 4  5  6  7  8  9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30

$

The cal command shows you a calendar for the current month.

Logging out

Before we go any further, it is wise to learn how to log out. If we don't log out, any one who comes along will have access to all our files. Childish people like to interfere with other people's files. Dishonest people steal other people's assignments. Kind people do the logging out for people they discover still logged in.

Logging out is easy - all you have to do is stop the xterm that was started when you logged in. This might have a distinctive title like login_xterm on your system. To stop an xterm that is running the Bourne shell, you hold down the "Control" or "Ctrl" key and type a "D". When the login_xterm is stopped, all windows on the screen should close and you are logged out.

Correcting mistakes

If we make a mistake while typing the name of a Unix command, we can correct it by pressing the key that rubs out the character just typed. This varies from system to system. Mine is at the top right of the main keypad and is marked with an left pointing arrow; when it is pressed it removes the last character from the line. The only line that can be corrected in an xterm is the last one and then only if we have not pressed the return key to end the line. When the return key is pressed the command is obeyed and it is too late to correct the mistakes by rubbing out characters.

There are often four other keys marked with arrows; they should not be used with xterms. If these keys echo like this when you type them:

$ ^[[A^[[B^[[D^[[C

you are unlikely to be troubled by them. On some systems, they cause the typing cursor to go up, down, left and right respectively while leaving the screen unmarked. They can then be a nuisance until you learn not to use them.

Setting the rubout key

If the rubout key isn't set up correctly on your system you can correct it for the current window by using this command:

$ stty erase X
$

The example would make the X key the rubout key. Clearly this would be impractical. You should instead, hit the key you want to use for rubbing out the previous character.

Command names must be exact

If we do not type the command names exactly as shown, Unix will not execute them. For example:

$ DATE
DATE: not found
$

or perhaps:

$ Date
Date: not found
$

The error messages mean that there is no such command as DATE or Date. If your system does not echo the four arrow keys mentioned in the last section, try typing them before the date command; you should see this message:

$ date
^[[A^[[B^[[D^[[Cdate: not found
$

We will see later why Unix says not found instead of `no such command'.

Stopping a command

Stopping a command is easy - you hold down the "control" or "Ctrl" key and type a "C". The command is interrupted and the shell prompts you for the next command. Here nothing much is going on but we stop it anyway:

$ ^C
$

See how the control C is echoed.

Who else is logged in?

There is another command that consists of just one word that we can use right now. It tells us who is logged on to our Unix system. For example:

$ who
acsgae   ttyp2   Jun 16 09:03   (:ttyp0:S.1)
stgdc    ttyp3   Jun 16 16:47   (ho-5221-007.csv.)
acsgjm   ttypb   Jun 16 09:52   (hbp-a244-07.csv.)
cmsps    ttype   Jun 16 13:21   (pscott.cms.shu.a)
cmsps    ttyq4   Jun 16 15:15   (pscott.cms.shu.a)
acsgjm   ttyq5   Jun 16 15:16   (hbp-a244-07.csv.)
bjsoar   ttyq9   Jun 16 13:52   (hbp-a246-13.csv.)
$

The exact output varies from system to system but the first word on each line is the user's login code.

Multi-command lines

You can put several commands on one line if you put a semi-colon (;) between them. You can put spaces before or after the semi-colon if you like.

$ date ; cal ; date
Fri Jun 16 17:01:40 BST 1995
   June 1995
 S  M Tu  W Th  F  S
             1  2  3
 4  5  6  7  8  9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30

Fri Jun 16 17:01:40 BST 1995
$

As you see, there is nothing in the output to show that it was produced by three separate commands.

Multi-line commands

Unix commands can spread over several lines if needed. Here is an example:

$ da\
> te
Wed Mar  5 09:13:24 GMT 1997
$

The backslash (\) before the first newline tells Unix the rest of the command follows on the next line so the command is date not da. Notice that Unix uses a different prompt (>) for continuation lines. We have deliberately made Unix use its secondary prompt; often it happens accidentally when you use quotation marks and forget to pair them off properly.

Parameters

Most Unix commands need more information to specify what they are to do. This interchange between Unix and a user shows a command with extra information:

$ cal 11 1947
   November 1947
 S  M Tu  W Th  F  S
                   1
 2  3  4  5  6  7  8
 9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30
$

The name of the command must come first; the extra information is entered on the same line as the command, after the command itself. We call this information the command's parameters . So, in the example, 11 was the first parameter and 1947 was the second parameter to cal. There must be at least one space character between the command name and the first parameter. There must be at least one space character between each parameter and the next. By the way, cal 11 47 would produce different output from cal 11 1947. The 47 is taken to be 47AD!

Default behaviour

Notice that when it is given parameters, cal behaves differently to how it behaves without any. The behaviour without parameters is called the default behaviour. The authors of Unix take great pains to ensure the default behaviour of commands is both useful and sensible.

Effect of different number of parameters

This example shows that who also behaves differently with different numbers of parameters:

$ who am i
holly!cmsps    ttyp5   Jun 19 13:37      (pscott.cms.shu.a)
$

With two parameters, who tells you about yourself. The numbers of parameters is the only thing who is bothered about; it gives exactly the same output with are you!

The cal command also behaves differently with a different number of parameters. Try: cal 1947 to see the effect.

QUESTIONS

  1. pending

  2. pending

ANSWERS

  1. pending

  2. pending


Valid XHTML 1.0! Valid CSS!
http://homepages.shu.ac.uk/~cmsps/unix/intro.html
Last updated: Thursday 05 April 2012 at 17:45