Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 28

Ex.

No: 1(a) STUDY OF UNIX OS


Date:

INTRODUCTION

An operating system is software that acts as an interface between the user and the
computer hardware. It is considered as the brain of the computer. It controls and co-ordinates the
internal activities of the computer and provides user interface.

The computer system is built with the following general components


i) Hardware

ii) Application Software

iii) Operating System

(i)Hardware: This includes the physical components such as CPU, Keyboard, Hard disk
and Printer.
(ii) Application Software: These are the programs that are used to accomplish specific
tasks.
(iii)Operating System: It is the component or the set of programs to manage and control the
hardware as well as co-ordinate the applications. Each system must have at least
have the hardware and the OS.
Functions of an Operating System

 Command interpretation: The CPU can’t understand the commands keyed in by a user.
It is the function of the OS to make it understand.
 Peripheral Interfaces: The OS also has to take care of the devices attached to the
system. The OS oversees communication between these devices and the CPU.
 Memory management: The OS handles the extremely important job of allocating
memory for various processes running on the system.
 Process management: This is required if several programs must run concurrently. CPU
time would then have to be rationed out by the OS to ensure that no programs get more
than its fair share of the processor time.

Services of an OS:
1. Process Management

2. File Management

3. I/O Management

4. Scheduling

5. Security Management

UNIX OPERARTING SYSTEM:


In the mid-1960s, AT &T Bell Laboratories developed a new OS called Multics. Multics
was intended to supply large scale computing services as a utility; much like electrical power. In
1969 Ken Thompson, Dennis Ritchie and others developed and simulated an initial design for a
file system that later evolved into the UNIX file system. The whole UNIX was rewritten in „C‟
language in 1973.Today, UNIX is a giant OS and is much powerful than most of its counter parts.
UNIX operating System is like a layer between the hardware and the applications that run
on the computer .It has functions that run on the computer. It has functions that manage the
executing applications.
UNIX system is an OS, which includes the traditional system components. UNIX
system includes a set of libraries and a set of applications.
KERNEL is the heart of UNIX OS that manages the hardware and the executing
process. The UNIX system views each device as a file called a device file. It implements
security controls to protect the safety and privacy of information. The Unix System allocates
resources including use of the CPU and mediates accesses to the hardware.
Application portability is the ability of a single application to be executed on various
types of computer hardware without being modified. This is one of the important advantages of
UNIX.

FEATURES OF UNIX:
1. Multitasking
Multitasking is the capability of the OS to perform various tasks simultaneously. i.e. A single
user can run multiple programs concurrently.
2. Multiuser Capability
Multiuser capability allows several users to use the same computer to perform their tasks.
Several terminals are connected to a single powerful computer and each user can work with
their terminals.
3. Security
Unix allows sharing of data. Every user must have a Login name and a password. So, accessing
another user’s data is impossible without permission.
4. Portability
Unix is a portable because it is written in high level languages so it can run on different
computers.
5. Communication
Unix supports communication between different terminals connected to the Unix server and
also between the users of one computer to the users of another computer located elsewhere
in the network.
6. Programming Facility
UNIX is highly programmable; the UNIX shell programming language has the
conditional statements and control structures and variables.

UNIX ARCHITECTURE
The functioning of Unix OS is handled in 3 ways. The closest layer is the hardware,
which provides the services to the OS. The Unix OS referred to Unix as the kernel, is the heart
of the Unix. The other layer is the shell, which acts as the mediator that translates the
commands given by the application programs.

Kernel
It is the core of the OS. It controls all the tasks and carries out all the functions of an
OS such as memory and file management etc., and it keep track of the programs that are
executing. It also handles the information exchange between the terminals such as tape drives
and printers etc.,

Functions of Kernel
1. Allocating and deallocating memory to each and every process.
2. Receiving instructions from the shell and carrying them out.
3. Managing files that held on various storage devices.
4. Scheduling, Coordinating and assigning various input/output devices simultaneously.
5. Enforcing security measures.
6. Providing Network services.
7. Providing Utility services.
8. Coordinating each and every process with signal handling.
9. Providing administrative functions or utilities.
Shell
It is the command interpreter of the OS. The commands given from the user are
moved to the shell. The shell analyses and interprets these commands into the machine
understandable form. The commands can be either typed in through the command line or
contained in a file called shell script. Hence, Shell acts as an interface between the user
and the kernel.
UNIX has a variety of shells, they are:
(i) Bourne Shell: It is developed by Steve Bourne and it is the most popular shell and widely
used. This shell comes bundled with almost every UNIX system.
(ii) Korn shell: It is developed by David G.Korn. This is superset of Bourne shell and it has
more capabilities.
(iii) C Shell: It is developed by Bil Joy. It is similar to C Programming language.

Features of Shell
1. All communications between user and Kernel takes place through the shell.
2. It allows the tasks to run on background.
3. It also enables us to construct scripts like a programming language.
4. A group of files can be executed using a single command.

Starting a UNIX session – Logging In


A user of UNIX based system works as a user terminal. After the boot procedure is
completed, that is the operating system is loaded in memory, the following message appears at
each user terminal:

Logging
Each user has an identification called the user name is the login name which has to be
entered when the login: message appears. The user is then asked to enter the password. UNIX
keeps track of all the UNIX user names and the information about identity in a special file. If the
login name entered does not match with any of the user names it displays the login message
again. This ensures that, only authorized people use the system. When a valid user name is
entered at the terminal the dollar symbol is displayed on the screen this is the UNIX prompt.

Ending a UNIX session –Logging Out


Once a user has logged into the system the users’ works session continues until the user
instructs the shell to terminate the session. This is done by pressing the ctrl and d keys together or
typing exit at the dollar prompt. Then the systems display the login: Prompt on the screen.
Ex.No: 1(b) BASIC UNIX COMMANDS

Date:
Aim:
To perform operations using basic unix commands
Algorithm:
1.start the terminal
2.type the UNIX commands for specific operation
3.Execute the command.
4.close the terminal window
1. cat command
This command is used to create a file in unix.
Syntax:
$ cat >filename

This command is also used for displaying contents in a file.


Syntax:
$ cat filename

2. ls command
It displays the list of files in the current working directory.
Syntax:
$ ls

3. rm command
This command is used to remove an existing file .
Syntax:
$ rm filename

4. wc commad
This command is used to count the number of lines characters & words in a file.
Syntax:
$ wc filename

5. pwd command
This command is used to know the current working directory.

Syntax:
$ pwd

6. date command
This command is used to display the current date ,year ,month ,day &time.
Syntax:
$ date

7. echo command
This command is used to print the message on the display .
Syntax:
$ echo text

8. cal command
This command is used to display the specified month or year calendar.
Syntax:
$ cal month

9. who command
This command is used to display the date & terminal type of all the users who are currently logged
into a system
Syntax:
$ who

10. who am I command


This command is used to display the logging details of the user who worked in that terminal.
Syntax:
$ who am i

11. bc command
This command is used to perform mathematical calculations.
Syntax:
$ bc operation
12. tputclear command
This command is used to clear the screen & place the $ prompt at the top most of the screen.
Syntax:
$ tput clear

13. tty command


This command is used to display the device type of the terminal.
Syntax:
$ tty

14. mkdir command


This command is used to create a new directory.
Syntax:
$ mkdir filename

15. man command


If we get stuck on something, and cannot find an expert to help, we can print any manual page on
our terminal with the command ’man’ command-name.
Syntax:
$ man who

16. head and tail command


The head command is used to display the initial part of a text file. This can be thought of aa a
complement command to the tail command. which displays the last part of a text file. By default head
command displays first 10 lines of a file and tail command the last lines. $ head [-count] filename.
Syntax:
$ head -4 friends
jude
jacob
raju
prem
will display the first four lines of a file called friends.

$ tail [+|- number] filename


Syntax:
$ tail +5 friend
will display from the 5th line from the beginning of the file.
$ tail -5 friend
will display from the 5th line from the end of the file.

17. cd command
It is used to change the current directory to the other directory specified.
Syntax:
$ cd jac

18. rmdir command


It is used to remove the directory. It requires the directory to be empty.
Syntax:
$ rmdir directoryname

19. cp command
It is used to create duplicate copies of ordinary files.
Syntax:
$ cp source destination

20. ln – link command


It is used to establish an additional filename for the same ordinary file.
Syntax:
$ ln ordinary_filename additional_filename
Ex:
$ ln red rose
Advantage of this command is that several users can have access to a common data file.Any
modification in the additional file or ordinary file reflects in both of them.

21. mv command
It is used to rename and move ordinary and directory files. To do this we need both execute and
write permissions.
Syntax:
$mv source destination
Ex:
Renaming, $ mv aaa bbb

To move the contents of the file old to the new, the syntax is: Ex: $ mv old new

22. To concatenate several files and display:

The above cat command will concatenate the two files (file1.txt and file2.txt) and it will display
the output in the screen.

Syntax:
cat file1.txt file2.txt

Some times the output may not fit the monitor screen. In such situation you can print those files
in a new file or display the file using less command.

Syntax:
cat file1.txt file2.txt | less

To concatenate several files and to transfer the output to another file.

cat file1.txt file2.txt > file3.txt

In the above example the output is redirected to new file file3.txt. The cat command will create
new file file3.txt and store the concatenated output into file3.txt.

23. To view the files page by page


More command is used to view one page of a file at a time.
Syntax:
cat filename | more
Or
Less command is also used to view the file page by page.
Syntax:
cat filename | less

24. To list files and directories, recursively on a single line


ls -R gives a recursive listing, including the contents of all subdirectories and their subdirectories
and so on.

25.To append more than one file


To append data into the same file use append operator >> to write into the file, else the file will be
overwritten (i.e., all of its contents will be erased).
Syntax:
cat >> file1.txt

26.To change the password


The passwd command changes the password for the user.

Syntax:
passwd tech

27. To view the commands which was executed


History command is used to view the commands which were executed.
Syntax :
History

28. List all filenames which has dot at the end


Syntax:
find ./ -type f -name '*[.].*'

29. List all filenames which has an extension


Syntax:
find ./ -type f -name '*.*'

30. Display all files, sub directories and files in subdirectories including their sizes
Syntax:
du –sk * | sort -n

31. Store the output of date command in a file called date. out
Syntax:
date >> date.out
32. To display the file attributes
Syntax:
stat filename

33. Change the mode of the file, where the changed file should possess all the permission
Syntax:
chmod 777

34. List all files in current directory whose second character is a digit
Syntax:
find ./ -type f -name ' ?[0-9]*'

35. Sort the files in alphabetical order


Syntax:
sort filename

36. Change the file to read only mode


Syntax:
chmod 0444

Maximum Mark obtained


mark
Aim 10
Algorithm& Program 40
Execution 30
Result 10
Viva 10
Total 100

Result:
Thus the various UNIX commands are executed successfully
Ex.No:2(a) STUDY OF VI EDITOR AND BASIC SHELL COMMANDS
Date:

An editor is program that allows to see a portion of a file on the screen and to modify
characters and lines by simply typing at the cursor position. There are number of editors that
may be included with the UNIX system, including ED, EX, VI and EMACS.

The vi Editor

VI - vi stands for visual.

VI is a full screen editor and is widely acknowledged as one of the most powerful editors
available .It is a full screen editor that allows the user to view and edit the entire document at
the same time.

The VI editor was written in the University of California at Berkeley by Bill Joy,
who is one of the co-founder of Sun Microsystems.

Features of VI Editor

1. It is very easy to learn and has more powerful and exciting features.
2. It works in great speed.
3. VI is case sensitive.
4. VI has powerful undo features than most other word processors, but it has no formatting
features.

Modes of VI Editor
VI editor works in three modes of operations specified below:

Command Mode: In this mode, all the keys pressed by the user are interpreted to be editor
commands. No text is displayed on the screen, even if corresponding key is pressed on
keyboard.
Insert Mode: This mode permits to insert new text, editing and replacement of existing text.
Once VI editor is in the insert mode, letters typed in the keyboard are echoed on the screen.

The ex or escape colon ( : ) Mode: This mode allow us to give commands at the command
line. The bottom line of the VI editor is called the command line.vi uses the command line to
display messages and commands.

Starting with VI editor

Syntax: vi filename

Moving the cursor

The cursor movement commands are:

Command Action

H or backspace Left one character

l or spacebar Right one character

K or - Up one line

J or + Down one line

I Moves forward a word

#b Moves back a word

#e Moves to the last character in the word

F[character] Moves right to the specified character in a line

T[character] Moves right and places it one character before the specified
character

0 or ^ Moves to the beginning of the file

#$ Moves to the end of the file

L Moves to the last line of the file

G Moves to the specified line number

Editing the file

 Open the file using $ vi filename

 To add text at the end of the file, position the cursor at the last character of the file.

 Switch from command mode to text input mode by pressing „a‟.

 Here „a‟ stands for append.

 Inserting text in the middle of the file is possible by pressing „i‟. The editor accepts
and inserts the typed character until Esc key is pressed.

Deleting Text

For deleting a character, move the cursor to the character, press „x‟. The character
will disappear.

Command Purpose

X Deletes one character


Nx Deletes n number of characters

#x Deletes on character at the cursor position

#X Deletes on the character before the cursor position

D$ or d Deletes a line from the cursor position to the end of the line

D0 Deletes from the cursor position to the starting of the line

#dd Deletes the current line where the cursor is positioned

#dw Deletes the word from the cursor position to the end of the word

Starting vi
Command Description
vi file start at line 1 of file
vi +n file start at line n of file
vi + file start at last line of file

Saving files and quitting vi


Command Description
:w save (write out) the file being edited
:w file save as file
:w! file save as an existing file
:q quit vi
:wq save the file and quit vi
:x save the file if it has changed and quit vi
:q! quit vi without saving changes

Moving the cursor


Keys pressed Effect
h left one character
l or <Space> right one character
k up one line
j or <Enter> down one line
b left one word
w right one word
( start of sentence
) end of sentence
1G top of file
nG line n
GG end of file
<Ctrl>W first character of insertion
<Ctrl>U up ½ screen
<Ctrl>D down ½ screen
<Ctrl>B up one screen
<Ctrl>F down one screen

Inserting text
Keys pressed Text inserted
a after the cursor
A after last character on the line
i before the cursor
I before first character on the line
o open line below current line
O open line above current line

Searching for text


Search Finds
/and next occurrence of ``and'', for example, ``and'', ``stand'',``grand''
?and previous occurrence of ``and''
/^The next line that starts with ``The'', for example, ``The'',``Then'',
``There''
/end$ next line that ends with ``end''
/[bB]ox next occurrence of ``box'' or ``Box''
SHELL PROGRAMMING

The format for the various conditional statements and looping statements and the
relational operators used for those conditions are given below.

if condition

if condition then
execute commands
fi

if – else condition

if condition then
execute commands
else
execute commands
fi

if – elif condition – multi way branching

if condition then
execute commands elif condition
then
execute commands
else
execute commands
fi

Relational Operators

Operator Meaning

-eq Equal to
-ne Not equal to

-gt Greater than

-ge Greater than or equal to

-lt Less than

-le Less than or equal to


Case condition

case expression in
pattern1) execute commands ;;
pattern2) execute commands ;;
pattern3) execute commands ;;
……………..
esac while looping
WHILE

While expression Do
execute commands
done

UNTIL
while’s complement until [ condition ]

FOR looping

for variable in list


do
execute commands
done

SHELL PROGRAMMING
Ex.No: 2b SUM AND AVERAGE OF 4 INTEGERS

Date:
Aim:

To write a shell program for finding the sum and average of 4 integers.

Algorithm:

1. Start the program.


2. Read the four variables a, b, c, d.
3. Calculate sum of a, b, c, d and store output in variable ‘sum’.
4. Calculate average by sum/4.
5. Print the sum and average of four numbers.
6. Stop the program.

SUM AND AVERAGE OF 4 INTEGERS

echo “Enter four integers”


read a
read b
read c
read d
sum=`expr $a + $b + $c + $d`
avg=`expr $sum / 4`
echo “Sum=$sum”
echo “Average=$avg”

OUTPUT:
Maximum Mark obtained
Enter four integers
10 mark
20 Aim 10
30 Algorithm& Program 40
40
Execution 30
Sum=100
Average=25 Result 10
Viva 10
Total 100
Result: Thus shell program for finding sum and average of 4 integers has been executed
successfully.

Ex.No: 2c SIMPLE INTEREST AND COMPOUND INTEREST


Date:
Aim:

To write a shell program for computing simple interest and compound interest.
Algorithm:

1. Start the program.


2. Read the variables p, n, r.
3. Calculate the simple interest by using the formula pnr/100.
4. Calculate the compound interest.
5. Print the simple interest and compound interest.
6. Stop the program.

SIMPLE INTEREST AND COMPOUND INTEREST

echo "Enter the Principal Amount"


read p
echo "Enter the Rate of Interest"
read r
echo "Enter the Number of Years"
read n
si=`expr $p \* $r \* $n`
si=`expr $si / 100`
echo "The Simple Interest is: Rs $si"
co=`expr 100 + $r`
co=`expr $co / 100`
i=2
ci=1
while [ $i -le $n ]
do
ci=`expr $ci \* $co`
i=`expr $i + 1`
done
ci=`expr $ci \* $p`
echo "The Compound Interest is: Rs $ci"

OUTPUT:

Enter the Principal Amount: 5000


Enter the Rate of Interest: 10

Enter the Number of Years: 1

The Simple Interest is: Rs 500

The Compound Interest is: Rs 5000

Result: Thus shell program for finding simple interest and compound interest has been
executed successfully.

