semester 2 lab manual 1
TRANSCRIPT
![Page 1: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/1.jpg)
185253 COMPUTER PRACTICE LAB-II
![Page 2: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/2.jpg)
TABLE OF CONTENTS
S.NO. NAME OF THE EXPERIMENT PAGE NO.
1 STUDY OF UNIX OPERATING SYSTEM
2 VI EDITOR
3 BASIC UNIXCOMMANDS
4 SHELL PROGRAM
ARITHMETIC OPERATION USING SHELL PROGRAM
5 GREATEST OF THREE NUMBERS
6 CHECKING WHETHER THE GIVEN NUMBER IS ARMSTRONG OR NOT
2
![Page 3: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/3.jpg)
8 8.a. SWAPPING OF VALUES (Using 3rd Variable)
8.b. SWAPPING OF VALUES (Without using 3rd Variable)
9 NUMBER OF DIGITS IN AN INTEGER
10 ARITHMETIC OPERATIONS USING SHELL PROGRAMMING
(Using SWITCH - CASE)
11 FACTORIAL OF A NUMBER
12 CHECKING FOR PRIME NUMBER
S.NO. NAME OF THE EXPERIMENT PAGE NO.
13 SUMS OF DIGITS OF AN INTEGER
14
C PROGRAMMING UNDER UNIX ENVIRONMENT
3
![Page 4: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/4.jpg)
MATRIX ADDITION
15 SORTING OF INTEGERS
16 EMPLOYEE DETAILS USING STRUCTURES
17 FILE MANIPULATION
18 GREAEST AND SMALLEST NUMBER IN AN ARRAY
19 TO CHECK WHETHER THE NUMBER IS A PALINDROME OR NOT
20 SYSTEM CALL FOR A READ – ONLY FILE
21 SYSTEM CALL FOR WRITE-ONLY FILE
4
![Page 5: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/5.jpg)
22 IMPLEMENTATION OF COPY COMMAND
23 IMPLEMENTATION OF WC COMMAND
24 FORK SYSTEM CALL
25 EXECL SYSTEM CALL
5
![Page 6: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/6.jpg)
STUDY OF UNIX OPERATING SYSTEM
Ex.No: 01
AIM:
To understand the basic definitions and features of Unix operating system.
Unix stands for unplaced information computing system.
6
![Page 7: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/7.jpg)
VERSION OF UNIX:
T&T Bell laboratories: The operating system originated at the Bell laboratories.
VERSION:
The first commercially licensed version.
PWB/UNIX:
Programmers were bench this version includes specialized utilities to managing software development/
UNIX SYSTEM III:
The version includes as updated set of all the facilities of the passed two versions. It was the first Unix versions to be transported to a micro computer.
UNIX SYSTEM I:
It includes performance importance and enhanced process to process communication mostly one system III.
UNIVERSITY OF CALIFORNIA:
BSD - 4.1 Software development BSD - 4.2 BSD - 4.3
(i) MULTIUSER: Unix is a multi-user OS which permits more than one user to utilize the computer simultaneously any one of the connected terminals.
(ii) MULTI-TASKING:
7
![Page 8: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/8.jpg)
The term task refers to a place initiated by the user to a process initiated by user Unix offer concept of classifying the task.
1. FOREGROUND TASK: It is the one where the user has to wait for the completion to present task before initiating the next task.
2. BACKGROUND TASK:
It is the one whose the user can continue interaction with the system while one or more of the presently issued task if is still executing.
3. PORTABLE: The application programs written for one system works on another system.
4. MODULATOR: It means the various part of the Unix system can be adder on the reproved without editing
performance of other component.
5. FILE STRUCTURE:
Unix has hierarchical file structure and imported free structure.
6. SECURITY:
Unix being a multi using operating system offers protection to user information from another. It maintains a list of users who are all allowed to access the system. The system also keep takes of that file and resources each user is authorized to renew user’s name to be added to the list before they have an access to the system.
TYPES OF USERS:
User is the owner of the file
8
![Page 9: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/9.jpg)
The owner of the file is the one who has created the file or the one to the ownership has been transferred by the creates of the file.
GROUPS:
The members of the group have the same group I.D. but different user I.D.
OTHERS:
The other member who is neither the user nor the member of the group. Unix is a communication operating system. It is three level operating system.
Communication between terminals connected to the computer.
Communication between two compare of some place which cannot use the same either same hardware as software through local area network.
Communication between two computers at the remote location.
1. SHELL:
The shell is the Unix system command’s inter plater. The two important shells are
Bourse shell
C shell
2. KERNEL:
Kernel has the direct interaction with the hardware. It controls functions like file management-1, system, security of file, process management, scheduling management.
RESULT:
Thus the Unix environment has been studied.
VI EDITOR
9
![Page 10: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/10.jpg)
Ex.No: 02
AIM:
To understand the basic functions and features of the VI editor
INSTRUCTIONS:
VI EDITOR- Visual editor
VI stands for Visual
The VI editor is the text editor. Originally developed for Unix programmers.
It is a model meaning. It is operating in the three different modes. The VI editor has been
Around for decade and is used daily by thousands of Linux and Unix users.
THREE ASPECTS OF VI:
VI is supplied with all Unix serpents VI can be used at other universities
or any business with UNIX system.
VI is a amount of memory allows efficient operation when the network is busy.
VI uses standard alphabetic keys for commands.
DIFFERENT MODES IN VI EDITOR
They are three different modes are available.
10
![Page 11: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/11.jpg)
1. Command mode
2. Insert task
3. Last line or escape mode
1. COMMAND MODE
When the first start editing a file with the VI editor we will be VI connected
mode…. In this mode we can issue many VI commands, including insert, append delete, other search and navigation commands the let you move around your file.
2.INSERT MODE
Once we issue a VI editor insert append or open command we will be in VI insert mode. If we are working with a mode VI is typically configure to show the current mode of operation.
At this point, we can
A. Type text into our file
B. Uses of the arrow keys
we can easily move back to command mode by esc.
Cpy- Copying the current line.
Q - Heading
I - Insert the word.
11
![Page 12: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/12.jpg)
3.LAST LINE MODE OR ESCAPE MODE:
The last VI mode is known as VI last line mode we can only get to last line mode from command mode we get into this mode by pressing the colon key.
After pressing the key, we will see a colon character appears at the beginning of the last line of our VI editor window and cursor will be moved to the position.
: w -to save our file but not quit VI.
: q -to quit if you haven’t left mode any edit.
:wq -to quit and save edits.
12
![Page 13: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/13.jpg)
RESULT:
Thus the basic things about the editor and the 8 modes in VI editor had been studied.
BASIC UNIXCOMMANDS
Ex.No: 03
AIM:
To execute and implement the basic Shell UNIX commands.
1. DIRECTORY HANDLING COMMANDS AND THEIR OUTPUT:
1. [root@localhost root]# mkdir ram Creates a directory named “ram”2. [root@localhost root]# cd arun Enters into the directory “arun”
Example:
[ece@localhost ece]$ cd arun
[ece@localhost arun]$
3. [root@localhost arun]# cd .. Comes out of the directory “arun”[root@localhost root]
Example:
13
![Page 14: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/14.jpg)
[ece@localhost ece]$ cd arun
[ece@localhost arun]$ cd ..
[ece@localhost ece]$
4. [root@localhost root]# pwd Shows the present working directory
Example:
[ece@localhost ece]$ pwd
/home/ece
5.[root@localhost root]# rmdirUsed to remove a directory
6.[root@localhost root]# ls Shows the list of files & directories
EXAMPLE:
[ece@localhost ece]$ ls
kanth
kar
kart
karth
karthick
karthik
karti
kat
katheeja
kathi
7.[root@localhost root]# ls -l List the permission of files / Directories
total 4
-rw-r--r-- 1 root root 1093 Mar 11 2006 anaconda-ks.cfg
-rw-r--r-- 1 root root 223 Mar 9 18:04 arith.sh
14
![Page 15: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/15.jpg)
-rw-r--r-- 1 root root 260 Mar 9 18:18 arm.sh
drwxr-xr-x 2 root root 4096 Mar 9 19:24 barnabas
2. FILE HANDLING COMMANDS.
1) [root@localhost root]# cat > msb Creates a New file “msb”I studying B.E ECE.
2) [root@localhost root]# cat msb Opens the created file “msb"I studying B.E ECE.
3) [root@localhost root]# cat >> msb Appends the details in the file “msb”I'm studying in Veltech Engineering college
4) [root@localhost root]# cat msbMy Name is Barnabas
I'm studying in Veltech Engineering college
5) [ece@localhost ece]$ cp ece1 ece2To create duplicate copies [ece@localhost ece]$ cat ece1
a
b
c
d
[ece@localhost ece]$ cat ece2
a
b
15
![Page 16: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/16.jpg)
c
d
6) [ece@localhost ece]$ ln ece7 ece8To link two files
[ece@localhost ece]$ cat ece8
Iam studying in vel tech college
i am in ece dept
7) [root@localhost root]# rm msb Removes the file “msb”
rm: remove regular file `msb'? y
8)(i) [root@localhost root]# wc msb Shows the no. of lines, words & characters.
2 10 65 msb
(ii)[root@localhost root]# wc -l msb Shows the no. of lines alone.
2 msb
(iii)[root@localhost root]# wc -w msb Shows the no. of words alone.
10 msb
(iv)[root@localhost root]# wc -c msb Shows the no. of characters alone.
65 msb
9) [ece@localhost ece]$ mv ece9 ece0 ls to move ordinary and directory files
[ece@localhost ece]$ cat ece0
Iam studying in vel tech college
16
![Page 17: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/17.jpg)
i am in ece dept
[ece@localhost ece]$ cat ece9
cat: ece9: No such file or directory
10) [ece@localhost ece]$ find ece8To find a file ece8
3. PATTERN SEARCHING COMMANDS.
1) [root@localhost root]# grep m msb Prints the sentence with the letter ‘m’ My Name is sudha
I'm studying in vel tech engineering college
2) [ece@localhost ece]$ egrep "are|our" yamuna1 to search for the multiple pattern we belong to our vel tech tech college
we are of ece students
3) [ece@localhost ece]$ fgrep "stands for" yamuna1 to extract only the fixed string without the use of any regular expression.
ece stands for electronic and conmmunication engineering
4. GENERAL PURPOSE COMMANDS.
I ) SIMPLE COMMANDS.
1. [root@localhost root]# cal Prints the calendar if the present month
March 2006
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
17
![Page 18: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/18.jpg)
19 20 21 22 23 24 25
26 27 28 29 30 31
2. [root@localhost root]# date Prints the current time.
Thu Mar 9 19:26:27 IST 2006
3. [root@localhost root]# time –l Prints the System Run-time
real 0m0.005s
user 0m0.000s
sys 0m0.000s
4. [root@localhost root]# man cal Opens manual on “cal”
[ece@localhost ece]$ man cp
CP(1) FSF CP(1)
NAME
cp - copy files and directories
SYNOPSIS
cp [OPTION]... SOURCE DEST
cp [OPTION]... SOURCE... DIRECTORY
cp [OPTION]... --target-directory=DIRECTORY SOURCE...
DESCRIPTION
Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.
18
![Page 19: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/19.jpg)
Mandatory arguments to long options are mandatory for short options
too.
-a, --archive
same as -dpR
--backup[=CONTROL]
make a backup of each existing destination file
-b like --backup but does not accept an argument
:
5. [root@localhost root]# bc Open the Binary Calculator
bc 1.06
Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
123 + 246
369
10 * 20
200
ii) STATUS INFORMATION COMMANDS
1. [ece@localhost ece]$ who –b Displays the date & time the system was brought up.
system boot Feb 23 23:02
19
![Page 20: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/20.jpg)
2. [ece@localhost ece]$ who –s Displays the short form listing which consists of the username,terminal number and time fields.
ece pts/2 Feb 23 23:03 (10.0.0.5)
ece pts/1 Feb 23 23:03 (10.0.0.10)
ece pts/4 Feb 23 23:03 (10.0.0.36)
ece pts/6 Feb 23 23:04 (10.0.0.135)
ece pts/0 Feb 23 23:04 (10.0.0.119)
ece pts/3 Feb 23 23:04 (10.0.0.49)
ece pts/5 Feb 23 23:05 (10.0.0.137)
3. [ece@localhost ece]$ who –u Restricts listing to users currently logged in.ece pts/2 Feb 23 23:03 00:02 1723 (10.0.0.5)
ece pts/1 Feb 23 23:03 00:02 1674 (10.0.0.10)
ece pts/4 Feb 23 23:03 . 1993 (10.0.0.36)
ece pts/6 Feb 23 23:04 00:01 1998 (10.0.0.135)
ece pts/0 Feb 23 23:04 . 2101 (10.0.0.119)
ece pts/3 Feb 23 23:04 . 2149 (10.0.0.49)
ece pts/5 Feb 23 23:05 . 2223 (10.0.0.137)
4. [root@localhost root]# who am I Identifies the current working Systemroot pts/0 Mar 9 19:21 (:0.0)
5. [ece@localhost ece]$ ps –a Display the status of all the process associated with –u,g options
PID TTY TIME CMD
2311 pts/7 00:00:00 mail
20
![Page 21: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/21.jpg)
2312 pts/3 00:00:00 ps
6. [ece@localhost ece]$ ps –g Display Inforamtion of all process of group loads specified in ggrplist
PID TTY STAT TIME COMMAND
2152 pts/3 S 0:00 -bash
2344 pts/3 R 0:00 ps –g
7. [ece@localhost ece]$ ps –l Lists the information using long format.F S UID PID PPID C PRI NI ADDR SZWCHAN TTY TIME CMD
4 S 502 2152 2149 0 75 0 - 1066 wait4 pts/3 00:00:00 bash
0 R 502 2348 2152 0 80 0 - 764 - pts/3 00:00:00 ps
8. [ece@localhost ece]$ tty It will retun the pathname with your terminal/dev/pts/3
9. [ece@localhost ece]$ id To Display the Numerical value that corresponda to your login name.uid=502(ece) gid=502(ece) groups=502(ece)
10. [ece@localhost ece]$ uname –a To display the relevant details about the Operating System on the standard output.Linux localhost.localdomain 2.4.20-6smp #1 SMP Thu Feb 27 09:59:40 EST 2003 i686 i686 i386 GNU/Linux
11. [ece@localhost ece]$ finger ece Gives you more Information about the User.Login: ece Name: (null)
Directory: /home/ece Shell: /bin/bash
On since Tue Feb 23 23:03 (IST) on pts/2 from 10.0.0.5
5 minutes 38 seconds idle
On since Tue Feb 23 23:03 (IST) on pts/1 from 10.0.0.10
21
![Page 22: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/22.jpg)
5 minutes 38 seconds idle
On since Tue Feb 23 23:03 (IST) on pts/4 from 10.0.0.36
7 seconds idle
On since Tue Feb 23 23:04 (IST) on pts/6 from 10.0.0.135
4 minutes 54 seconds idle
On since Tue Feb 23 23:04 (IST) on pts/0 from 10.0.0.119
3 minutes 32 seconds idle
On since Tue Feb 23 23:04 (IST) on pts/3 from 10.0.0.49
On since Tue Feb 23 23:05 (IST) on pts/5 from 10.0.0.137
4 minutes 2 seconds idle
On since Tue Feb 23 23:06 (IST) on pts/7 from 10.0.0.119
7 seconds idle
On since Tue Feb 23 23:07 (IST) on pts/8 from 10.0.0.34
2 minutes 12 seconds idle
New mail received Tue Feb 23 23:08 2010 (IST)
Unread since Tue Feb 23 22:44 2010 (IST)
No Plan.
iii ) COMMUNICATION COMMANDS
1. [root@localhost root]# wall Hai To send message “hai” to all users there who are currently logged in.
Hai
2. [ece@localhost ece]$ mail ece To send the textual messages electrically transferred from one user to another.
Subject: Hai.
How are you.
22
![Page 23: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/23.jpg)
All the best for your exams.
Have a nice day.
3. [ece@localhost ece]$ mail Read your mailMail version 8.1 6/6/93. Type ? for help.
"/var/spool/mail/ece": 1 messages 1 new
a) >N 1 [email protected] Tue Feb 23 23:06 18/720 "Hai."
&
Message 1:
From [email protected] Tue Feb 23 23:06:23 2010
Date: Tue, 23 Feb 2010 23:06:22 +0530
From: [email protected]
Subject: Hai.
How are you.Have you prepared for your exams.
ALL THE BEST FOR YOUR EXAMS.
Have a nice day.
b) & reply To send the reply to the specified user
To: [email protected] [email protected]
Subject: Re: Hai.
Thanks
Cc: & cse
Unknown command: "cse"
c)& exit Exit from the mail
d)&type Print the mail message
e)&delete Delete from the mail box
23
![Page 24: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/24.jpg)
OTHER SHELL COMMANDS
I ) INPUT AND OUTPUT REDIRECTION COMMANDS
1. [root@localhost root]# > To redirect the output of a cammmand to a file.[ece@localhost ece]$ date>a1
[ece@localhost ece]$ cat a1
Tue Feb 23 22:59:03 IST ece@localhost ece]$
2. [ece@localhost ece]$ >> To append the output at the end of a file[ece@localhost ece]$ cat science
date
time
[ece@localhost ece]$ cat tech
one
two
[ece@localhost ece]$ cat science>>tech
[ece@localhost ece]$ cat tech
one
two
date
time
[ece@localhost ece]$
3. [ece@localhost ece]$< To redirect the output of a command to a file.[ece@localhost ece]$ cat<department
24
![Page 25: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/25.jpg)
CSE
EEE
[ece@localhost ece]$
4. [ece@localhost ece]$ < >To redirect the output and input command to a file
[ece@localhost ece]$ cat<department>dnames
[ece@localhost ece]$ cat dnames
CSE
EEE
[ece@localhost ece]$
II ) SECURITY COMMANDS
1.[ece@localhost ram]$ ls –l To list files & Directories in long format
total 3
-rw-rw-r-- 1 ece ece 30 Feb 9 23:47 lop
-rwx------ 1 ece ece 30 Feb 9 23:43 pal
-rw-rw-r-- 1 ece ece 65 Feb 23 23:16 science
2. ece@localhost ram]$ chmod -ux science (-) denotes removing the permission
(u)denotes the user acess
(x) denotes executing permissions
[ece@localhost ram]$ ls -l
total 3
-rw-rw-r-- 1 ece ece 30 Feb 9 23:47 lop
25
![Page 26: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/26.jpg)
-rwx------ 1 ece ece 30 Feb 9 23:43 pal
---------- 1 ece ece 65 Feb 23 23:16 science shows
Missing Permissions
3)[ece@localhost ram]$ chmod u+r,u+w science (+) denotes assigning
Permissions
(w) denotes writing permissions
(r)denotes reading permissions
[ece@localhost ram]$ ls -l
total 3
-rw-rw-r-- 1 ece ece 30 Feb 9 23:47 lop
-rwx------ 1 ece ece 30 Feb 9 23:43 pal
-rw------- 1 ece ece 65 Feb 23 23:16 science the user now has reading writing permission
4) [ece@localhost ram]$ chmod u=x science (=)denotes assigning absolutely
[ece@localhost ram]$ ls -l
total 3
-rw-rw-r-- 1 ece ece 30 Feb 9 23:47 lop
-rwx------ 1 ece ece 30 Feb 9 23:43 pal
---------- 1 ece ece 65 Feb 23 23:16 science
5) [ece@localhost ram]$ chmod g+r,g+w science (g)denotes group accessibility
[ece@localhost ram]$ ls -l
total 3
-rw-rw-r-- 1 ece ece 30 Feb 9 23:47 lop
26
![Page 27: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/27.jpg)
-rwx------ 1 ece ece 30 Feb 9 23:43 pal
----rw---- 1 ece ece 65 Feb 23 23:16 science the group
has now permission to access
6) [ece@localhost ram]$ chmod o+r,o+w science (o)denotes other user
accessibility
[ece@localhost ram]$ ls -l
total 3
-rw-rw-r-- 1 ece ece 30 Feb 9 23:47 lop
-rwx------ 1 ece ece 30 Feb 9 23:43 pal
----rw-rw- 1 ece ece 65 Feb 23 23:16 science
7) [ece@localhost ram]$ chmod a+r,a+x science (a)denotes all user accessibility
[ece@localhost ram]$ ls -l
total 3
-rw-rw-r-- 1 ece ece 30 Feb 9 23:47 lop
-rwx------ 1 ece ece 30 Feb 9 23:43 pal
-r-xrwxrwx 1 ece ece 65 Feb 23 23:16 science
III) WILD CARD PATTERNS
1. [ece@localhost ansatz]$ ls m* To representing any number of characters used in the prefix or suffix.
[ece@localhost raja]$ ls
kanna mvj mvs student vikram vikram]
[ece@localhost raja]$ ls m*
27
![Page 28: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/28.jpg)
mvj:
mvs:
[ece@localhost raja]$
[ece@localhost raja]$ ls
kanna mvj mvs student vikram vikram]
2. [ece@localhost raja]$ ls mv? To represent one character only either in the prefix or in the suffix of the file name
[ece@localhost raja]$ ls
kanna mvj mvs student vikram vikram]
[ece@localhost raja]$ ls mv?
mvj:
mvs:
[ece@localhost raja]$ ls
3. [ece@localhost raja]$ ls sh[1-4] To access a subset of related files
[ece@localhost raja]$ ls
kanna mvj mvs sh1 sh2 sh3 sh4 sh5 student vikram vikram]
[ece@localhost raja]$ ls sh[1-4]
sh1:
sh2:
sh3:
sh4:
[ece@localhost raja]$
IV ) PIPES AND FILTERS COMMANDS
28
![Page 29: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/29.jpg)
1. [ece@localhost ece]$ who |wc The output of “who”is taken as the input for the
“wc” and result is displayed.
9 54 426
2. [ece@localhost ece]$ who | tee names | wc –l The Use of tee command is to
store the intermediate results in a file.
16
[ece@localhost ece]$ cat names
ece pts/4 Feb 23 23:56 (10.0.0.135)
ece pts/17 Feb 24 00:05 (10.0.0.221)
ece pts/10 Feb 24 00:09 (10.0.0.15)
ece pts/13 Feb 24 00:13 (10.0.0.180)
ece pts/1 Feb 24 00:14 (10.0.0.49)
ece pts/24 Feb 24 00:14 (10.0.0.6)
ece pts/3 Feb 24 00:17 (10.0.0.36)
ece pts/30 Feb 24 00:19 (10.0.0.137)
ece pts/7 Feb 24 00:19 (10.0.0.34)
ece pts/28 Feb 24 00:22 (10.0.0.5)
ece pts/20 Feb 24 00:23 (10.0.0.120)
ece pts/21 Feb 24 00:24 (10.0.0.3)
ece pts/31 Feb 24 00:25 (10.0.0.119)
ece pts/34 Feb 24 00:25 (10.0.0.14)
ece pts/32 Feb 24 00:27 (10.0.0.4)
29
![Page 30: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/30.jpg)
3. [ece@localhost ece]$ head -5 names To Display the First Five lines from a
File.
jack
arun
prashant
deepak
naga
[ece@localhost ece]$
4. [ece@localhost ece]$ tail -5 names To Display the Last Five lines from a
File.
ajith
superman
spiderman
goten
vegetables
5. (i)[root@localhost root]# sort msb1 Sorts in ascending order
Aravind
Balaji
Dinesh
Ganesh
Kamalesh
(ii) [root@localhost root]# sort -r msb1 Sorts in descending order
Kamalesh
30
![Page 31: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/31.jpg)
Ganesh
Dinesh
Balaji
Aravind
6.[root@localhost root]# pr To display the Page number,File Creation date and
along with the name of the file.
[ece@localhost ece]$ pr g1
2010-02-25 02:38 g1 Page 1
echo ENTER THE NAME
read n
echo-n THE ENTERED NAME IS $n
7.[root@localhost root]# cut To cut the selected fields.
[ece@localhost ece]$ cat college
diva vh2346
jan vh2345
kavi vh2387
naru vh2876
subi vh2498
[ece@localhost ece]$ cut -c2 college
i
a
a
31
![Page 32: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/32.jpg)
a
u
[ece@localhost ece]$
8.[root@localhost root]# paste To paste the contenet from one to another file.
[ece@localhost ece]$ cat college1
dhoni vh2376
raina vh2398
raj vh2270
subi vh2498
jan vh2345
[ece@localhost ece]$ cat college2
divi vh2334
raina vh2398
raj vh2270
[ece@localhost ece]$ paste -d% college1 college2
dhoni vh2378
divi vh2334
raina vh2397
raina vh2398
raj vh2270
raj vh2270
subi vh2498%
jan vh2345
32
![Page 33: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/33.jpg)
9.[root@localhost root]# join To extract the common lines from two sorted Files.
[ece@localhost ece]$ cat college1
dhoni vh2376
raina vh2398
raj vh2270
subi vh2498
jan vh2345
[ece@localhost ece]$ cat college2
divi vh2334
raina vh2398
raj vh2270
[ece@localhost ece]$ join –v1 college1 college2
dhoni vh2376
subi vh2498
jan vh2345
[ece@localhost ece]$
10.[root@localhost root]# uniq To Displays the adjacent duplicate lines in a existing file
[ece@localhost ece]$ cat tech3
nithu vh2272
uma vh2297
nithu vh2272[ece@localhost ece]$
[ece@localhost ece]$ uniq -c2 tech3
3 nithu vh2272
[ece@localhost ece]$
33
![Page 34: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/34.jpg)
11.[root@localhost root]# nl Used to add Line Numbers to a File.
[cse@localhost cse]$ nl dept
1 cse
2 ece
3 eee
4 it
5 mech
[cse@localhost cse]$ nl -v4 deptstarts the numbering of each page
by 4
4 cse
5 ece
6 eee
7 it
8 mech
[cse@localhost cse]$ nl –i3 deptincrements number of lines by 3.
1 cse
4 ece
7 eee
10 it
13 mech
12.[root@localhost root]# tr It is used to translate characters taken from the standard input.
[cse@localhost cse]$ cat dept
cse
ece
34
![Page 35: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/35.jpg)
eee
it
mech
[cse@localhost cse]$ cat dept|tr "[a-z]" "[A-Z]"
CSE
ECE
EEE
IT
MECH
13.[root@localhost root]# pg It displays the output of a command on the screen
page by page.
14.[root@localhost root]# more To Displays the output page by page
[ece@localhost ece]$ ls | more
0
00
000123
003
0045
007
1
10
100
10001
101
35
![Page 36: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/36.jpg)
12
122
123
1234
1234567890
124
132
133
143
159
18
--More— Press Enter to see the next page.
15.[root@localhost root]# cmp Compare any two files, including binary files. It
takes two filenames as arguments.
[ece@localhost ece]$ cat > college
diva vh2346
jan vh2345
kavi vh2387
naru vh2876
subi vh2498
[ece@localhost ece]$ cat > college1
DHONI VH2876
RAINA VH2398
KAIF VH2657
36
![Page 37: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/37.jpg)
HARI VH2765
SRI VH4567
[ece@localhost ece]$ cmp college college1
college college1 differ: byte 1, line 1
[ece@localhost ece]$ cat college[ece@localhost ece]$ cmp -b college college1
college college1 differ: byte 1, line 1 is 144 d 104 D
16.[root@localhost root]# diff Files are compared on line by line basis.
[ece@localhost ece]$ cat college
diva vh2346
jan vh2345
kavi vh2387
naru vh2876
subi vh2498
[ece@localhost ece]$ cat college1
dhoni vh2387
raina vh2389
subi vh2498
sachin vh2564
diva vh2346
[ece@localhost ece]$ diff -e college college1
1,5c
dhoni vh2387
raina vh2389
subi vh2498
37
![Page 38: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/38.jpg)
sachin vh2564
diva vh2346
[ece@localhost ece]$ diff -b college college1
1,4c1,2
< diva vh2346
< jan vh2345
< kavi vh2387
< naru vh2876
---
> dhoni vh2387
> raina vh2389
5a4,5
> sachin vh2564
> diva vh2346
17.[root@localhost root]# comm Compare and displays lines common as well as
unique to two files.
[ece@localhost ece]$ comm -1 college college1
dhoni vh2387
raina vh2389
subi vh2498
sachin vh2564
diva vh2346
[ece@localhost ece]$ comm -13 college college1
dhoni vh2387
38
![Page 39: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/39.jpg)
raina vh2389
subi vh2498
sachin vh2564
diva vh2346
[ece@localhost ece]$ comm -23 college college1
diva vh2346
jan vh2345
kavi vh2387
naru vh2876
subi vh2498
RESULT:
Thus the Basic Shell Commands were executed.
ARITHMETIC OPERATION USING SHELL PROGRAM
Ex.No: 04
AIM:
To write a shell Program to do the Arithmetic Operations like Addition, Subtraction, Multiplication, Division.
39
![Page 40: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/40.jpg)
ALGORITHM:
Step 1: Start
Step 2: Read the values of ‘a’ and ‘b’
Step 3: Write the expression to calculate sum, difference, Product, Quotient and Remainder.
Step 4: Print the Results
Step 5: Stop.
SHELL PROGRAM:
echo "Enter Two Numbers"
read a b
c=`expr $a + $b`
d=`expr $a - $b`
e=`expr $a \* $b`
f=`expr $a / $b`
g=`expr $a % $b`
echo "Sum = $c"
echo "Difference = $d"
echo "Product = $e"
echo "Quotient = $f"
echo "Remainder = $g"
SAMPLE OUTPUT:
40
![Page 41: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/41.jpg)
Enter Two Numbers
12 10
Sum = 22
Difference = 2
Product = 120
Quotient = 1
Remainder = 2
RESULT:
Thus the shell Program to do arithmetic operation was executed without any errors.
GREATEST OF THREE NUMBERS
Ex. No.: 05
AIM:
To write a shell program to find the greatest of three Numbers.
ALGORITHM:
Step 1: Start
Step 2: Read the value of ‘a’, ‘b’ and ‘c’
Step 3: Check if ‘a’ is greater than ‘b’ and ‘a’ is greater than ‘c’
Step 4: Then print “A is Greater”
Step 5: If Step 3 is not satisfied check if ‘b’ is greater than ‘c’
41
![Page 42: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/42.jpg)
Step 6: Then print “B is Greater”
Step 7: Else Print “C is greater”
Step 8: Stop.
SHELL PROGRAM:
echo "Enter Three Numbers"
read a b c
if [ $a -gt $b -a $a -gt $c ]
then
echo "$a is Greater"
elif [ $b -gt $c ]
then
echo "$b is Greater"
else
echo "$c is Greater"
fi
SAMPLE OUTPUT:
Enter Three Numbers
12 14 16
16 is Greater
42
![Page 43: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/43.jpg)
RESULT:
Thus the program to find the greatest of three Numbers was executed and the output was verfied.
CHECKING WHETHER THE GIVEN NUMBER IS ARMSTRONG OR NOT
Ex. No.: 06
AIM:
To check whether the given Number is Armstrong Number or not using Shell Programming.
ALGORITHM:
Step 1: Start
Step 2: Read the value of ‘num’
Step 3: Store the value of ‘num’ in ‘x’ and initialize the value of sum = 0.
Step 4: When the value of ‘num’ is greater then 0, solve the following expressions
Step 5: y = num/10; z=y*y*y; sum=num+z; num=num/10
Step 6: Close the While Loop
43
![Page 44: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/44.jpg)
Step 7: Check if ‘x’ is equal to ‘sum’
Step 8: Print the Result
Step 9: Stop.
SHELL PROGRAM:
44
![Page 45: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/45.jpg)
echo "Enter a Number"
read num
x=$num
sum=0
while [ $num -gt 0 ]
do
y=`expr $num % 10`
z=`expr $y \* $y \* $y`
sum=`expr $sum + $z`
num=`expr $num / 10`
done
if [ $x -eq $sum ]
then
echo "$x is an armstrong Number"
else
echo "$x is not an armstrong Number
Enter a Number
153 is an armstrong Number
[root@localhost root]# sh arm.sh
Enter a Number
123
123 is not an armstrong Number
45
![Page 46: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/46.jpg)
RESULT:
Thus the Shell Program to check whether the given Number is Armstrong or not was executed and the output was verified.
FIBONACCI SERIES
Ex No.: 07
AIM:
To write a Shell Program to print the Fibonacci series.
ALGORITHM:
Step 1: Start
Step 2: Read the Value of ‘n’
Step 3: Initialize i=2; a=0; b=1.
46
![Page 47: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/47.jpg)
Step 4: Print the Value of ‘a’ and ‘b’
Step 5: When ‘i’ is lesser than ‘n’, solve the expression c=a+b
Step 6: Print c
Step 7: Swap the Values of ‘b’ to ‘a’ and ‘c’ to ‘b’
Step 8: Solve the expression “i=i+1”
Step 9: Stop.
47
![Page 48: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/48.jpg)
SHELL PROGRAM:
echo "Enter the Limit"
read n
i=2
echo "Fibonacci Series"
echo "----------------"
a=0
b=1
echo $a
echo $b
while [ $i -lt $n ]
do.
c=`expr $a + $b`
echo $c
a=$b
b=$c
48
![Page 49: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/49.jpg)
i=`expr $i + 1`
done
SAMPLE OUTPUT:
Enter the Limit
8
Fibonacci Series
----------------
0
1
1
2
3
5
8
13
49
![Page 50: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/50.jpg)
RESULT:
Thus the Shell Program to print the Fibonacci Series was executed and the output was verified.
SWAPPING OF VALUES
(Using 3 rd Variable)
Ex. No.: 08(a)
AIM:
To write a Shell Program to swap two values using 3rd variable.
ALGORITHM:
Step 1: Read the Values of ‘a’ and ‘b’
Step 2: Swap the values of ‘a’ to the third variable ‘temp’
Step 3: Swap the value of ‘b’ to ‘a’ and ‘b’ to ‘temp’
Step 4: Print the swapped values of ‘a’ and ‘b’
Step 5: Stop.
SHELL PROGRAM:
50
![Page 51: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/51.jpg)
echo "Enter Two Numbers"
read a b
temp=$a
a=$b
b=$temp
echo $a $b
SAMPLE OUTPUT:
Enter Two Numbers
12 10
10 12
RESULT:
Thus the program to swap two values using 3rd variable was executed and the output was verified.
SWAPPING OF VALUES
51
![Page 52: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/52.jpg)
(Without using 3 rd Variable)
Ex. No.: 08(b)
AIM:
To write a Shell Program to swap two values without using 3rd Variable.
ALGORITHM:
Step 1: Start
Step 2: Read the values of ‘a’ and ‘b’
Step 3: Solve the expression, a=a + b; b=a – b; a=a – b
Step 4: Print the swapped values of ‘a’ and ‘b’
Step 5: Stop
SHELL PROGRAM:
echo "Enter Two Numbers"
read a b
a=`expr $a + $b`
52
![Page 53: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/53.jpg)
b=`expr $a - $b`
a=`expr $a - $b`
echo $a $b
SAMPLE OUTPUT:
Enter Two Numbers
12 10
10 12
53
![Page 54: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/54.jpg)
RESULT:
Thus the Shell Program to swap two values without using third variable was executed and the output was verified.
NUMBER OF DIGITS IN AN INTEGER
Ex. No.: 09
AIM:
To count the number of digits in an integer, using Shell Programming.
ALGORITHM:
Step 1: Start
Step 2: Read the value of ‘a’
Step 3: Initialize the value of c as 0
Step 4: When the value of ‘a’ is not equal to 0, solve n=a %10.
54
![Page 55: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/55.jpg)
Step 5: If the value of ‘n’ is not equal to 0, solve c=c + 1(increment)
Step 6: End if condition
Step 7: Solve the expression “a=a / 10”
Step 8: Repeat steps 4 to 7 until ‘a’ is equal to 0
Step 9: Print the value of c as the number of digits in an integer.
Step 10: Stop.
SHELL PROGRAM:
echo "Enter a Number"
read a
c=0
while [ $a -ne 0 ]
do
n=`expr $a % 10`
c=`expr $c + 1`
a=`expr $a / 10`
done
echo "The Number of Digits in the Integer is $c"
SAMPLE OUTPUT:
Enter a Number
123456789
The Number of Digits in the Integer is 9
RESULT:
55
![Page 56: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/56.jpg)
Thus the Shell Program to calculate the number of digits of an integer was executed and the output was verified.
ARITHMETIC OPERATIONS USING SHELL PROGRAMMING
(Using SWITCH - CASE)
Ex. No. 10
AIM:
To write a Shell Program to perform Arithmetic Operations using Switch – Case
ALGORITHM:
Step 1: Start
Step 2: Read the two Numbers.
Step 3: Get the operation choice from the User
Step 4: Give the expressions for each case and solve them.
Step 5: Print the Result
Step 6: Stop
56
![Page 57: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/57.jpg)
SHELL PROGRAM:
echo "Enter Two Numbers"
57
![Page 58: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/58.jpg)
read a b
echo "What do you want to do? (1 to 5)"
echo "1) Sum"
echo "2) Difference"
echo "3) Product"
echo "4) Quotient"
echo "5) Remainder"
echo "Enter your Choice"
read n
case "$n" in
1) echo "The Sum of $a and $b is `expr $a + $b`";;
2) echo "The Difference between $a and $b is `expr $a - $b`";;
3) echo "The Product of the $a and $b is `expr $a \* $b`";;
4) echo "The Quotient of $a by $b is `expr $a / $b`";;
5) echo "The Remainder of $a by $b is `expr $a % $b`";;
esac
SAMPLE OUTPUT:
[root@localhost Shell]# sh arith_switch.sh
Enter Two Numbers
12 10
What do you want to do? (1 to 5)
1) Sum
2) Difference
58
![Page 59: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/59.jpg)
3) Product
4) Quotient
5) Remainder
Enter your Choice
4
The Quotient of 12 by 10 is 1
RESULT:
Thus the Shell program to perform arithmetic operations using Switch Case was executed and the output was verified.
FACTORIAL OF A NUMBER
Ex. No. 11
AIM:
To write a Shell Program to find the factorial of a Number.
ALGORITHM:
59
![Page 60: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/60.jpg)
Step 1: Start
Step 2: Read the Number as ‘n’
Step 3: Assign the value of ‘p’ as 0 and solve the expression i = n-1
Step 4: While the value of ‘i’ is greater than or equal to 1 do the following steps.
Step 5: Solve the expression n=n*1 and i=i-1 (Decrementing Operation)
Step 6: Then print the value of ‘n’ as the factorial of the given number.
Step 7: Stop.
SHELL PROGRAM:
echo "Enter a Number"
read n
i=`expr $n - 1`
p=1
while [ $i -ge 1 ]
do
n=`expr $n \* $i`
i=`expr $i - 1`
done
echo "The Factorial of the given Number is $n"
SAMPLE OUTPUT:
60
![Page 61: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/61.jpg)
[root@localhost Shell]# sh fact.sh
Enter a Number
5
The Factorial of the given Number is 120
RESULT:
Thus the Shell Program to find the factorial of the Number was executed and the output was verified.
CHECKING FOR PRIME NUMBER
Ex. No.: 12
AIM:
To write a Shell Program to check if the Number is a Prime Number or a Composite Number.
ALGORITHM:
Step 1: Start
Step 2: Read the Number as ‘n’
Step 3: Initialize the value of ‘t’ to 0 and solve the expression i=n-1
61
![Page 62: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/62.jpg)
Step 4: While ‘i’ is greater than or equal to 2, perform the following steps.
Step 5: Solve p=n%i
Step 6: Check the condition (p=0) and then solve t=t+1
Step 7: End the If Condition
Step 8: Solve the expression i=i-1
Step 9: If ‘t’ is greater than 0, print “The Number is not a Prime Number”
Step 10: If the condition is not satisfied, print “The Number is a Prime Number”
62
![Page 63: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/63.jpg)
SHELL PROGRAM:
echo "Enter a Number"
read n
i=`expr $n - 1`
t=0
while [ $i -ge 2 ]
do
p=`expr $n % $i`
if [ $p -eq 0 ]
then
t=`expr $t + 1`
fi
i=`expr $i - 1`
done
if [ $t -gt 0 ]
then
echo "The Number $n is not a Prime Number"
63
![Page 64: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/64.jpg)
else
echo "The Number $n is a Prime Number"
fi
SAMPLE OUTPUT:
[root@localhost Shell]# sh prime.sh
Enter a Number
2
The Number 2 is a Prime Number
[root@localhost Shell]# sh prime.sh
Enter a Number
4
The Number 4 is not a Prime Number
RESULT:
64
![Page 65: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/65.jpg)
Thus the Shell Program to check if the given number is prime or not, was executed and the output was verified.
SUMS OF DIGITS OF AN INTEGER
Ex. No. 13
AIM:
To write a Shell Program to calculate the sum of digits of an Integer.
ALGORITHM:
Step 1: Start
Step 2: Read the Integer as ‘num’
Step 3: Initialize the value of sum=0.
Step 4: While the value of ‘num’ is greater than 0, solve the following expressions.
Step 5: Find the remainder of ‘num’ by 10 and store in ‘y’
Step 6: Add ‘sum’ with ‘y’ and store it in ‘sum’
Step 7: Divide ‘num’ by 10 and store the value in ‘num’
Step 7: Close the while loop
Step 8: Print the Result
Step 9: Stop
SHELL PROGRAM:
echo "Enter a Number"
65
![Page 66: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/66.jpg)
read num
sum=0
while [ $num -gt 0 ]
do
y=`expr $num % 10`
sum=`expr $sum + $y`
num=`expr $num / 10`
done
echo "The Sum of the Digits of the Integer is $sum"
SAMPLE OUTPUT:
[root@localhost Shell]# sh digits_sum.sh
Enter a Number
123456
The Sum of the Digits of the Integer is 21
RESULT:
Thus the Shell program to calculate the sum of the digits of the integer was executed and the output was verified.
C PROGRAMMING UNDER UNIX ENVIRONMENT
MATRIX ADDITION
66
![Page 67: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/67.jpg)
Ex. No. 14
AIM:
To write a C Program to add two matrices under UNIX Environment
ALGORITHM:
Step 1: Start
Step 2: Declare a[10][10], b[10][10], add[10][10], i, j, m, n.
Step 3: Get the number of rows and columns as m and n respectively
Step 4: Scan the values of the matrices according to the values of m and n.
Step 5: Add the two matrices directly and store the values in ‘add[10][10]’
Step 6: Using ‘for loop’ print the values of add[10][10]
Step 7: End the loop
Step 8: Stop.
67
![Page 68: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/68.jpg)
C PROGRAM:
#include<stdio.h>
main()
{
int add[10][10],a[10][10],b[10][10],i,j,m,n;
printf("\nEnter the Number of Rows and Columns:\n\n ");
scanf("%d%d",&m,&n);
68
![Page 69: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/69.jpg)
printf("Enter the 1st Matrix: \n\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("Enter the 2nd Matrix: \n\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&b[i][j]);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
add[i][j]=a[i][j]+b[i][j];
}
printf("The Addition of the two matrices is: \n");
for(i=0;i<n;i++)
{
printf("\n\n");
for(j=0;j<n;j++)
{
printf("%d\t",add[i][j]);
}
}
}
69
![Page 70: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/70.jpg)
SAMPLE OUTPUT:
[root@localhost C_Program]# cc matadd.c
[root@localhost C_Program]# ./a.out
Enter the Number of Rows and Columns:
2
2
Enter the 1st Matrix:
70
![Page 71: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/71.jpg)
1
2
3
4
Enter the 2nd Matrix:
1
2
3
4
The Addition of the two matrices is:
2 4
6 8
71
![Page 72: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/72.jpg)
RESULT:
Thus the C Program to add two matrices under UNIX Environment was executed and the output was verified.
SORTING OF INTEGERS
Ex. No. 15
AIM:
To write a C Program to sort an array of integers under UNIX environment
ALGORITHM:
Step 1: Start
Step 2: Get the number of array integers as ‘n’
Step 3: Using ‘for loop’, get the ‘n’ number of integers as a[]
Step 4: Again using ‘for loop’ check if the second number is greater than the first
72
![Page 73: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/73.jpg)
Step 5: If the above step is true, swap the value of second to first
Step 6: Repeat Steps 4 and 5 for all the integers scanned using ‘for loop’
Step 7: Again using ‘for loop’ print the sorted list of values.
Step 8: Stop
73
![Page 74: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/74.jpg)
C PROGRAM:
#include<stdio.h>
main()
{
int x[50],n,i,j,temp;
printf("How many Numbers ?\n");
scanf("%d",&n);
printf("\nEnter the List of %d numbers:\n",n);
for(i=0;i<n;i++)
scanf("%d",&x[i]);
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
if(x[i]>x[j])
{
temp=x[i];
x[i]=x[j];
x[j]=temp;
}
printf("\nThe Sorted list of Numbers is \n");
for(i=0;i<n;i++)
74
![Page 75: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/75.jpg)
printf("%d\t",x[i]);
}
SAMPLE OUTPUT:
[root@localhost C_Program]# cc sort.c
[root@localhost C_Program]# ./a.out
How many Numbers ?
5
Enter the List of 5 numbers:
4
8
3
6
2
The Sorted list of Numbers is
2 3 4 6 8
RESULT:
Thus the C Program to sort the given array of integers under UNIX environment was executed and the output was verified.
EMPLOYEE DETAILS USING STRUCTURES
75
![Page 76: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/76.jpg)
Ex. No.: 16
AIM:
To write a C Program to get and print the employee details using Structures under UNIX Platform.
ALGORITHM:
Step 1: Start
Step 2: Initialize the structure with employee no., name and salary.
Step 3: Inside the main function, get the number of employees
Step 4: Using for loop, scan the employee details
Step 5: Again using for loop, print the employee details
Step 6: Stop
76
![Page 77: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/77.jpg)
C PROGRAM:
#include<stdio.h>
struct emp
{
char name[25];
77
![Page 78: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/78.jpg)
int salary;
int empno;
}a[20];
Ma in()
{
int n,i;
printf("\n Enter the Number of Employees: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n\nEnter the %d Employee details",i+1);
printf("\n\n1.Employee No.: ");
scanf("%d",&a[i].empno);
printf("\n2.Name: ");
scanf("%s",&a[i].name);
printf("\n3.Salary: ");
scanf("%d",&a[i].salary);
}
for(i=0;i<n;i++)
{
printf("\n\nEmployee %d",i+1);
printf("\n1.Employee No.: %d",a[i].empno);
printf("\n2.Employee Name: %s",a[i].name);
printf("\n3.Salary: %d",a[i].salary);
}
78
![Page 79: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/79.jpg)
}
SAMPLE OUTPUT:
[root@localhost C_Program]# cc employ.c
[root@localhost C_Program]# ./a.out
Enter the Number of Employees: 2
Enter the 1 Employee details
1.Employee No.: 1234
79
![Page 80: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/80.jpg)
2.Name: Ramesh
3.Salary: 12500
Enter the 2 Employee details
1.Employee No.: 2314
2.Name: Ganesh
3.Salary: 12500
Employee 1
1.Employee No.: 1234
2.Employee Name: Ramesh
3.Salary: 12500
Employee 2
1.Employee No.: 2314
2.Employee Name: Ganesh
3.Salary: 12500
80
![Page 81: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/81.jpg)
RESULT:
Thus the C Program to get and print the employee details using Structures was executed and the output was verified.
FILE MANIPULATION
Ex. No.: 17
AIM:
To write a C Program to perform following file manipulation:
1. Read a file2. Copy a file3. Change the case of the entire file4. Count the number of words, spaces and lines in a file.
C PROGRAM:
#include<stdio.h>
#include<string.h>
main()
81
![Page 82: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/82.jpg)
{
char ch,ct;
int choice,tr,letters=0,words=0,spaces=0,lines=0;
FILE *fp1,*fp2;
do
{
fp1=fopen("data1.txt","r");
printf("\nEnter u'r Choice\n");
printf("\n1.Read the file\n2.Copy the file\n3.Change\n4.Count\n5.Exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
while(!feof(fp1))
{
ch=getc(fp1);
printf("%c",ch);
}
break;
case 2:
fp2=fopen("data2.txt","w");
fseek(fp1,0L,SEEK_SET);
while(!feof(fp1))
{
ct=fgetc(fp1);
82
![Page 83: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/83.jpg)
tr=fputc(ct,fp2);
}
fclose(fp1);
fclose(fp2);
fp2=fopen("data2.txt","r");
printf("The Content of the copied file is ");
while(!feof(fp2))
{
ch=fgetc(fp2);
printf("%c",ch);
}
fclose(fp2);
break;
case 3:
fp2=fopen("data2.txt","r");
while(!feof(fp2))
{
ch=getc(fp2);
if((ch<91)&&(ch>=65))
{
ch=ch+32;
printf("%c",ch);
}
else if((ch>=97)&&(ch<=122))
{
83
![Page 84: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/84.jpg)
ch=ch-32;
printf("%c",ch);
}
else
{
printf("%c",ch);
}
}
break;
case 4:
fp2=fopen("data2.txt","r");
while(!feof(fp2))
{
ch=fgetc(fp2);
if(ch==' ')
{
spaces++;
}
else if (ch=='\n')
{
lines++;
}
else
{
letters++;
84
![Page 85: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/85.jpg)
}
}
printf("\nTotal lines: %d",lines);
printf("\nTotal words: %d",spaces+lines);
printf("\nTotal spaces: %d",spaces);
printf("\nTotal letters: %d",spaces+lines+words);
fclose(fp2);
break;
}
}
while(choice!=5);
}
SAMPLE OUTPUT:
Enter u'r Choice
1.Read the file
2.Copy the file
3.Change
4.Count
5.Exit
1
Wisdom better than the Rubies.
85
![Page 86: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/86.jpg)
?
Enter u'r Choice
1.Read the file
2.Copy the file
3.Change
4.Count
5.Exit
2
The Content of the copied file is Wisdom better than the Rubies.
??
Enter u'r Choice
1.Read the file
2.Copy the file
3.Change
4.Count
5.Exit
3
wISDOM BETTER THAN THE rUBIES.
??
Enter u'r Choice
1.Read the file
2.Copy the file
86
![Page 87: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/87.jpg)
3.Change
4.Count
5.Exit
4
Total lines: 1
Total words: 5
Total spaces: 4
Total letters: 5
87
![Page 88: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/88.jpg)
RESULT:
Thus the FILE MANIPULATION operation was performed and the output was verified.
GREAEST AND SMALLEST NUMBER IN AN ARRAY
Ex.No.: 18
AIM:
To write a C Program to find the greatest and smallest number in an array of integers.
ALGORITHM:
88
![Page 89: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/89.jpg)
Step 1: Start
Step 2: Get the maximum limit for the array as ‘n’
Step 3: Using ‘for’ loop, get the array of integers to the value of ‘n’
Step 4: Again using ‘for’ loop, sole the following conditions.
Step 5: If the first digit is greater than the second, perform the following swap conditions.
Step 6: Swap the value of a[0] to ‘temp’, a[j] to a[i] and a[i] to ‘temp’.
Step 7: Print the result
Step 8: Stop
89
![Page 90: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/90.jpg)
PROGRAM:
#include<stdio.h>
main()
{
int a[100],i,j,n,temp;
printf("\nEnter the maximum limit: ");
scanf("%d",&n);
printf("\nEnter %d numbers: \n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
90
![Page 91: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/91.jpg)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("\nThe smallest number is %d",a[0]);
printf("\nThe biggest number is %d",a[n-1]);
}
SAMPLE OUTPUT:
[root@localhost C_Program]# cc maxmin.c
[root@localhost C_Program]# ./a.out
Enter the maximum limit: 5
Enter 5 numbers:
9
6
2
10
15
The smallest number is 2
91
![Page 92: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/92.jpg)
The biggest number is 15
RESULT:
Thus the C Program to find the greatest and smallest number in an array was executed and the output was verified.
TO CHECK WHETHER THE NUMBER IS A PALINDROME OR NOT
Ex. No.: 19
AIM:
To write a C Program to check if the given number is a palindrome or not
ALGORITHM:
Step 1: Start
Step 2: Get the number as ‘n’
Step 3: Swap the value of ‘n’ to ‘c’.
Step 4: While the value of ‘n’ is greater than or equal to 1, solve the following expressions.
Step 5: Store the remainder of ‘n’ by 10 as ‘a’; Solve b=(b*10)+a; and n=n/10
92
![Page 93: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/93.jpg)
Step 6: If the value of ‘c’ is equal to ‘b’, print “The number is a palindrome”.
Step 7: If the above condition is not satisfied, print “The number is not a palindrome”.
Step 8: Stop.
93
![Page 94: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/94.jpg)
PROGRAM:
#include<stdio.h>
main()
{
int a,c,n,b=0;
printf("\nEnter a number: ");
scanf("%d",&n);
c=n;
while(n>=1)
{
a=n%10;
b=(b*10)+a;
n=n/10;
}
if(c==b)
printf("\nThe number is a palindrome\n");
else
printf("\nThe number is not a palindrome\n");
}
94
![Page 95: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/95.jpg)
SAMPLE OUTPUT:
[root@localhost C_Program]# cc palin.c
[root@localhost C_Program]# ./a.out
Enter a number: 12321
The number is a palindrome
[root@localhost C_Program]# ./a.out
Enter a number: 123
The number is not a palindrome
RESULT:
Thus the C Program to check whether the number is a palindrome or not was executed and the output was verified.
SYSTEM CALL FOR A READ – ONLY FILE
Ex. No.:20
95
![Page 96: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/96.jpg)
AIM:
To write C Program to perform system call to open for a read-only file.
ALGORITHM:
Step 1: Start
Step 2: Initialize the integer variables- i and fd. Also initialize the string variable, buf[100].
Step 3: Open the already created file ‘aa’ as a read only and store its contents in fd.
Step 4: Read fd, buf, 100
Step 5: Using for loop, print the character array, buf[i]
Step 6: The data already stored in ‘aa’ along with the garbage value will be printed as output.
Step 7: Stop
Note:
A file ‘aa’ with data must be created before the program is compiled and is run, to avoid segmentation error.
Header Files:
fcntl.h File control Options
96
![Page 97: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/97.jpg)
unistd.h Standard symbolic constants and types.
PROGRAM:
#include<stdio.h>
#include<fcntl.h>
#include<unistd.h>
main()
{
int i,fd;
char buf[100];
fd=open("aa",O_RDONLY);
97
![Page 98: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/98.jpg)
read(fd,buf,100);
for(i=0;i<100;i++)
{
printf("%c",buf[i]);
}
}
SAMPLE OUTPUT:
[root@localhost C_Program]# cc syscall-r.c
[root@localhost C_Program]# ./a.out
My name is Barnabas
t��<@PB@,X@&8Z@0�@�@c@TB
B
98
![Page 99: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/99.jpg)
RESULT:
Thus the C Program for system call to open a read-only file was executed and the output was verified.
SYSTEM CALL FOR WRITE-ONLY FILE
Ex. No.: 21
AIM:
To write a C Program to perform system call to open a write-only file.
ALGORITHM:
Step 1: Start
Step 2: Initialize the integer variables – ‘i’ and ‘fd’ with a character variable ‘buf[100]’.
Step 3: Get the data form the user and store it as ‘buf’.
99
![Page 100: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/100.jpg)
Step 4: Open the file ‘aa’ as write-only and store it in fd.
Step 5: Write the data given by the user to the file and ‘fd’
Step 6: Stop
Note:
After the program is run, open the file ‘aa’ to find the garbage values stored in it.
A file ‘aa’ with data must be created before the program is compiled and is run, to avoid segmentation error.
Header Files:
fcntl.h File control Options
unistd.h Standard symbolic constants and types.
100
![Page 101: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/101.jpg)
PROGRAM:
#include<stdio.h>
#include<fcntl.h>
#include<unistd.h>
main()
{
int i,fd;
char buf[100];
printf("Give data: ");
scanf("%s",buf);
fd=open("aa",O_WRONLY);
write(fd,buf,sizeof(buf));
}
SAMPLE OUTPUT:
[root@localhost C_Program]# cc syscall-w.c
[root@localhost C_Program]# ./a.out
Give data: I study in VEC
[root@localhost C_Program]# cat aa
101
![Page 102: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/102.jpg)
IBe�B�B
Bt��<@PB@,X@-8Z@0�@�@c@TBB
RESULT:
Thus the C Program to perform system call to opena write-only file was executed and the output was verified.
IMPLEMENTATION OF COPY COMMAND
102
![Page 103: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/103.jpg)
Ex. No.: 22
AIM:
To write a C Program to implement copy command in UNIX.
ALGORITHM:
Step 1: Start
Step 2: Open the file to be copied, and store it in fp1
Step 3: Open a new file, where the contents are to be copied and store it in fp2.
Step 4: Copy all the characters one by one until the End of File.
Step 5: Close all the files.
Step 6: Stop.
103
![Page 104: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/104.jpg)
C PROGRAM:
#include<stdio.h>
main(int a, char *av[2])
{
char ch;
104
![Page 105: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/105.jpg)
FILE *fp1,*fp2;
fp1=fopen(av[1],"r");
fp2=fopen(av[2],"w");
while((ch=getc(fp1))!=EOF)
{
putc(ch,fp2);
}
fclose(fp1);
fclose(fp2);
}
SMAPLE OUTPUT:
[root@localhost C_Program]# cc copy.c
[root@localhost C_Program]# cat>a
Vel Tech Engineering College
[root@localhost C_Program]# ./a.out a b
[root@localhost C_Program]# cat b
Vel Tech Engineering College
105
![Page 106: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/106.jpg)
RESULT:
Thus the C Program to implement copy command in UNIX was executed and the output was verified.
IMPLEMENTATION OF WC COMMAND
Ex. No.: 23
AIM:
To write a C Program to implement wc command in UNIX.
ALGORITHM:
Step 1: Start
106
![Page 107: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/107.jpg)
Step 2: Initialize the variables c=0; w=0; l=0.
Step 3: Using If – Else condition count the no. of characters, letters and words.
Step 4: Using string compare function, print the result with respect to the command given.
Step 5: Stop.
107
![Page 108: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/108.jpg)
C PROGRAM:
#include<stdio.h>
main(int a,char *av[3])
{
FILE *f;
char ch;
int w=0,l=0,c=0;
if(a==2)
f=fopen(av[1],"r");
else
f=fopen(av[2],"r");
while(!feof(f))
{
ch=getc(f);
c=c++;
if(ch==' '||ch=='\n')
108
![Page 109: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/109.jpg)
w=w+1;
if(ch=='\n')
l=l+1;
}
fclose(f);
if(strcmp(av[1],"-c")==0)
printf("\nThe no. of characters are %d\n",c-1);
else if(strcmp(av[1],"-w")==0)
printf("\nThe no. of words are %d\n",w);
else if(strcmp(av[1],"-l")==0)
printf("\nThe number of lines are %d\n",l);
else if(av[2]=='\0')
printf("\n%d\t%d\t%d\n",c-1,w,l);
}
SAMPLE OUTPUT:
[root@localhost C_Program]# cc wc.c
[root@localhost C_Program]# ./a.out msb
77 10 7
[root@localhost C_Program]# ./a.out -l msb
The number of lines are 7
[root@localhost C_Program]# ./a.out -w msb
The no. of words are 10
[root@localhost C_Program]# ./a.out -c msb
The no. of characters are 77
109
![Page 110: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/110.jpg)
RESULT:
Thus the C Program to implement wc commad in UNIX was executed and the output was verified.
FORK SYSTEM CALL
Ex. No.: 24
AIM:
To write a C Program for FORK SYSTEM CALL
ALGORITHM:
Step 1: Start
Step 2: Open the inbuilt function fork() inside the header file, unistd.h and store it in f
Step 3: When f is equal to 0, print the Child process ID, Parent ID and the process ID, using inbuilt ID functions.
Step 4: Stop.
110
![Page 111: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/111.jpg)
111
![Page 112: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/112.jpg)
C PROGRAM:
#include<stdio.h>
#include<unistd.h>
main()
{
int f;
f=fork();
if(f==0)
{
printf("\nChild Process ID: %d\n",getpid());
printf("\nParent ID: %d\n",getppid());
}
else
{
printf("\nProcess ID: %d\n",getpid());
printf("\nParent ID: %d\n",getppid());
}
}
SAMPLE OUTPUT:
[root@localhost C_Program]# cc fork.c
[root@localhost C_Program]# ./a.out
112
![Page 113: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/113.jpg)
Child Process ID: 3844
Parent ID: 3843
Process ID: 3843
Parent ID: 3673
RESULT:
Thus the C Program for Fork System Call in UNIX was executed and the output was verified.
EXECL SYSTEM CALL
Ex. No.: 25
Date: 3rd May, 2006
113
![Page 114: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/114.jpg)
AIM:
To write a C Program for EXECL System Call in UNIX
ALGORITHM:
Step 1: Start
Step 2: Give the command for Execl, to print the Date and Time.
Step 3: Stop
C PROGRAM:
#include<stdio.h>
#include<unistd.h>
main()
{
execl("/bin/date","date",0);
}
SAMPLE OUTPUT:
[root@localhost C_Program]# cc execl.c
[root@localhost C_Program]# ./a.out
Tue May 2 18:01:37 IST 2006
114
![Page 115: Semester 2 Lab Manual 1](https://reader034.vdocuments.net/reader034/viewer/2022051013/547c0684b379593f2b8b4edb/html5/thumbnails/115.jpg)
[root@localhost C_Program]#
RESULT:
Thus the C Program for EXECL System Call in UNIX was executed and the output was verified.
115