OS Lab Manual
OS Lab Manual
Bill joy had written standard editor of Unix (vi) and very popular shell. (c
shell ).
Another version of UNIX by AT&T was SVR4 (System V Release 4)
features a special directory where we find all the Berkley utilities. Sun Microsystem used the BSD system as a foundation for developing their own brand of
UNIX (Sun OS). Its major contribution was Network File System (NFS). Today
this version of Unix is known as SOLARIS and is SVR4 based. Microsoft was the
1st to run Unix on a pc with 640KB of memory. They called their product XENIX
that was based on earlier version of AT&T but with some BSD borrowed utilities.
XENIX was later sold off to SCO (Santa Cruz Operation). SCO Unix now offers
two major flavors: SCO Open Server Release 5 and SCO Unix-ware 7; the later is
SVR4-Compliant.
Linux:- Linus Torvalds is the father of Linux. Linux is distributed under the GNU
general public license which makes it mandatory for developers and sellers to
make the source code public.
Multi-User:
In Unix, the resources are actually shared between all users. It can be a memory,
CPU or Hard-disk.
The computer breaks up a unit of time into several segments and each user
is allotted a segment, so at any point of time, the machine will be doing the job of
a single user. The moment the allotted time expires; the next job that is stored in
the queue is taken up and executed.
The process goes on till the clock has turned a full circle and the first user job is
taken up once again. Thus the kernel does several jobs in one second.
Multi-tasking:
In UNIX a single user can also run multiple tasks simultaneously. You can switch
between processes.
You can run background and foreground processes simultaneously, suspend a
process or even kill them. This is done by running one job normally and the other
in the background. Processes which are time consuming and non-interactive in
nature can be done in the background while we can continue using the system to
do other work. An ampersand (&) placed at the end of command line does this
task.
Ex:- sort f1 &
936
You will be returned the $ prompt immediately .You will be also given a no which
represents the job. In this situation, you can another command.
Security:
In UNIX, all application programs runs in shell. The entire information (like harddisk, mouse, printer etc) is stored in files. When UNIX is booted all the necessary
information are written into respective files.
These files are created at booting time only (in Unix there is no executable files)
So, any application program (even a virus) cannot directly affect the hardware.
Hence, Unix systems are more secured and powerful than windows.
Pattern Matching:
It is the pattern matching feature of Unix that makes it so attractive to the user.
*, ? (wild card characters) used by the system to indicate the expression can match
a number of filenames.
ls l c* this command displays all the files starting with c.
some of the most advanced and useful tools also use a special expression called
regular expression.
Programming Facility:
The Unix shell programming language has all the necessary ingredients like
control structure, loop, if-else statement and variable etc. that establish it as a
programming language in its own right.
The shell simplifies a number of things that are impossible in other languages. A
shell script contains a sequence of commands to be executed with a single
filename.
There are different types of shell available. They are BOURNE shell, C shell,
KORNE shell.
Pipes:
Simple programs can be developed for complex operation with minimum effort
such as pipes. This reduces the necessity of writing new programs for the complex
operation.
Ex:- who | wc -l
HW
Kernel
Shell
Fig: Structure of Unix
Kernel:- Kernel forms the core of the Unix operating system. This interacts with
the hardware. It is loaded into the memory when a system is booted .Its functions
are
Managing the system resources
Allocating time for different users and processes
Deciding process priorities and performing them.
Since it isolates itself from user programs, it can be moved across different
systems. Kernel does not interact with the user directly .Instead, it starts up a
separate file called Unix Shell.
Shell:-Shell forms the interface between the kernel and the user.
bin
date,cat,who,ls
dev
etc
usr
unix
lib
tmp
bin
Command Name: ls
Syntax: ls
Description: it displays the files in the current directory
Example: 1. ls
jkn rajni kumar
2. ls a* ( display the files starting with letter a )
ab
acc
avishek
awc
3. ls l (display all files in detailed format)
_rw_ _ r _ _ r 1 cse 020 47 Feb 23 :
12 : 35
--------------------------------------------------------------------------------------------------------------------
Command Name: wc
Syntax: wc option filename
Description: It displays the number of lines/words/characters in a file
Example: 1. wc lwc a
3 7 52
2. wc l a
3
3. wc w a
7
4. wc c a
52
--------------------------------------------------------------------------------------------------------------------
Command Name: nl
Syntax: nl filename
Description: It is used to provide line no to a file.
Example: nl file1
--------------------------------------------------------------------------------------------------------------------
india
pak
england
cat > file2
india
china
japan
Command Name: cmp
Syntax: cmp file1 file2
Description: It is used to compare two files. Two files are compared byte by byte
and the location of the first mismatch byte-no is displayed on the screen.
Example:
--------------------------------------------------------------------------------------------------------------------
india
china
japan
comm -2 file1 file2
india
pak
england
--------------------------------------------------------------------------------------------------------------------
Operation
Permission
u user
g group
+ assign
remove
r read
w write
o others
x execute
--------------------------------------------------------------------------------------------------------------------
Command Name: ps
Syntax: ps
Description: It gives the information on process status for process associate with
your terminal.
Example: ps
PID
TTY
TIME
CMD
24518
pts/1
00:00:00
bash
24559
pts/1
00:00:00
ps
Description: It gives the information on process status for process associate with
your terminal.
Example: kill -9 24518 //permanently delete the process for the given PID
Filters
There are certain commands which perform some filtering action on the data
called as filters.
Command Name: more
Syntax: command | more
Description: It is used to display the contents page by page.
Example: cal 2011 | more
--------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------
sort f b
sort n
sort u
--------------------------------------------------------------------------------------------------------------------
Example:
1. cut c 3 file1
2. cut d -f2 file1 //cut the field2(2nd column) of each line in file1
field
column2
separator
Command Name: pr
Syntax: pr filename | more
Description: It requires a file for printing by adding suitable header, footer &
formatted text.
Example: pr f | more
--------------------------------------------------------------------------------------------------------------------
Command Name: tr
Syntax: tr [a-z] [A-Z]
Description: It is used to translate all characters into uppercase.
Example: cat file1 | tr [a-z] [A-Z]
Shell Programming
1. PROGRAM TO FIND AVERAGE OF THREE NUMBERS
echo "enter three numbers"
read a
read b
read c
d=`expr $a + $b + $c`
e=`expr $d / 3`
echo "average=$e"
Output :enter three numbers
5
8
7
average=6
--------------------------------------------------------------------------------------------------2. PROGRAM USING if
echo "enter two number "
read a
read b
if [ $a == $b ]
then
echo "a is equal to b"
fi
if [ $a != $b ]
then
echo "a is not equal to b"
fi
Output :enter two number
5
9
a is not equal to b
---------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------
Output:1
21
321
4321
54321
654321
7654321
87654321
987654321
--------------------------------------------------------------------------------------------------17. PROGRAM USING FOR LOOP
ns="1 2 3 4 5"
for p in $ns
do
echo "$p"
done
Output:1
2
3
4
5
--------------------------------------------------------------------------------------------------18. PROGRAM TO DISPLAY A MULTIPLICATION TABLE
echo "enter a value"
read a
b=$a
for i in 1 2 3 4 5 6 7 8 9 10
do
a=`expr $b \* $i`
# \* represents multiplication
echo " $b * $i = $a"
done
Output:enter a value
5
5*1=5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50
--------------------------------------------------------------------------------------------------19. PROGRAM TO CHECK A CHARACTER IS VOWEL OR NOT
echo "enter a character "
read c
case $c in
(a)echo "it is a vowel";;
(e)echo "it is a vowel";;
(i)echo "it is a vowel";;
(o)echo "it is a vowel";;
(u)echo "it is a vowel";;
(*)echo "it is not a vowel";;
esac
Output:enter a character
a
it is a vowel
---------------------------------------------------------------------------------------------------
a=0
while [ $a -lt 10 ]
do
a=`expr $a + 1`
p=`expr $a % 2`
if [ $p -eq 0 ]
then
continue
fi
echo "$a"
done
Output:1
3
5
7
9
--------------------------------------------------------------------------------------------------23. PROGRAM TO DIPLAY PYRAMID
echo "enter no of rows"
read n
i=1
while [ $i -le $n ]
do
j=1
while [ $j -lt $i ]
do
echo -n " "# -n option is used to continue printing in same line instead of going to next line
j=`expr $j + 1`
done
k=$i
while [ $k -le $n ]
do
echo -n "* "
k=`expr $k + 1`
done
echo
# to go to next-line
i=`expr $i + 1`
done
Output:enter no of rows
10
**********
*********
********
*******
******
*****
****
***
**
*
--------------------------------------------------------------------------------------------------24. PROGRAM TO DIPLAY PYRAMID
echo "enter no of rows"
read n
i=1
while [ $i -le $n ]
do
j=$i
while [ $j -lt $n ]
do
echo -n " "
j=`expr $j + 1`
done
k=1
while [ $k -le $i ]
do
echo -n "* "
k=`expr $k + 1`
done
echo
i=`expr $i + 1`
done
Output:enter no of rows
10
*
**
***
****
*****
******
*******
********
*********
**********
--------------------------------------------------------------------------------------------------25. PROGRAM TO REVERSE A STRING ON COMMAND LINE
s=`echo $1|wc -c`
echo -n "reverse string is "
while [ $s -gt 0 ]
do
temp=`echo -n $1|cut -c $s`
s=`expr $s - 1`
echo -n "$temp"
done
Output:sh prog25.sh driems
reverse string is smeird
Output:2
5
3
7
--------------------------------------------------------------------------------------------------27. PROGRAM TO ADD THE ELEMENTS OF ARRAY
echo "how many numbers "
read n
i=0
sum=0
arr=( )
echo " enter $n numbers"
while [ $i -lt $n ]
do
echo -n "x="
read arr[i]
sum=`expr $sum + ${arr[i]}`
i=`expr $i + 1`
done
echo " sum is =$sum"
Output:how many numbers
4
enter 4 numbers
x=3
x=7
x=6
x=4
sum is =20