Professional Documents
Culture Documents
CP Lab II Lab Manual
CP Lab II Lab Manual
S T U D Y O F U N I X O P E R AT I N G S Y S T E M
A I M : To s t u d y a b o u t u n i x o p e r a t i n g s y s t e m .
WHAT IS UNIX
UNIX is an operating system which was developed first in the 1960s and has under constant development ever since by operating s ystem we mean the suite of programs which make the computer work. It is a stable, multi -user, multi -tasking operating system for servers, desktops and laptops. UNIX operating system also have graphical user interface (GUI) similar to Microsoft windows operating system which provides an eas y to environment. However knowledge of unix is required for operations which are not covered by graphical program or for when there is no windows interface available. TYPES OF UNIX There are many different versions of unix, although they share the common similarities. The most popular varieties of unix are Sun Solaris, GNU/Linux and Macos X. THE UNIX OPERATING SYSTEM The Unix Operating system is made up of three parts 1. The Kernel 2. The Shell 3. The Programs THE KERNEL The Kernel of Unix is the hub of the operating system. It allocates time and memory to programs and handles the file store and communication in response to system calls.
As an illustration of the way that the shell and the kernel work together, suppose a user t ypes rm m yfile (which has the effect of removing the file m yfile).
The shell scratches the filestore for the file containing the program rm. And then req uests the kernel, through system calls to rm m yfile finished running, the shell then returns the unix prompt % to the user, indicating execute the program rm on myfile. When the process that it is waiting for future commands.
THE SHELL The shell acts as an interface between the user and the kernel. When user login, the login program checks the username and password, and then starts another program called the shell. The shell is a command line interpreter (CLI). It interprets the com mands the user types in an arranges for them to be carried out. The commands are themselves programs. When they terminate, the shell gives the user another prompt (% on our s ystem). By t yping part of the name of a command, filename and directory and pressi ng the [tab] key, the shell will complete the rest of them automaticall y. If shell finds more than one name beginning with those letters you have t yped, it will beep, prompting to t ype a few more letters before pressing the tab key again. The shell keeps the list of the commands you have t yped in, if you need to repeat a command, use the cursor key to scroll up and down the list or t ype history for a list of previous commands. Different users may use different shells. Initiall y, your sy stem administrator will suppl y a default shell, which can be overridden or changed.
Bourne shell (sh) C shell (csh) Korn shell (ksh) TC Shell (tcsh) Bourne Again Shell (bash)
Each shell also includes its own pro gramming language. Command files, called "shell scripts" are used to accomplish a series of tasks. Utilities :UNIX provides several hundred utilit y programs, often referred to as commands. Accomplish universal functions
Modular: single functions can be grouped to perform more complex tasks. The Bourne shell is one of the oldest shells and is the most efficient for background work. users. The C shell provides sophisticated interactive capabilities lacking in the Bourne shell. Features of this shell include a command history However it provides few facilities for interact ive
buffer, command aliases and file name completion. The C shell has a s yntax which resembles the C programm ing language. The C shell is the default shell for interactive work on many UNIX s ystems. It will be covered in this document, although most of the
basic commands given here are relatively standard across all the main shells. The Korn shell was written by David Korn from AT&T and in it he attempted to merge the preferred features of both the Bourne and C shells as well as adding some additional features.
Unfortunatel y the Korn shell was not available for free, as other UNIX shells were, so many users a nd companies did not chose to use of it. The Bash shell was based on the Bourne shell (Bourne again shell) and as with Korn it attempted to combine the best features of the other shells which were available at the time. This shell however was available for free. Bash was initiall y adopted for LINUX although
several varieties of LINUX now exist e.g. RedHat, SuSE and Debian GNU.
FILES AND PROCESS Everything in unix is either a file or a process. A process is an executing program identi fied by a unique PID (Process Identifier).
A file is collection of datas. They are created by users using text editors, running, compilers etc. Examples of files Document The text of a program written in some high level language.
All files are grouped together in the directory structure. The file system is arranged in a hierarchical structure, like inverted tree. The top of the hierarchy is traditionall y called root (written [/] slash) FEATURES OF UNIX 1. Multitasking Multitasking is the capabilit y of the operating system to perform various tasks simultaneousl y. I.e. A single user can run multiple programs (tasks) concurrently.
2.
Multi-user Capability Multi-user capabilit y of UNIX allows several users to use the
same computer to perform t heir tasks. Several terminals (keyboards and monitors) are connected to a single powerful computer (Unix server) and each user can work with their terminals.
3.
Securit y 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. I.e. Invalid users cannot access data.
4. Portabilit y UNIX is portable because it is written in a high level language. So, UNIX can be run on different computers.
o Between the different terminals connected to the UNIX server. o Between the users of one computer to the users of another computer located elsewhere in the network.
6. Programming facility UNIX is highl y programmable, the UNIX shell programming language has all necessary ingredients like conditional and control structure (Loops) and variables, that establish it as a programming language in its own right.
ADVANTAGES OF UNIX UNIX is a very stable operating system. UNIX supports the Multi -User feature. UNIX is a Multitasking operating system. UNIX can be loaded to any t ype of computer hardware. UNIX is optimized for program development. UNIX has rich set of small commands and utilities that do specific tasks. UNIX has a powerful unified file system, everything in a file data, program and all physical devices. UNIX has the abilit y to string commands and utilities together in unlimited ways to accomplish more complicated tasks. UNIX allows onl y authorized users to modify files and
directories. UNIX allows onl y System Administrators to make changes in System Configuration files.
Aim
Unix is security conscious, and can be used only by those persons who have an account. Telnet (Telephone Network) is a Terminal emulator program for TCP/IP networks that enables users to log on to remote servers.
User has to authenticate himself by providing username and password. Once verified, a greeting and $ prompt appears. The shell is now ready to receive
commands from the user. Options suffixed with a hyphen () and arguments are separated by space. General commands
Command date date +%D date +%T date +%Y date +%H cal calyear calmonth year who who am i tty uname uname r uname n echo "txt" echo$HOME bc lp file mancmdname history exit
Function Used to display the current system date and time. Displays date only Displays time only Displays the year part of date Displays the hour part of time Calendar of the current month Displays calendar for all months of the specified year Displays calendar for the specified month of the year Login details of all users such as their IP, Terminal No, User name, Used to display the login details of the user Used to display the terminal name Displays the Operating System Shows version number of the OS (kernel). Displays domain name of the server Displays the given text on the screen Displays the user's home directory Basic calculator. Press Ctrl+d to quit Allows the user to spool a job along with others in a print queue. Manual for the given command. Press q to exit To display the commands used by the user since log on. Exit from a process. If shell is the only process then logs out
Directory commands
Function Path of the present working directory A directory is created in the given name under the current A number of sub-directories can be created under one stroke directory Change Directory. If the subdir starts with / then path starts from To switch to the home directory. To switch to the root directory. root (absolute) otherwise from current working directory.
Command cat >filename catfilename cat>>filename cpsrc des cpi src des cp r src des mv old new mv f1 f2 f3 dir mv v old new rm file rm * rm r * rm f * ls lsname lsname* ls a ls xdirname ls R ls l cmp file1 file2 wc file chmod perm file
Function To create a file with some contents. To end typing press Ctrl+d. The > symbol means redirecting output to a file. (< for Displays the file contents. Used input)to append contents to a file Copy files to given location. If already exists, it will be Warns the user prior to overwriting the destination file overwritten Copies the entire directory, all its sub-directories and files. To rename an existing file or directory. i option can also be To move a group of files to a directory. used Display name of each file as it is moved. Used to delete a file or group of files. ioption can also be used To delete all the files in the directory. Deletes all files and sub-directories To forcibly remove even write-protected files Lists all files and subdirectories (blue colored) in sorted To check whether a file or directory exists. manner. Short-hand notation to list out filenames of a specific pattern. Lists all files including hidden files (files beginning with .) To have specific listing of a directory. Recursive listing of all files in the subdirectories Long listing showing file access rights (read/write/executerwx for user/group/others-ugo).Displays nothing if files are Used to compare two files. It produces a statistics of lines (l), words(w), and characters(c). identical. Changes permission for the specified file. (r=4, w=2, x=1) chmod 740 file sets all rights for user, read only for groups and no rights for others
The commands can be combined using the pipeline (|) operator. For example, number of users logged in can be obtained as.
who | wc -l
Finally to terminate the unix session execute the command exit or logout.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
January Su Mo Tu We Th Fr Sa 1 5 6 7 8 2 3 4 2 3
February Su Mo Tu We Th Fr Sa 1 4 5 6 7 8 2 3 4
March Su Mo Tu We Th Fr Sa 1 5 6 7 8
9 10 11
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
November
October December
Su Mo Tu We Th Fr Sa 1 2 3 4
Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
Su Mo Tu We Th Fr Sa 1 2 3 4 5 6
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
[vijai@localhost vijai]$ who root vijai cse1 ecea :0 pts/0 pts/3 pts/4 Apr Apr Apr Apr 9 9 9 9 08:41 13:00 (scl-64) 13:18 (scl-41.smkfomra.com) 13:18 (scl-29.smkfomra.com)
Linux
[vijai@localhost vijai]$ bc bc 1.06 Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc. 3+5 8 2%3 2
[vijai@localhost vijai]$ mkdir filter [vijai@localhost vijai]$ ls filter list.sh regexpr shellscripts
[vijai@localhost vijai]$ cat > greet hi ece-a wishing u the best [vijai@localhost vijai]$ cat greet hi ece-a wishing u the best
[vijai@localhost vijai]$ cat >> greet bye [vijai@localhost vijai]$ cat greet hi ece-a wishing u the best bye
[vijai@localhost vijai]$ ls -a . .. .bash_history .bash_logout .bash_profile .bashrc .canna .gtkrc .emacs .kde greet list.sh regexpr shellscripts .viminfo .viminfo.tmp .xemacs
[vijai@localhost vijai]$ ls -l total 16 -rw-rw-r--rw-rw-r-drwxrwxr-x drwxrwxr-x 1 1 2 7 vijai vijai vijai vijai vijai vijai vijai vijai 32 30 Apr Apr 4096 Apr 4096 Apr 11 4 9 4 14:52 13:58 14:30 14:57 greet list.sh regexpr shellscripts
greet
list.sh
regexpr
shellscripts
[vijai@localhost vijai]$ mv greet greet.txt [vijai@localhost vijai]$ ls greet.txt list.sh regexpr shellscripts
[vijai@localhost vijai]$ mv greet.txt ./regexpr/ [vijai@localhost vijai]$ ls list.sh shellscripts ./regexpr/ demo regexpr vijai]$ ls
[vijai@localhost greet.txt
[vijai@localhost vijai]$ rm -i *.sh rm: remove regular file `fact.sh'? y rm: remove regular file `list.sh'? n rm: remove regular file `prime.sh'? y [vijai@localhost vijai]$ ls list.sh regexpr shellscripts
[vijai@localhost vijai]$ cmp list.sh fact.sh list.sh fact.sh differ: byte 1, line 1
[vijai@localhost vijai]$ chmod ug+x list.sh [vijai@localhost vijai]$ ls -l list.sh -rwxrwxr-1 vijai vijai 30 Apr 4 13:58 list.sh
[vijai@localhost vijai]$ chmod 740 list.sh [vijai@localhost vijai]$ ls -l list.sh -rwxr----1 vijai vijai 30 Apr 4 13:58 list.sh
RESULT
Thus the stud y and execution of Unix commands has been completed S u c c e s s fu l l y.
STUDY OF VI EDITOR
Aim
To introduce the concept of text editing vi editor and the options regarding the control of the editor.
vi Editor
A text editor is one of the most common and useful tools in all Operating Systems. Unix provides a versatile editor vi, a full-screen editor and owes its origin to Bill Joy. "vi" stands for visual editor. A vi session begins by invoking vi with or without a filename
The user is presented with a full empty screen, each line beginning with a ~. This is vi's way of indicating non-existent lines. Out of 25 lines on the terminal, 24 can be used to enter text. The last line is reserved for commands and also used by the system to display messages. vi functions in three modes namely:
1. Input modeWhere any key depressed is entered as text 2. Command modeWhere keys are used as commands to act on text (initial
mode) 3. ex modeex mode commands that can be entered in the last line to act on text
INPUT MODE
vi modes
vi starts with command mode. To insert text any of the following commands should be used.
Commands i I a A o O
Function Inserts text to the left of the cursor. Inserts text at the beginning of line. Appends text to right of cursor Appends text at end of line Opens line below Opens line above
In Input mode the editor displays INSERT in the last line. Press Enter key to start a fresh line of text in Input mode and the ~ disappears. To quit input mode press Esc key.
COMMAND MODE
EDIT COMMANDS
Command Function R Replaces more than a single character. The editor displays s x ?text U or u dd dw d$ d0 yy yw p REPLACE in the last line. the left and switches to Input mode. they Deletes a single character to The existing text is overwritten as Deletes the character in the current cursor position are typed. Locates the text in the file. If not found, the message "Pattern not found" the last Use Reverses appears.chan n to repeat the forward search and N for Cuts the entire line backward search. NAVIGATION COMMANDS Cuts the entire word Cuts a line the buffer. ge made to from cursor position to the end of the line Cuts from the cursor position to the start of the line Copies (yanks) the entire line Copies the entire word Pastes the text
Function Moves back to beginning of a word Moves forward to beginning of word Moves to start of the line Moves to end of the line Up one line Down one line Left one character Right one character Scrolls a page forward Scrolls a page backward To move to the specific line
One of the most notable features of vi is the facility of prefixing a number to most commands. When prefixed, commands interpret the instruction to be repeated as many times. For example 3x deletes the next three character.
THE MODE
EX
The essential save and exit commands form the features of ex mode. Press : (colon) in command mode to switch to ex mode. The : is displayed in the last line. Type the command and press Enter key to execute the same.
Function Saves file, displays the number of lines & characters and returns to saved under the given name The file is Used to write specific line numbers to puts file. The Input mode. If it is an unnamed file then visome a message.. (dot) represents session and returnsfirst line and $ could be aused to Quits vi current line, 0 for to $ prompt. vi has safety represent last line. mechanism that warns if changes any changes are notsince the last Quits vi session without saving made to file made saved. Save and exit save Escape to shell This is yet another powerful feature known as substitution. It is similar to Find and Replace. % represents all lines, g To insert global. To make vi ask for confirmation before replacing makes it another file into the current file. Splits screen into multiple windows and opens the file. use gc instead of g.
This is vi improved vim A rudimentary text ~ ~ ~ ~ ~ ~ ~ ~ ~ Sample.txt 2L, 30C written vi Editor for new file
RESULT
Thus the study of text manipulation using vi editor has been completed successfully.
AIM
ALGORITHM
1. Enter the value of a number. 2. Perform the product of the given number thrice. 3. Display the result.
PROGRAM:
read a
let b=$a*$a*$a
OUTPUT
enter a number
the cube is 27
RESULT
EX NO: DATE:
Student Details
AIM
ALGORITHM
1. Start the program. 2. Get name,DOB,reg.no and percentage respectively. 3. Display the details 4. Stop the program
PROGRAM:
read n
read d
read r
read p
OUTPUT:
murugan
14.5.1993
48
95
name: murugan
DOB: 14.5.1993
reg.no: 48
percentage: 95
RESULT:
EX NO: DATE:
Sum Of N Numbers
AIM:
ALGORITHM:
1. Enter the limit as n. 2. Initialize i as 1 and sum as 0. 3. Compute the value of sum and i until i is less than or equal to n. 4. Sum is computed by adding the value of sum and i. 5. i is computed by adding one to i.
PROGRAM:
read n
i=1
sum=0
while [ $i -le $n ]
do
let sum=$sum+$i
let i=$i+1
done
OUTPUT:
enter limit
RESULT
EX NO : DATE:
AIM:
ALGORITHM:
sum=0
for i in 1 2 3 4 10
do
done
the sum is 20
RESULT:
EXP NO : DATE:
AIM:
ALGORITHM:
1. Get the previous unit and current unit. 2. Calculate the current consumed. 3. If the current consumed is greater than 1000 then amount is the product of current consumed and 0.9. 4. If the current consumed is greater than 500 and less than 1000 then amount is the product of current consumed and 0.6. 5. If the current consumed is less than 500 then amount is the product of current consumed 0.9. 6. Display the amount for the current consumed.
read pu cu
let cc=$cu-$pu
then
let amt=$cc*9/10
then
let amt=$cc*6/10
else
let amt=$cc*3/10
fi
OUTPUT:
1000 2000
RESULT:
EX NO: DATE:
NET SALARY
AIM:
ALGORITHM
1. Enter the basic pay,hra,ta,da and pf. 2. Add the basic pay,hra, ta,da and save it as the gross salary. 3. If gross salary is greater then 10000 then income tax equals the product of gross salary and 0.5. 4. If gross salary is less than 10000 then income tax equals the product of gross salary and 0.2. 5. Assign deduction as the sum of pf and it. 6. Net salary the difference between gross salary and deduction.
PROGRAM:
read bp hra ta da pf
let gs=$bp+$hra+$ta+$da
then
let it=$gs*5/100
else
let it=$gs*2/100
fi
let de=$pf+$it
let ns=$gs-$de
OUTPUT:
RESULT:
EX NO : DATE:
AIM:
ALGORITHM:
clear
read choice
case $choice in
1) pwd;;
2) date;;
3) who;;
esac
OUTPUT:
2.todays date
3.list of users
/home/linuxpert
RESULT:
EX NO: DATE:
AIM:
ALGORITHM:
1. Read the value of n. 2. Assign the value of x as 0 and i as 2. 3. Increment the value of i until it reaches n. 4. Perform the modulo of n with i and store it in s. 5. If s equals zero then assign x as 1. 6. IF x equals 1 then print n is not a prime number. 7. If x doesn't equal 1 the print n is a prime number.
read n
x=0
for((i=2;i<n;i++))
do
let s=$n%$i
if [ $s -eq 0 ]
then
x=1
break
fi
done
if [ $x -eq 1 ]
then
else
fi
OUTPUT:
7 is a prime number
RESULT
EX NO DATE:
FACTORIAL NUMBER
AIM:
ALGORITHM:
1. Read the value of n. 2. Assign the value of i and fact as 1. 3. Find the product of fact and i until i is less than or equal to 1. 4. Increment the value of i value by 1. 5. Print the factorial value.
PROGRAM:
read n
i=1
fact=1
while [ $i -le $n ]
do
let fact=$fact*$i
let i=$i+1
done
OUTPUT:
factorial of 7 is 5040
RESULT:
EX NO: DATE:
AIM:
ALGORITHM:
2. If choice is one, addition is performed and sum is displayed. 3. If choice is two, subtraction is performed and difference is displayed. 4. If choice is three, multiplication is performed and product is displayed. 5. If choice is four, division is performed and quotient is displayed.
PROGRAM
read n
read a b
case $n in
1)let x=$a+$b
2)let x=$a-$b
3)let x=$a*$b
4)let x=$a/$b
exit
esac
OUTPUT:
42
the quotient is 2
RESULT:
EX NO : DATE:
FIBONACCI SERIES
AIM:
ALGORITHM:
PROGRAM:
read n
a=0
b=1
i=3
echo $a
echo $b
while [ $i -le $n ]
do
c=`expr $a + $b`
echo "$c"
a=$b
b=$c
i=`expr $i + 1`
done
OUTPUT:
enter a number
fibonacci series
RESULT:
EX NO : DATE:
ODD OR EVEN
AIM:
ALGORITHM:
1. Get the number as x. 2. read num. 3. check if x % 2 equal 0 and if yes display the given number is even. 4. Else display the given number is odd.
PROGRAM:
read num
x=`expr $num % 2`
if test $x -eq 0
then
else
fi
OUTPUT:
4 is an even number
RESULT:
EX NO DATE:
AIM:
ALGORITHM:
3. If so,Print A is greater. 4. Else, check if B is greater than C. 5. If so, print B is greater, else print C is greater.
read a b c
then
elif [ $b -gt $c ]
then
else
fi
OUTPUT:
enter 3 values
698
9 is big
RESULT:
EX NO : DATE:
POSITIVE OR NEGATIVE
AIM:
ALGORITHM:
1. Get the number as num. 2. Check if num is greater than 0 , if yes display the given number is negative. 3. Else display the given number is negative.
read a
if [ $a -ge 0 ]
then
else
fi
OUTPUT:
enter a number
-6
-6 is negative
enter a number
4 is positive
RESULT:
EX NO :
DATE:
AIM:
ALGORITHM:
1. Read a and b 2. If a equals b then print numbers are equal. 3. Else print numbers are not equal.
PROGRAM:
read a
read b
if test $a -eq $b
then
else
fi
OUTPUT:
enter a number
RESULT:
EX NO : DATE:
AIM:
ALGORITHM:
PROGRAM:
read n
i=0
while [ $i -lt $n ]
do
i=`expr $i + 1`
echo $i
done
OUTPUT:
RESULT:
EX NO: DATE:
AIM :
ALGORITHM:
1. Enter the two numbers such as and b to be swapped. 2. Assign a to the temp variable t. 3. Assign the value of b to a. 4. Assign the temp value to b. 5. Display the value of a and b.
PROGRAM
read a b
t=$a
a=$b
b=$t
34
a=4 b=3
RESULT
EX NO: DATE:
AREA OF CIRCLE
AIM:
ALGORITHM:
PROGRAM:
readonly pi
read radius
OUTPUT:
area: 452.16
circumference: 75.36
RESULT: