Unit IV Introduction To Unix and Shell Programming
Unit IV Introduction To Unix and Shell Programming
Introduction to Unix
History of Unix OS
The development of UNIX system got started at Bell Labs by mainly Ken Thompson, Dennis
Ritchie
The first version of this operating system was written in assembly language
But later during 1973, Version 4 was written in C language
During the 1990s, the UNIX system started gaining popularity as the Linux distributions
developed
In 2000, Apple released its own UNIX system, known as Darwin, which later became
MacOS.
Components of Unix OS
Characteristics of Unix
Portability: This feature helps the user to change and compile the code on a new machine
Multi-tasking: more than one process to run at a time if one is running, other can also run
background
Pipes and Filters: which help in making of complex programs from simpler programs
Shell: The shell hides the intricate hardware details from the user
Extensive Library: has a support of extensive library which makes it a useful and relevant
Advantages of Unix
The main advantage of Unix is its portability which helps the user to run program on different
machine
UNIX makes minimum use of physical memory usage while running the various tasks
flawlessly
UNIX is capable to perform complex tasks with the minimal load and efficiently
It supports Hierarchical File system which helps for easy maintenance and efficiency
UNIX system is secure due to its strong server validation and authentication
Disadvantages of Unix
UNIX operating system is command line based which increases the difficulty for the casual
users to use it
It is meant for expert programmers who know command line commands very well
Documentation of various UNIX tools is hard to find
The commands used are cryptic, makes use of special characters which makes it difficult for
new users
The File
Files Category
/bin and /usr/bin – these are directories where all the commonly used Unix commands are
found
/etc – this directory contains the configuration files of the system
/dev – this directory contains all device files
/lib and /usr/lib – contains all library files in binary
/usr/include – contains standard header files used by C programs
/tmp – users are allowed to create temporary files
/var – the variable part of the file system
Unix Shell:
A Unix shell is a command-line interpreter or shell that provides a command line user interface
for Unix-like operating systems. The shell is both an interactive command language and a scripting
language, and is used by the operating system to control the execution of the system
using shell scripts.
A shell is a command-line interpreter and typical operations performed by shell scripts include file
manipulation, program execution, and printing text.
Shell is an environment in which we can run our commands, programs, and shell scripts.
There are different types of a shell, just as there are different typess of operating systems. Each type
of shell has its own set of recognized commands and functions.
A shell script is a computer program designed to be run by the Unix/Linux shell which could be one
of the following:
Shell Prompt
The prompt, $, which is called the command prompt, is issued by the shell. While the prompt is
displayed, we can type a command.
Shell Types:
C shell (csh)
TENEX/TOPS C shell (tcsh)
The original Unix shell was written in the mid-1970s by Stephen R. Bourne while he was at the
AT&T Bell Labs in New Jersey.
Bourne shell was the first shell to appear on Unix systems, thus it is referred to as "the shell".
Bourne shell is usually installed as /bin/sh on most versions of Unix. For this reason, it is the shell of
choice for writing scripts that can be used on different versions of Unix.
The Bourne shell program name is sh and its path in the Unix file system hierarchy is /bin/sh.
The C shell, csh, was modeled on the C programming language, including the control structures and
the expression grammar. It was written by Bill Joy as a graduate student at University of California,
Berkeley, and was widely distributed with BSD Unix.
The C shell also introduced many features for interactive work, including
the history and editing mechanisms, aliases, directory stacks, tilde notation, cdpath, job
control and path hashing.
Shell Scripts
The basic concept of a shell script is a list of commands, which are listed in the order of execution. A
good shell script will have comments, preceded by # sign, describing the steps.
Shell scripts and functions are both interpreted. This means they are not compiled.
A variable is a character string to which we assign a value. The value assigned could be a number,
text, filename, device, or any other type of data.
Variable Types
Following is the list of important environment variables. These variables are set and accessed as mentioned
below –
Variable Description
DISPLAY Contains the identifier for the display that X11 programs should use by default.
HOME Indicates the home directory of the current user: the default argument for the
cd built-in command.
IFS Indicates the Internal Field Separator that is used by the parser for word splitting
after expansion.
PATH Indicates the search path for commands. It is a colon-separated list of directories
in which the shell looks for commands.
RANDOM Generates a random integer between 0 and 32,767 each time it is referenced.
TZ Refers to Time zone. It can take values like GMT, AST, etc.
UID Expands to the numeric user ID of the current user, initialized at the shell startup.
The following table shows a number of special variables that can be used in your shell scripts −
Variable Description
$n These variables correspond to the arguments with which a script was invoked.
Here n is a positive decimal number corresponding to the position of an argument
(the first argument is $1, the second argument is $2, and so on).
$* All the arguments are double quoted. If a script receives two arguments, $* is
equivalent to $1 $2.
$@ All the arguments are individually double quoted. If a script receives two
arguments, $@ is equivalent to $1 $2.
$$ The process number of the current shell. For shell scripts, this is the process ID
under which they are executing.
Command-Line Arguments
The command-line arguments $1, $2, $3, ...$9 are positional parameters, with $0 pointing to the
actual command, program, shell script, or function and $1, $2, $3, ...$9 as the arguments to the
command.
There are special parameters that allow accessing all the command-line arguments at
once. $* and $@ both will act the same unless they are enclosed in double quotes, "".
Both the parameters specify the command-line arguments. However, the "$*" special parameter
takes the entire list as one argument with spaces between and the "$@" special parameter takes the
entire list and separates it into separate arguments.
Basic UNIX commands
Files:
ls: lists your files
ls-l: lists your files in 'long format', which contains lots of useful information, e.g. the exact
size of the file, who owns the file and who has the right to look at it, and when it was last
modified.
ls-a: lists all files, including the ones whose filenames begin in a dot, which you do not
always want to see.
There are many more options, for example to list files by size, by date, recursively etc.
more filename: shows the first part of a file, just as much as will fit on one screen. Just hit
the space bar to see more or q to quit.
emacs filename: is an editor that lets you create and edit a file.
mv filename1 filename2: moves a file (i.e. gives it a different name, or moves it into a
different directory.
rm filename: removes a file. It is wise to use the option rm i, which will ask you for
confirmation before actually deleting anything.
diff filename1 filename2: compares files, and shows where they differ
wc filename: tells you how many lines, words, and characters there are in a file
chmod options filename: lets you change the read, write, and execute permissions on your
files. The default is that only you can look at them and change them, but you may sometimes
want to change these permissions. For example, chmod o+r filename will make the file
readable for everyone, and chmod or filename will make it unreadable for others again.
File Compression:
gzip filename: compresses files, so that they take up much less space. Usually text files
compress to about half their original size, but it depends very much on the size of the file and
the nature of the contents. There are other tools for this purpose, too (e.g. compress), but gzip
usually gives the highest compression rate.
lpq: check out the printer queue, e.g. to get the number needed for removal, or to see how
many other files will be printed before yours will come out
lprm jobnumber: remove something from the printer queue. You can find the job number by
using lpq
genscript: converts plain text files into postscript for printing, and gives you some options
for formatting.
Directories:
Directories are used to group files together in a hierarchical structure.
cd dirname: change directory. You always start out in your 'home directory', and you can get
back there by typing 'cd' without arguments.
Finding things:
ff: find files anywhere on the system.
grep string filename(s): looks for the string in the files. This can be useful a lot of purposes,
e.g. finding the right file among many, figuring out which is the right version of something,
and even doing serious corpus work.
who: tells you who's logged on, and where they're coming from.
talk username: lets you have a (typed) conversation with another user write username lets
you exchange online messages with another user
elm: lets you send email messages to people around the world (and, of course, read them).
About your (electronic) self:
whoami: returns your username.
kill PID: kills (ends) the processes with the ID you gave.
du filename: shows the disk usage of the files and directories in filename
telnet hostname: also lets you connect to a remote host. Use rlogin whenever possible.
ftp hostname: lets you download files from a remote host which is set up as an ftpserver.
Miscellaneous tools:
webster word: looks up the word in an electronic version of Webster's dictionary and returns
the definition(s)
cal: shows a calendar of the current month. Use e.g., 'cal 11 2020' to get that for November
2020, or 'cal 2020' to get the whole year.
man commandname: shows you the manual page for the command
For any command information use the man command_name (man stands for manual)
Type man commandname on the terminal and get the information of that command
Shell Programming:
The shell programming language incorporate most of the features that most modern programming languages
offer. Shell program is series of Unix commands. Shell script can take input from user, file and output them on
screen.
Examples:
$ bash bar
$ sh bar
$ ./bar
Prog1: Write following shell script, save it, execute it and note down the it's output.
$ vi ginfo
#
#
# Script to print user information who currently login , current date & time
#
clear
echo "Hello $USER"
echo "Today is \c ";date
echo "Number of user login : \c" ; who | wc -l
echo "Calendar"
cal
exit 0
Shell Keywords:
Keywords are the words whose meaning has already been explained to the shell. Following is the list of
keywords available in the Bourne shell:
echo If until trap read Else case wait set fi
esac Eval unset while break Exec readonly do ulimit shift
done Exit umask export for return
Variables in Shell
To process our data/information, data must be kept in computers RAM memory. RAM memory is divided into
small locations, and each location had unique number called memory location/address, which is used to hold the
data. Programmer can give a unique name to this memory location/address called memory variable or variable
(Its a named storage location that may take different values, but only one at a time).
'value' is assigned to given 'variable name' and Value must be on right side = sign.
Example:
$ no=10# this is ok
$ 10=no# Error, NOT Ok, Value must be on right side of = sign.
To define variable called 'vech' having value Bus
$ vech=Bus
To define variable called n having value 10
$ n=10
Variable name must begin with Alphanumeric character or underscore character (_), followed by one or more
Alphanumeric character. For e.g. Valid shell variable are as follows
HOME, SYSTEM_VERSION, vech, no
echo Command
Use echo command to display text or value of variable.
Shell Arithmetic
Use to perform arithmetic operations. To carry out arithmetic operations we have to use the command expr
which is capable of evaluating arithmetic expression
Syntax:
expr op1 math-operator op2
Examples:
a = 10, b = 10
echo `expr $a + $b`
echo `expr $a – $b`
echo `expr $a \* $b`
echo `expr $a / $b`
echo `expr $a % $b`
on execute of this , get the following output
30, 10, 200, 2, 0
expr is capable of carrying out only integer arithmetic. To carry out arithmetic on real numbers it is necessary to
use bc command
a = 10.5, b = 3.5
Example:
c = `echo $a + $b`| bc
d = `echo $a – $b’| bc
e = echo $a \* $b |bc
- The sequence control instruction ensures that the instructions are executed in the same order in which
they appear in the program
- Decision and Case control instructions allow the computer to take a decision to which instruction is to
be executed next
- The Loop control instruction helps computer to execute a group of statements repeatedly
Syntax:
case value in Ex: echo “Enter a number from 1 to 3”
choice 1) read num
case $num in
do this
1) echo you entered 1
and this
;;
;; 2) echo you entered 2
choice 2) ;;
do this 3) echo you entered 3
and this ;;
;; *) echo I said 1 to 3
choice 3) ;;
do this esac
and this
;;
*)
do this
;;
esac
Loops: a loop involves repeating some portion of the program either a specified number of times or until a
particular condition is being satisfied. There are three loop statements.
(a) Using a for statement
(b) Using a while statement
(c) Using a until statement\
Syntax of while:
while control command
do
command1
command 2
done
Ex: count =1
while [$count –le 3]
do
echo “Enter the values of P, n, and r\c”
read P n r
si = `echo $P \* $n \* $r / 100 | bc`
echo Simple interest = Rs $si
count = `expr $count = 1`
done
There are minor differences between the working of while and until loops.
- The while loop executes till the exit status of the control command is true and terminates when the exit
status becomes false.
- Unlike this the until loop executes till the exit status of the control command is false and terminates
when this status becomes true
Ex: i = 1 Ex: i = 1
while [$i –le 10] while [$i –gt 10]
do do
echo $i echo $i
i = `expr $i + 1` i = `expr $i + 1`
done done
Output: 1 2 3 4 5 5 7 8 9 10 Output: 1 2 3 4 5 5 7 8 9 10
Logical Operators:
Shell allows usage of three logical operators while performing a test. These are;
(a) –a (read as AND)
(b) –o (read as OR
(c) ! (read as NOT)
Ex: The marks obtained by a student in 5 different subjects are input through the keyboard. Write a program to
calculate the division obtained by the student. The student gets a division as per the following rules:
Percentage above or equal to 60 – First division
Percentage between 45 and 59 – Second division
Percentage between 33 and 44 – Third division
Percentage less than 33 – Fail
Program:
echo “Enter marks in five subjects\c”
read m1 m2 m3 m4 m5
per = `expr \($m1 + $m2 + $m3 + $m4 + $m5\) / 5`
if [per –ge 60]
then
echo First division
elif [per –ge 45 –a per –lt 60]
then
echo Second division
elif [per –ge 33 –a per –lt 45]
then
echo Third division
else
echo Fail
fi
fi
fi
Shell Metacharacters:
Sometimes the metacharacters are also called regular expression. All the metacharacters can be classified as:
Type Metacharacters
Filename substitution ? * [….] [!...]
I/O Redirection < > >> << m> m>&n
Process execution ; ( ) & && ||
Quoting Metacharacters \ “” ‘‘ ``
Positional parameters $1 ….. $9
Special parameters $0 $* $@ $# $! $$ $-
Special parameters:
Metacharacters Meaning
$$ PID of current shell
$? Exit status of the last executed command
$! PID of last background process
$- Current shell settings
$# Total number of positional parameters
$0 Name of the command being executed
$* List of all shell arguments. Cant yield each argument separately
$@ Similar to $*, but yields each argument separately when enclosed in double quotes
Array Basics in Shell Scripting
An array is a systematic arrangement of the same type of data. But in Shell script Array is a variable which
contains multiple values may be of same type or different type since by default in shell script everything is
treated as a string. An array is zero-based ie indexing start with 0.
1. Indirect Declaration: In this declaration, assigned a value in a particular index of Array Variable. No
need to first declare. ARRAYNAME[INDEXNR]=value
2. Explicit Declaration: In this declaration, first declare array then assigned the values. declare -a
ARRAYNAME
3. Compound Assignment: In this assignment, we declare array with a bunch of values. We can add other
values later too. ARRAYNAME=(value1 value2 .... valueN)
echo ${ARRAYNAME[*]}
Output:
prakhar ankit 1 rishabh manish abhinav
prakhar ankit 1 rishabh manish abhinav
prakhar ankit 1 rishabh manish abhinav
prakhar ankit 1 rishabh manish abhinav