Lecture 20 - UNIX 4


Home > Lectures > Lecture20

Objectives

Contents


The upcoming tutorial  is in the Unix Lab B1.11

Search Path

Unix is a highly modular system. The shell (csh)  looks at each command given and determines the program to be run. Normally, the command name is simply the name of a program. This means, that if we want a new command, we simply write a program to do the job, and then install it in an appropriate place (directory) where it is generally accessible. The shell searches your path to find programs when a command is given. Typically, the search path includes the following (where system binary files - programs and commands - are stored:
   /bin, /usr/bin, /usr/local/bin, ~/bin, .
plus some others which may be needed for special purposes. (The ~/ points to your home directory.)
If the file is not found within the path then Unix reports: 'Command not found'

The path is stored in and environment variable called PATH.
Environmental variables are similar to program variables and are stored within the OS. Hence, all applications can access them.
You can display your path by typing the command;   echo $PATH.

Setup Files

Setup files are files, that contain a set of Unix commands, one command per line, stored together within a text file that are executed when some particular event occurs, like logging on. These files are usually kept in your home directory and, although some of them are created and updated by applications and the system, they can also be edited with any text editor. Warning: take extreme care in editing these files. The file names commence with a dot ".". Examples of setup files are: .login, .forward, .cshrc.

By default the ls command does not list files with names starting with a dot ".". To view these files use the following commands use the -a switch. The "a" switch stands for all files.

Format

    ls -a file-names
    ls -al file-names

Examples

    ls -a
    ls -al

Some commands used within setup files are (they can also be executed directly from the command line):

Some special setup files are:

.login

Executed when you login. Contains commands to:

.cshrc

.forward

Other Unix Special Files

/etc/passwd

/etc/group

/etc/hosts

/bin

Standard Input and Output

A fundamental principle of the Unix OS is that output of one program can be used as input for another program.
From the command line the operation of the input and output can also be redirected through the use of the redirection characters <, <<, >, >> and piping using the vertical bar character '|' where the standard output of one command becomes the standard input of another.
eg:
 ls                 (Output is directed to stdout - i.e. the screen )
 ls > afile.txt     (Output is sent to the file afile.txt
                    - everything in afile.txt is overwritten)
 ls >> afile.txt    (Output is appended to the end of afile.txt If it already
                                         exists, otherwise a new file is created)

 cat > afile.txt    (Copies from stdin (the keyboard) to the file afile.txt
                    until the End-Of-File (EOF) is reached - a CTRL-D)

Input is usually from the keyboard, but it can be redirected also, really only used for programs that have a set input sequence, such as a list of numbers that can easily be stored in a file. eg:
 progName < afile.txt

 ls -l | less   Output from ls is sent as input into the program less.
                less simply displays a sequence of characters one screen at a time)
 

Other commands often used in a pipe sequence are lp, lpr, sort and grep. eg:

Grep

grep is for searching files. grep stands for Global Regular Expression Print.

Format

grep  [-ciln]  search-pattern  files-to-search

Options/Switches

-c  Only count the number of matching lines the search pattern was found on
-i  Ignore case (case insensitive search)
-l  Only list the names of the files that contain the search pattern
-n List the line numbers the search pattern was found on

Examples

grep hello *.java          (Search for all the java programs 
                            with the string hello in them)
grep smith /etc/passwd     (Search for all occurrences of 
                            "smith" in the password file)
grep -ic 'Prog Env' * | less  (Search for all occurrences of "Prog Env"
                               in the any file in this directory using a
                               case insensitive search and only counting 
                               occurrences)

The vi Editor

The vi (pronounced Vee I) editor is a character based, full screen, text editor. It uses its own set of commands (you can get a summary from here - it is printable postscript file which you must save to Unix and then print. In Firefox, right click on the link and select Save Link As..., click Ok to save the file, then print it).

Vi is a moded editor. This means that it operates in different modes. When you first start it, it is in command mode. Here you can give keystroke commands to move around the file, delete lines, copy, paste, and the like. When you give one of the text insertion commands, such as i, you move to insert mode where everything you type goes into the file. You leave insert mode by pressing the ESCAPE key, which returns you to command mode. There is also a bottom line mode which allows more complex search and replace operations. The commands are case sensitive e.g. a is different to A. Once you are familiar with the commands, vi is extremely quick to use, particularly if  if you are a touch typist.

vi has a number of advantages:

Some useful vi commands:

To run

Modes

Files

Moving Around

Editing Text

Exit

Note: When you complete an editing session, you save the file and exit using ZZ

The Pico Editor

pico is a basic full screen text editor that doesn't require a graphical environment to run. This can be useful if you are logging in from a system that doesn't have a graphical environment. pico uses special keystrokes to perform commands, such as Ctrl+X to exit, Ctrl+G for help. It doesn't use the mouse.

Some commands are shown at the bottom of the window. Many commands use the control key (Ctrl).

To Run

Files

Moving Around

Editing Text

Miscellaneous

Exit

Editors

Unix is an old system, which has been updated constantly. There are many available editors. Some include:

Executing (or Running) a Program

Discuss Assignment 3


Further Reading


Previous Lecture | Lecture Index | Next Lecture | Tutorial
Last modified 08-May-2006 by Tim Whitfort.
Copyright © 2003-2006 Brian Retallick & Tim Whitfort