Ex.No:2d SWAPPING OF TWO NUMBERS

Date:

Aim:

To write a shell program for swapping of two numbers.


Algorithm:

1. Start the program.


2. Read the variables a, b.
3. Interchange the values of a and b using another temporary variable c as follows:
c=a
a=b
b=c
4. Print a and b.
5. Stop the program.

SWAPPING OF TWO NUMBERS

echo “swapping using temporary variable”


echo “enter a”
read a
echo “enter b”
read b
c=$a
a=$b
b=$c
echo “after swapping”
echo “$a”
echo “$b”

OUTPUT:
Enter a
10
Enter b
20
After swapping
20
10

Result: Thus shell program for swapping two numbers has been executed successfully.

Ex.No: 2e COMPUTE THE POWER OF X

Date:

Aim:

To write a shell program for computing the power of x.

Algorithm:

1. Start the program.


2. Read the number as n.
3. Read the power as p.
4. Compute power of a number by pr=pr*n.
5. Print power.
6. Stop the program.

COMPUTE THE POWER OF X

echo “enter a number”


read n
echo “enter power”
read p
i=1
pr=1
while [ $i -le $p ]
do
pr=`expr $pr \* $n`
i=`expr $i + 1`
done
echo “result is:”
echo “$pr”

OUTPUT:
Enter number
5
Enter power
2
Result is:
25

Result: Thus shell program for computing the power of x has been executed successfully.

Ex.No:2f LEAP YEAR OR NOT

Date:

Aim:

To write a shell program for checking whether year is leap year or not.

Algorithm:

1. Start the program.


2. Read the year.
3. Check whether year%4, year%100, year%400 is zero.
4. If zero then print year is leap year.
5. Else print year is not leap year.
6. Stop the program.

LEAP YEAR OR NOT

echo "Finding Leap Year"

echo "enter any year"

read y

a=`expr $y % 4`

b=`expr $y % 100`

c=`expr $y % 400`

if [ $a -eq 0 -a $b -ne 0 -o $c -eq 0 ]

then

echo "$y is a leap year "

else
echo "$y is not a leap year "

fi

OUTPUT:

Enter a year:
2000
Year is leap year

Result: Thus shell program for checking whether year is leap year or not has been executed
successfully.

Ex.No: 2g GIVEN NUMBER IS ODD OR EVEN

Date:

Aim:

To write a shell program for checking whether given number is odd or even.

Algorithm:

1. Start the program.


2. Read the number as num.
3. Check whether num%2 is zero.
4. If zero then print number is even.
5. Else print number is odd.
6. Stop the program.

GIVEN NUMBER IS ODD OR EVEN

echo “enter a number”


read a
z=`expr $a % 2`
if [ $z -eq 0 ]
then
echo “$a is even”
else
echo “$a is odd”
fi
OUTPUT:

Enter a number
5

5 is Odd

Result: Thus shell program for checking whether given number is odd or even has been
executed successfully.

Ex.No:2h GIVEN NUMBER IS POSITIVE OR NEGATIVE

Date:
Aim:

To write a shell program for checking whether given number is positive or negative.
Algorithm:

1. Start the program.


2. Read the number as num.
3. If num<0 then print number is negative.
4. Else if num>0 then print number is positive.
5. Else print number is zero.
6. Stop the program.

GIVEN NUMBER IS POSTIVE OR NEGATIVE

echo “enter a number”


read n
if [ $n -gt 0 ]
then
echo “$n is positive”
elif [ $n -lt 0 ]
then
echo “$n is negative”
else
echo “zero”
fi

OUTPUT:

Enter a number

2
2 is Positive

Result: Thus shell program for checking whether the given number is positive or negative has
been executed successfully.

Ex.No: 2i QUOTIENT AND REMAINDER OF TWO NUMBERS

Date:
Aim:

To write a shell program for finding the quotient and remainder of two numbers.

Algorithm:

1. Start the program.


2. Read the number as n.
3. Read the divisor as d.
4. Calculate quotient as n/d.
5. Calculate remainder as n%d.
6. Print quotient and remainder.
7. Stop the program.

QUOTIENT AND REMAINDER OF TWO NUMBERS

echo “enter a number”


read n
echo “enter divisor”
read d
q=`expr $n / $d`
r=`expr $n % $d`
echo “quotient is:$q”
echo “remainder is:$r”

OUTPUT:

Enter a number
77

Enter divisor
5

Quotient is: 15

Remainder is: 2
Result: Thus shell program for finding the quotient and remainder of two numbers has been
executed successfully.

You might also like