Unix Shell Programming
Unit-1: Overview of Unix
Introduction
◼ UNIX is an operating system.
◼ An operating system is a set of programs that
act as a link between the computer and the user.
◼ The operating system (OS) manages the
resources of a computer.
◼ Examples of computer resources are: CPU,
RAM, disk memory, printers, displays,
keyboard, etc
- The interface provided by the OS can
either be a Graphical User Interface- GUI
or a Command Line Interface- CLI.
Unix OS
◼ Unix was originally developed in 1969 by a group
of AT&T employees Ken Thompson, Dennis
Ritchie, Douglas McIlroy, and Joe Ossanna at Bell
Labs.
◼ There are various Unix variants available in the
market.
◼ Solaris Unix, AIX, HP Unix and BSD are a few
examples.
◼ Linux is also a flavor of Unix which is freely
available.
Unix Architecture
◼ Unix follows a modular architecture, in which, the
functionalities are divided between the two modules:
Kernel and Shell. This is division of labour.
◼ The kernel interacts with the machine hardware and
the shell interacts with the user.
◼ Kernel: It is a collection of functions written in C
language. It is loaded into the memory when the
system is booted.
◼ It performs memory management, process management
and file management.
◼ Shell: The shell is the command interpreter. It
takes the commands given by the user, interprets
them and directs them to the kernel for further
processing.
◼ There are different types of shell- Bourne shell
(sh), C shell (csh), Korn shell (ksh), Bourne
Again Shell (bash)
Features of Unix
1) Multiuser
- Multiple users can access the system by
connecting to points known as terminals.
- Several users can run multiple programs
simultaneously on one system.
2) Multitasking
The kernel is designed to handle a user's multiple
needs.
In a multitasking environment, a user sees one
job running in the foreground; the rest run in the
background.
3) Pattern Matching
UNIX has very sophisticated pattern matching
features.
Regular Expressions describe a pattern to
match, a sequence of characters, not words, within a
line of text.
4) Portable
UNIX can be installed on many hardware
platforms.
Unix operating system is written in C language,
hence it is more portable than other operating
systems.
5) UNIX Toolkit
UNIX offers facility to add and remove many
applications as and when required.
Tools include
→ general purpose tools
→ text manipulation tools
→ compilers/interpreters
→ networked applications and
→ system administration tools
6) Programming Facility
The UNIX shell is also a programming
language; it was designed for programmer, not for
end user.
It has all the necessary ingredients, like control
structures, loops and variables, that establish powerful
programming language.
This features are used to design shell scripts –
programs that can also invoke UNIX commands.
Many of the system's functions can be
controlled and automated by using these shell scripts.
7) Documentation
The principal on-line help facility available is
the man command, which remains the most
important references for commands and their
configuration files.
Apart from the man documentation, there's a
vast ocean of UNIX resources available on the
Internet.
Command Structure
◼ UNIX commands take the following general form:
verb [options] [arguments]
◼ where verb is the command name that can take a
set of optional options and one or more optional
arguments.
◼ Commands, options and arguments have to be
separated by spaces or tabs to enable the shell to
interpret them as words.
◼ The shell compresses multiple occurrences of
whitespace into a single whitespace.
Internal and External Commands
◼ Some commands are implemented as part of the
shell itself rather than separate executable files. Such
commands that are built-in are called internal
commands.
◼ If the command (file) has an independence
existence in the /bin directory, it is called external
command.
General Purpose Utilities
◼ cal
◼ date
◼ echo
◼ bc
◼ script
◼ passwd
◼ path
◼ who
◼ uname
◼ pwd
◼ mkdir, cd, rmdir
Important Environment Variables
◼ HOME - your home directory.
◼ USER and LOGNAME - your login ID.
◼ HOSTNAME - the name of the host computer.
◼ PWD - the current working directory.
◼ PATH - a list of directories in which to look for executable
commands.
◼ Certain applications and commands may communicate with the
shell and reference the environment variables that it maintains.
◼ For example, it seems that frm and nfrm seem not to work if $MAIL is
not defined. frm and nfrm are commands to list the contents of your
inbox without logging into pine.
cat command
Use Case Command Example Description/Output
Prints the content of [Link]
Display a file cat [Link]
on screen.
Display multiple files Shows contents of both files
cat [Link] [Link]
together one after another.
cat > [Link] (type text, Creates a file and saves typed
Create a new file
then Ctrl+D) input.
cat >> [Link] (type text, Adds new text to the end of
Append to an existing file
then Ctrl+D) a file.
Combine files into a new cat [Link] [Link] > Merges contents of files into
one [Link] a single file.
Adds line numbers to every
Number all output lines cat -n [Link]
line.
Number only non-empty Numbers only non-blank
cat -b [Link]
lines lines.
The vi Editor
◼ The default editor that comes with the UNIX
operating system is called vi (visual editor).
Using vi editor, we can edit an existing file or
create a new file from scratch. we can also use
this editor to just read a text file.
◼ Open vi editor
vi file_name
vi: Modes
Shell scripts
◼ A shell script contains a list of commands which
have to be executed regularly.
◼ Shell script is also known as shell program.
◼ The user can execute the shell script itself to
execute commands in it.
◼ The extension is -- .sh
◼ To execute use the command:
sh [Link]
Sample scripts
#! /bin/sh
echo “Welcome to Shell Programming”
echo “Today’s date : `date`”
echo “My Shell :$SHELL”
◼ Shell script to add 2 numbers
◼ Shell script to display the current working
directory
◼ Shell script to change the password