Unix Fundamentals Chapter 01
Unix Fundamentals Chapter 01
______________________________________________________________________________
Chapter One
Command Action
mv file1 file2 renames file2 to file2. If file2 already exists, its contents
will be overwritten with the contents of file1.
History:
UNIX is a robust multi-user operating system that was created with the future in mind.
Before its creation, most operating systems were written in assembler, which is a low-level language
that produces vendor specific instructions. The resulting operating systems were proprietary, and
left the consumer with no choice but to purchase expensive hardware and software on which to run
their enterprise. The proprietary nature of the systems also left them unable to communicate with
other vendor‘s systems without additional software. The proverbial left hand and right hand weren‘t
even on the same planet, much less able to communicate in the same language.
UNIX changed everything. It was originally created by Dennis Ritchie and Ken Thompson of
AT&T. They used ―C‖ to write the majority of the operating system. ―C‖ is a high level language
that is extremely portable. It is therefore relatively easy to port UNIX and its applications from one
hardware vendor‘s system to another, which explains why you can find UNIX on pretty much any
available hardware (even a mainframe).
They also included in UNIX many powerful tools that, when combined with each other, give the user
a great deal amount of control over their programs and environment. It is much more than a simple
file system and process control center. Unfortunately, it is the robust nature of UNIX and its seem-
ing complexity that make mastering it a daunting task. Unfortunately, it was written by geeks to be
used by geeks – so there isn‘t much fluff to help you along when you are first getting started.
AT&T‘s version of UNIX (System V) was distributed to many academic institutions for a small fee,
but they were not able to provide support for their distribution. The University of California at
Berkeley (UCB) created a UNIX of their own based on AT&T‘s original work. This is commonly
referred to as BSD UNIX. UCB added some important features to the original work by AT&T.
They created the vi editor, as well as provided the TCP/IP networking protocol with their version of
UNIX.
As time passed, more and more hardware vendors took one of the two original versions of UNIX and
modified it to be shipped with their own hardware. A tweak here and a tweak there, and soon, the
UNIX culture had become defined by the incompatibilities of each vendor‘s offering.
There have been several attempts to standardize UNIX in recent years. Some of these include
AT&T‘s System V Interface Definition (SVID), POSIX (the Portable Operating System Interface to
Conputer Environments), as well as the Single UNIX Specification.
Linux is a free version of UNIX that, among other systems, can be run on Personal Computers. It is
distributed under the GNU Public License that requires all Linux source code be made available to
the public. Red Hat, SuSE and Mandrake are some of the most well known Linux distributions.
There are also free databases to run on Linux, such as MySQL. It is a relatively inexpensive endea-
vor to setup a Linux machine as a Web Server with access to a database on a low cost Personal Com-
puter. Support is getting easier to come by as well, as the Linux community is one that can easily be
14 UNIX Fundamentals Workshop
______________________________________________________________________________
accessed via various newsgroups on the Internet. All of these advantages combined are why Linux
is being used more and more by businesses around the world.
Note: You can easily download a Linux distribution and install it on your PC. Most distributions
will detect a Windows installation, and automate the process for you that creates a dual-boot machine.
UNIX Fundamentals Workshop 15
______________________________________________________________________________
Architecture
The UNIX kernel is central to all operations and services available. Users and their processes do not
actually have access to the machine‘s hardware directly, but must interact with a system call interface
(API) that the kernel provides. The kernel has many jobs – including, but not limited to:
In order to communicate with the kernel, a user requires an interpreter known as the shell process.
The shell is the interface between the UNIX kernel and the user. There is only one kernel process on
a UNIX system, but it keeps up with all shell and system processes.
16 UNIX Fundamentals Workshop
______________________________________________________________________________
Popular Shells
TC tcsh Public domain shell. Provides the features of C shell with emacs-
style editing of command line.
Korn ksh Written by David Korn of Bell labs and now standard shell
provided on UNIX systems. Delivers all features of C and
TC shells with a shell programming language similar to that
of Bourne. Default shell used at SAS.
Bourne bash Public domain shell written by the Free Software Foundation
Again under their GNU initiative. Intended as full implementation of
SHell IEEE Posix Shell and Tools specification. Widely used within
or Bash the academic community. Provides all interactive features of C
and Korn. Its programming language is compatible with Bourne.
Note: The majority of examples in this book are written to be used with the Korn Shell (/bin/ksh).
UNIX Fundamentals Workshop 17
______________________________________________________________________________
Shell Comparisons
Terminal Windows
The shell runs inside a terminal window, an application that accepts user-entered input for the shell.
Terminal windows available at SAS include Xterm and Jterm. For the purposes of this class, we will
be using the Xterm, as that is what will be available at customer sites.
UNIX Fundamentals Workshop 19
______________________________________________________________________________
Diagram
Terminal Window
Shell
Kernel
20 UNIX Fundamentals Workshop
______________________________________________________________________________
Xterm
Xterm
The terminal-window application Xterm is typically available on all UNIX systems. The Xterm ap-
plication can be invoked from Windows using special emulation software called Exceed. The Exceed
application Xstart invokes UNIX applications, using either desktop icons or the Start Menu.
UNIX Fundamentals Workshop 21
______________________________________________________________________________
-or-
4. Choose OK.
Note: Use the Tab key to change fields in the Xstart dialog box.
Note: If you already have an xterm window open, issuing the xterm & command also opens an
additional Xterm window.
22 UNIX Fundamentals Workshop
______________________________________________________________________________
Diagram
Windows
Exceed
Xstart
Xterm
Korn Shell
Kernel
UNIX Fundamentals Workshop 23
______________________________________________________________________________
Diagram
Windows
Exceed
Xstart
X-Windows
X-Term
Korn Shell
Kernel
24 UNIX Fundamentals Workshop
______________________________________________________________________________
Larger Fonts
To open an Xterm or Jterm window with larger-sized fonts, add the following X-resource option
-fn some-font
to the Xterm or Jterm command, either entered at shell prompt or via Exceed Xstart window.
Examples
Xstart
Window
Finding Fonts
To sample a particular font, enter the xfd -fn some-font & command.
UNIX Fundamentals Workshop 25
______________________________________________________________________________
Exceed
The Windows application Exceed emulates the X-windows UNIX environment. Exceed tools availa-
ble by selecting Start Programs Hummingbird Connectivity v7.1 from the Taskbar include:
Application Description
Client Wizard Easily enter information needed to start a UNIX program (or X client) and
optionally create both startup files and corresponding shortcuts.
Launch Pad Launch applications and manage your program windows. Also includes a
Virtual Desktop that effectively increases the size of your Windows desk-
top display area.
Ping Send Internet Control Message Protocol (ICMP) echo request packets to
network host.
Remote Tools Execute commands on and copy files to another computer on the network,
using the RSH and REXEC protocols.
Xstart Create and install icons, to easily start frequently used UNIX programs,
with username and password preset, if desired.
26 UNIX Fundamentals Workshop
______________________________________________________________________________
Exceed also enables easy marking and pasting of text between application windows on UNIX and
Windows.
Note: If using a two-button mouse, press both buttons simultaneously to emulate middle mouse-
button behavior.
On a conceptual level, when you login to a UNIX machine, your ―identity‖ consists of three things: a
userid, a password, and an associated group. Anything you do will be associated with your userid,
commonly referred to as UID. If you create a file, it will be owned by you. If you send mail to a
friend, you will be logged as the sender. If you run a SAS/SHARE server, that process will be
owned by you.
For the purposes of this class, we will be using HP 10.20. An example (internal to SAS) login fol-
lows:
Your UNIX system is now running a shell on your behalf which is waiting to run commands or utili-
ties. This shell process works between you (the user) and the operating system. We‘ll talk more
about shells later, but for now it is just important that you realize you are already interacting with a
process.
When you are ready to exit the UNIX system, execute one of the following commands:
or
One of those should return you to a login prompt and leave the system ready for another user.
If you are using Exceed, your xterm window will close when you logout.
28 UNIX Fundamentals Workshop
______________________________________________________________________________
Terminal Settings
Everything you enter on a UNIX command line is dependent on your terminal‘s properties. Have you
ever logged into a UNIX machine and had your [backspace] or [delete] keys not work as you would
expect them to? If so, the stty command can come to your rescue.
Display all (-a) current stty settings for your terminal (shell):
In the above output, the ―kill‖ command is set to the ―@‖ key. This is a common setting, though not
a desirable one. Effectively, each time I type the ―@‖ sign within a command – such as in an email
address or an oracle database alias – only the characters that appear AFTER the ―@‖ will be sent to
the shell . There are ways of escaping it, of course, which we will find out about later. For now, let‘s
change it to Control-K:
Processes:
Now, login to your UNIX system again. Once you get logged into the system, execute the following
command:
login012> ps Enter
This ps command, when given without any arguments, will list all processes currently running on the
system that are owned by your UID:
login012> ps Enter
PID TTY TIME CMD
28164 pts/2 0:00 ksh
Every process running on UNIX has an associated Process Identification number associated with it,
commonly referred to as the PID. The PID of the only process I am currently running is 28164. The
command (CMD) running under that PID is ksh, which is the Korn Shell process I am interacting
with.
30 UNIX Fundamentals Workshop
______________________________________________________________________________
Processes (cont’d):
To get an idea of how many processes are running on the UNIX machine by all users, execute the fol-
lowing command:
Use the who command to list all users that are currently logged into the system:
Depending on the flavor of UNIX you are using, you can enter the whoami or who am i com-
mand to tell you the current userid (UID) you are using (This can be especially helpful if you have
access to privileged accounts and need to make sure you are running as the proper userid before start-
ing a server process).
Print the name of the current machine on which you are logged in (useful when the machine name is
not listed as part of your command prompt):
Files:
There are many ways you can create a file in UNIX. Let‘s create a simple file using the echo com-
mand. The cd without any arguments will make sure you are in your userid‘s home directory when
you begin this exercise:
login012> cd Enter
login012> echo ―this is my first file‖ > first_file Enter
The echo command is typically used to ―echo‖, or display, something to the screen. We have used
the ―>‖ symbol to redirect the output of the echo command to a file (don‘t worry about the redirec-
tion symbol right now…we‘ll get to that later). For now, it is just important that you realize you
have created a file.
login012> ls Enter
first_file
Create a backup copy of your file with the cp (copy) command, then use ls to list the contents of the
current directory:
login012> ls Enter
first_file
first_file.bak
34 UNIX Fundamentals Workshop
______________________________________________________________________________
Now, what if you decide you want to change the name of your backup filenames from *.bak to *.orig.
You can easily accomplish this with the mv (move) command:
login012> ls Enter
first_file
first_file.orig
login012> ls Enter
first_file
(rm behaves a little differently when removing a directory. We will get into that a little later).
UNIX Fundamentals Workshop 35
______________________________________________________________________________
Directories:
By now, you have probably noticed that UNIX systems don‘t really tell you much when things work.
For example, when you issue the above mkdir command, nothing is displayed on your terminal to
offer reassurance that it actually worked properly and the directory was indeed created. You know it
was successful simply because your prompt returns and no error was generated. UNIX will always
tell you when something is wrong, but rarely when something is right. Remember, UNIX was
created by programmers for programmers, so there is not a lot of superfluous information provided.
Chalk it up to just being the way it is for now. This apparent lack of information will come in handy
later when we cover filtering and redirection. For now, you can use the ls –F command to check that
the command worked and your directory was created:
login012> ls –F Enter
first_file
tempdir/
.
.
The –F option on the ls command will add a trailing slash ―/‖ to any entry that is a directory. As
you can see in the above output, first_file is a regular file and tempdir is a directory.
Change into your tempdir directory with the cd command and list the contents with ls:
login012> ls Enter
first_file
36 UNIX Fundamentals Workshop
______________________________________________________________________________
Scripts:
Let‘s create a very simple script in your tempdir directory using the echo command:
This has created a file named test.sh with the contents being the who command.
Run your test.sh file with the following command. You should see output similar to the follow-
ing:
Cleanup:
Now, let‘s cleanup the files and directories we have created. First, you will need to remove the files
in your tempdir directory. While in that directory, execute the following commands:
Now, use the cd command to change to the parent directory (the ―..‖ moves you up one level in the
directory structure):
login012> cd .. Enter
You may wonder why we didn‘t just remove the directory first, since certainly that would have re-
moved the contents of the directory as well. Well, rmdir is a funny command in that it requires a
directory to be empty before it can be removed. Of course, as with all things UNIX, there is a way
around this; you can use the rm command with the –rf options. We‘ll cover that in more depth lat-
er.