Operating System Lab Manual
Operating System Lab Manual
Operating System Lab Manual
For
OPERATING SYSTEM
(2140702)
B.E. (COMPUTER)
SEM IV
Operating System LAB Manual
Table of Contents
Sr. Topic Page
No. No.
1. Introduction 1
1.1 What is kernel? 1
1.2 How to use shell? 2
1.3 What is process? 5
1.4 Why process required? 5
1.5 Linux commands related with process 6
1.6 Redirection of Standard output/input or Input – Output 6
redirection
1.7 Pipes 7
1.8 Filter 7
Its environment provided for user interaction. Shell is a command language interpreter that
executes commands read from the standard input device (keyboard) or from a file. Linux may
use one of the following most popular shells.
Any of the above shell reads command from user (via Keyboard or Mouse) and tells Linux
O/s what users want. If we are giving commands from keyboard it is called command line
interface.
$ echo $SHELL
To use shell (You start to use your shell as soon as you log into your system) you have to
simply type commands. Following is the list of common commands.
Prompt)
current directory
To create text file cat > { file name } $ cat > myfile
~2~
Operating System LAB Manual
(CTRL+D)
screen at a time
file/directory
name
Directory/subdirectory.
~3~
Operating System LAB Manual
(i..e. yourself)
feature is disabled by
System
Administrator)
~4~
Operating System LAB Manual
numeric order
Process is any kind of program or task carried out by your PC. For e.g. $ ls -lR, is command
or a request to list files in a directory and all subdirectory in your current directory. It is a
process. A process is program (command given by user) to perform some Job. In Linux when
you start process, it gives a number (called PID or process-id), PID starts from 0 to 65535.
Linux is multi-user, multitasking o/s. It means you can run more than two processes
simultaneously if you wish. For e.g., to find how many files do you have on your system you
may give command like
$ ls / -R | wc -l
This command will take lot of time to search all files on your system. So you can run such
command in Background or simultaneously by giving command like
$ ls / -R | wc -l &
The ampersand (&) at the end of command tells shells start command (ls / -R | wc -l) and run
it in background takes next command immediately. An instance of running command is
called process and the number printed by shell is called process-id (PID), this PID can be
used to refer specific running process.
~5~
Operating System LAB Manual
Mostly all command gives output on screen or takes input from keyboard, but in Linux it's
possible to send output to file or to read input from file. For e.g. $ ls command gives output to
screen; to send output to file of ls give command, $ ls > filename. It means put output of ls
command to filename. There are three main redirection symbols >, >>, <
To output Linux-commands result to file. Note that if file already exist, it will be overwritten
else new file is created. For e.g. to send output of ls command give
$ ls > myfiles
Now if 'myfiles' file exist in your current directory it will be overwritten without any type of
warning. (What if I want to send output to file, which is already exist and want to keep
information of that file without losing previous information/data? For this, Read next
redirector)
To output Linux-commands result to END of file. Note that If file exist , it will be opened
and new information / data will be written to END of file, without losing previous
information/data, And if file is not exist, then new file is created. For example,
~6~
Operating System LAB Manual
To take input to Linux-command from file instead of key-board. For e.g. to take input for cat
command give
1.7 Pipes
A pipe is a way to connect the output of one program to the input of another program without
any temporary file.
A pipe is nothing but a temporary storage place where the output of one commands stored
and then passed as the input for second command. Pipes are used to run more than two
commands (Multiple commands) from same command line.
1.8 Filter
If a Linux command accepts its input from the standard input and produces its output on
standard output is known as a filter. A filter performs some kind of process on the input and
gives output. For e.g.. Suppose we have file called 'hotel.txt' with 100 lines data, And from
~7~
Operating System LAB Manual
'hotel.txt' we would like to print contains from line number 20 to line number 30 and store
this result to file called 'hlist' then give command
Here head is filter which takes its input from tail command (tail command start selecting
from line number 20 of given file i.e. hotel.txt) and passes this lines to input to head, whose
output is redirected to 'hlist' file.
~8~
Operating System LAB Manual
Shell program is series of Linux commands. Shell script is just like batch file is MS-DOS but
have more power than the MS-DOS batch file. Shell script can take input from user, file and
output them on screen. Useful to create our own commands that can save our lots of time and
to automate some task of day today life.
1) System variables - Created and maintained by Linux itself. This type of variable defined
in capital letters.
2) User defined variables (UDV) - Created and maintained by user. This type of variable
defined in lower letters.
You can see system variables by giving command like $ set, some of the important System
variables are
Syntax: variablename=value
For example,
~9~
Operating System LAB Manual
$ no=10
$ vech=Bus
$echo $vech
$echo $no
Rules for Naming variable name (Both UDV and System Variable)
(1) 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
(2) Don't put spaces on either side of the equal sign when assigning value to variable. For e.g.
in following variable declaration there will be no error
$ no=10
$ no =10
$ no= 10
$ no = 10
(3) Variables are case-sensitive, just like filename in Linux. For e.g.
$ no=10
$ No=11
$ NO=20
$ nO=2
Above all are different variable name, so to print value 20 we have to use $ echo $NO and
Not any of the following
~ 10 ~
Operating System LAB Manual
(4) You can define NULL variable as follows (NULL variable is variable which has no value
at the time of definition) For e.g.
$ vech=
$ vech=""
Try to print it's value $ echo $vech , Here nothing will be shown because variable has no
value i.e. NULL variable.
Now we write our first script that will print "Knowledge is Power" on screen. To write shell
script you can use in of the Linux's text editor such as vi or mcedit or even you can use cat
command. Here we are using cat command you can use any of the above text editor. First
type following cat command and rest of text as its
clear
Press Ctrl + D to save. Now our script is ready. To execute it type command
$ ./first
This will give error since we have not set Execute permission for our script first; to do this
type command
$ chmod +x first
$ ./first
First screen will be clear, then Knowledge is Power is printed on screen. To print message of
variables contains we user echo command, general form of echo command is as follows
echo "Message"
~ 11 ~
Operating System LAB Manual
Because of security of files, in Linux, the creator of Shell Script does not get execution
permission by default. So if we wish to run shell script we have to do two things as follows
(1) Use chmod command as follows to give execution permission to our script
Syntax: ./your-shell-program-name
For e.g.
$ ./first
Here '.'(dot) is command, and used in conjunction with shell script. The dot(.) indicates to
current shell that the command following the dot(.) has to be executed in the same shell i.e.
without the loading of another shell in memory. Or you can also try following syntax to run
Shell Script
specify complete path of your script whenever you want to run it from other directories like
giving following command
$ /bin/sh/home/vivek/first
~ 12 ~
Operating System LAB Manual
Before making any decision in Shell script you must know following things Type bc at $
prompt to start Linux calculator program
$ bc
After this command bc is started and waiting for you commands, i.e. give it some calculation
as follows type 5 + 2 as
5+2
5-2
5/2
5>2
0 (Zero) is response of bc, How? Here it compare 5 with 2 as, Is 5 is greater than 2, (If I ask
same question to you, your answer will be YES) In Linux (bc) gives this 'YES' answer by
showing 0 (Zero) value. It means whenever there is any type of comparison in Linux Shell It
gives only two answers one is YES and NO is other.
Now will see, if condition which is used for decision making in shell script, If given
condition is true then command1 is executed.
Syntax:
if condition
then
of condition is 0 (zero)
...
...
fi
~ 13 ~
Operating System LAB Manual
Here condition is nothing but comparison between two values, for compression we can use
test or [expr] statements or even exist status can be also used. An expression is nothing but
combination of values, relational operator (such as >,<, <> etc) and mathematical operators
(such as +, -, / etc ).
test command or [ expr ] is used to see if an expression is true, and if it is true it return
zero(0),otherwise returns nonzero(>0) for false.
Now will write script that determine whether given argument number is positive. Write script
as follows
#!/bin/sh
if test $1 -gt 0
then
fi
Run it as follows:
$ chmod +x ispostive
$ ispostive 5
$ispostive -45
$ispostive
~ 14 ~
Operating System LAB Manual
Logical Operators
3.3 if...else...fi
~ 15 ~
Operating System LAB Manual
Syntax:
if condition
then
of condition is 0(zero)
...
...
else
...
...
fi
Multilevel if-then-else
Syntax:
if condition
then
elif condition1
elif condition2
else
~ 16 ~
Operating System LAB Manual
fi
Computer can repeat particular instruction again and again, until particular condition
satisfies. A group of instruction that is executed repeatedly is called a loop.
for loop
Syntax:
do
execute one for each item in the list until the list is
done
Suppose,
for i in 1 2 3 4 5
do
done
Run it as,
$ chmod +x testfor
$ ./testfor
while loop
Syntax:
while [ condition ]
do
command1
~ 17 ~
Operating System LAB Manual
command2
command3
..
....
done
Loop is executed as long as given condition is true. For eg. Above for loop program can be
written using while loop as
#!/bin/sh
if [ $# -eq 0 ]
then
exit 1
fi
n=$1
i=1
while [ $i -le 10 ]
do
i=`expr $i + 1`
done
~ 18 ~
Operating System LAB Manual
$ chmod +x nt1
$./nt1 7
The case statement is good alternative to multilevel if-then-else-fi statement. It enables you to
match several values against one variable. It’s easier to read and write.
Syntax:
case $variable-name in
pattern1) command
...
..
command;;
pattern2) command
...
..
command;;
patternN) command
...
..
command;;
*) command
...
..
command;;
esac
The $variable-name is compared against the patterns until a match is found. The shell then
executes all the statements up to the two semicolons that are next to each other. The default is
*) and its executed if no match is found. For e.g. create script as follows
~ 19 ~
Operating System LAB Manual
if [ -z $1 ]
then
elif [ -n $1 ]
then
rental=$1
fi
case $rental in
esac
$ chmod +x car
$ car van
$ car car
$ car Maruti-800
~ 20 ~
Operating System LAB Manual
Create script as
read fname
Run it as follows
$ chmod +x sayH
$ ./sayH
This script first ask you your name and then waits to enter name from the user, Then user
enters name from keyboard (After giving name you have to press ENTER key) and this
entered name through keyboard is stored (assigned) to variable fname.
~ 21 ~