0% found this document useful (0 votes)
4 views30 pages

Introduction to Unix Shell Programming

The document provides an overview of the Unix operating system, detailing its architecture, features, and command structure. It explains the roles of the kernel and shell, highlights Unix's multiuser and multitasking capabilities, and introduces various Unix commands and environment variables. Additionally, it covers the vi editor, shell scripting, and pattern matching techniques used in Unix.

Uploaded by

eramansharma438
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views30 pages

Introduction to Unix Shell Programming

The document provides an overview of the Unix operating system, detailing its architecture, features, and command structure. It explains the roles of the kernel and shell, highlights Unix's multiuser and multitasking capabilities, and introduces various Unix commands and environment variables. Additionally, it covers the vi editor, shell scripting, and pattern matching techniques used in Unix.

Uploaded by

eramansharma438
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

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
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
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
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
General Purpose Utilities
■ cal
■ date
■ echo
■ bc
■ ps
■ passwd
■ PATH
■ who
■ tput clear
■ ls (ls>list)
■ wc(wc|list)
■ uname
■ Pwd
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.
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.
The File System
■Ordinary File
■Directory File
■Device File
■ /
■ ├── bin
■ │ ├── bash
■ │ ├── ls
■ │ └── cp
■ ├── boot
■ │ └── vmlinuz
■ ├── etc
■ │ ├── passwd
■ │ ├── fstab
■ │ └── hosts
■ ├── home
■ │ ├── user1
■ │ │ ├── documents
■ │ │ └── pictures
■ │ └── user2
■ │ ├── downloads
■ │ └── music
■ ├── lib
■ │ ├── [Link]
■ │ └── [Link]
■ ├── tmp
■ ├── usr
■ │ ├── bin
■ │ ├── lib
■ │ └── share
•/ (Root Directory): The starting point of the file system.
•/bin: Contains essential binaries (commands) required for system ope
•/etc: Configuration files for the system.
•/home: User home directories where personal files are stored.
•/lib: Shared libraries used by executable files.
•/tmp: Temporary files created by applications.
•/dev: contains all device files.
■mkdir
■rmdir
■cd
■pwd
■Absoulte Pathname:
■Relative Pathname:
Basic File attributes
■dr-xr-xr-x 2 kumar metal 128 jun 18
22:41 note
■-r-xr-xr-x 2 kumar metal 128 jun 18 22:41
progs

Chmod: changing File permissions:


Relative permission: chmod category
operation permission filename
Absolute Permission: r=4, w=2,x=1
■Chown: chown owner filename
■Chgrp:
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
Pattern Matching
■The meta characters that are used
to construct patterns are called wild
cards.
■* : any no of characters including
none
■?: single character
■??: two characters
■[ijk]: single character either I j or k
■[x-z]: single character in the range
x to z
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
#!/bin/bash
# Prompt the user for the first number
echo "Enter the first number: "
read num1
# Prompt the user for the second number
echo "Enter the second number: "
read num2
# Perform the addition
sum=$((num1 + num2))
# Display the result
echo "The sum of $num1 and $num2 is: $sum“

■ Shell script to display the current working directory


■ Shell script to change the password

You might also like