My ClickWorld.com

Your Ultimate Guide to Programming.

What is a shell?

The shell is the layer around the Unix operating system.The shell’s job is to translate the user’s command lines into operating system instructions. For example, consider this command:

sort –n phonelist > phonelist.sorted

This means, sort the lines in the phonelist file in numeric order, and put the result in the file phonelist.sorted.

Remember that the shell itself is not Unix – just the user interface to it. Unix is one of the first operating systems to make the user interface independent of the operating system.

When you log on, the UNIX system starts running a program that acts as an interface between you and the UNIX kernel. This program, called the UNIX shell, executes the commands that you have types on the keyboards. When a shell starts running, it gives you a prompts and waits for your commands. When you type a command and press , the shell interprets your command and executes it. If you type a nonexistent command, the shell tells you this, then redisplays the prompt and waits for you to type the next command. Because the primary purpose of the shell is to interpret your commands, it is also known as the UNIX command interpreter.

For example:
[academ] $ ls /common/hart/unix
/common/hart/unix not found --> error message from the shell
[academ] $ psd
ksh: psd: not found --> error message from shell

A shell command can be built-in or external. The shell has the code for executing built-in command, but the code for an external command is in a file. To execute an external command, the shell searches several directories, one by one, to locate the file that contains the code for the command. If the file is found, it is executed if it contains code (binary or shell script). Then names of the directories that the shell searches to locates the file for an external command form what is known as the search path. The search path is stored in a shell variable called PATH. You can change the search path for your shell by adding new directory names in PATH or by deleting existing directory names from it.

Directories:

Let’s review the most important concepts about directories. The fact that directories can contain other directories leads to a hierarchical structure, more popularly known as a tree, for all files on a Unix System.

All files can be named by expressing their location on the system relative to the root directory; such names are built by listing all of the directories names beginning at the root, separated by slashes (/), followed by the file’s name. This is an absolute pathname. For Example to print the tempfile.txt in the common area:
enq –Pbtc308l1 /common/cis/hart/unix/tempfile.txt

The Working Directory

Of course, it is annoying to have to use the full pathname (absolute pathname) whenever you need to specify a file. So there is also the concept of the working directory (aka the current directory), which is the directory you are “in”. If you give a pathname with no leading slash, then the location of the file is worked out relative to the working directory. Such pathnames are called relative pathnames; you’ll use them much more often than full pathnames. For Example: The working directory is /common/cis/hart; you want to print the tempfile.txt in the common area.
enq –Pbtc308l1 unix/tempfile.txt

Tilde Notation or HOME shell variable

The Korn shell has a way of abbreviating the home directories – the tilde (~) – stands for your home directory - /home/userid . To list the contents of the tempfile.txt located in the ciss-102 directory you can use the following command:
cat ~/ciss-102/tempfile.txt.

A shell variable is a name that has a value associated with it. The shell has two types of variables – local and environment. Environment variables are available to use (or known) in the user’s total environment. Meaning they are known in programs, in subdirectories, etc. The Korn Shell keeps track of several environment variables. By convention, environment variables names are UPPER CASE.

shell Options

The KornShell lets you set options to control its behavior. To do this you use the set command. This allows you to change the shell’s behavior.

Option Description

emacs allows command line editing using the basic emacs commands
noclobber don’t allow output redirection (>) to clobber (or overwrite) an existing file
To set the options on; use the set –o. To turn the option off à set +o.
PLEASE NOTE: -o (letter o)
set -o emacs à allows command line editing
This allows you to use all the emacs commands (^b, ^f, ^a, ^e, etc to edit the command line). Now when you type a command and you notice that you have a typo you can use emacs editing commands to update – NO MORE RETYPING!

Try It.
[academ]$ set –o emacs
Now test it:
[academ]$ ls /common/hart/unix
You get the following error message from the shell:
/common/hart/unix not found
Type in ^ p (this displays the previous command)
Now use the ^b to back over and insert the missing subdirectory
[academ]$ ls /common/cis/hart/unix
Please note: This only lets you edit the command line for this session only!
To make it permanent you need to put the code in the environment file.

Shell Start-up Files

The actions of each shell, the mechanics of how it executes commands and programs, how it handles the command and program I/O, and how it is programmed, are affected by the setting of certain environment variables. This is the file, where you change how your shell “looks and feels”. Each time you login to the Unix Box – a file .profile which MUST be located in the home directory, is executed. The .profile contains the initial settings of important environment variables for eh shell and some other utilities. The .profile sets the shell’s default options. If you want to change how the shell behaves you make those changes to the environment file.

The environment file that we will use is the .kshrc file.
ksh à Korn Shell
rc à run commands

export: Creating Environment Variables

All shells use environment variables to manage individual preferences. The Korn Shell using the export statement to change the value of an environment variable, so it will be kept when the session ends.

[academ]$ PS1=’This is my new prompt> ’
Changes the prompt for this session only, when you log out and log back in your prompt will be
[academ]$ To make the changes permanent you need to export the variable in your .kshrc file.
This unit’s assignment will show you how to update your environment.

Google