Вы находитесь на странице: 1из 113

UNIX commands for 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 egins y invo!ing vi with or without a filename
$vi
$vi filename
vi functions in three modes namely"
#. Input mode$where any !ey depressed is entered as text
%. Command mode$where !eys are used as commands to act on text &initial mode'
(. ex mode$ex mode commands that can e entered in the last line to act on text
1. INPUT MOD
vi starts with command mode. )o insert text any of the following commands should e
used.
Commands !unction
".
No.
Command Description
# $vi filename a full-screen editor will appear
% i *nserts text to the left of the cursor.
( * *nserts text at the eginning of line.
+ a Appends text to right of cursor
, A Appends text at end of line
- o Opens line elow
. O Opens line aove
#. COMM$ND MOD
DIT COMM$ND"
Command Description
% /eplaces more than a single character. )he editor
displays %P&$C in the last line. )he existing text
is overwritten as they are typed.
s 0eletes a single character to the left and switches to
Input mode.
x 0eletes the character in the current cursor position
'text 1ocates the text in the file. *f not found2 the message
1
"3attern not found"appears. Use n to repeat the
forward search and N for ac!ward search.
U or u /everses the last change made to the uffer.
dd 4uts the entire line
d( 4uts the entire word
d$ 4uts a line from cursor position to the end of the line
d) 4uts from the cursor position to the start of the line
** 4opies &yan!s' the entire line
+( 4opies the entire word
p 3astes the text

N$,I-$TION COMM$ND"
Command Description
5oves ac! to eginning of a word
w 5oves forward to eginning of word
6 5oves to start of the line
7 5oves to end of the line
! Up one line
8 0own one line
h 1eft one character
l /ight one character
4trl9f Scrolls a page forward
4trl9 Scrolls a page ac!ward
l: )o move to the specific line
2
.. X MOD
)he essential save and exit commands form the features of ex mode. 3ress /
&colon' in command mode to switch to ex mode. )he / is displayed in the last line. )ype
the command and press Enter !ey to execute the same.
Command Description
w
Saves file2 displays the numer of lines ; characters and
returns to *nput mode. *f it is an unnamed file then vi puts a
message.
w file )he file is saved under the given name
L1,L2 w file
Used to write specific line numers to some file. )he .
&dot' represents current line2 ) for first line and $ could e
used to represent last line.
<
=uits vi session and returns to $ prompt. vi has a safety
mechanism that warns if changes made to file are not
saved.
<>
=uits vi session without saving any changes made since the
last save
"w< Save and exit
sh ?scape to shell
@sASstrARstrAg
)his is yet another powerful feature !nown as sustitution.
*t is similar to Bind and /eplace. 0 represents all lines2 1
ma!es it gloal. )o ma!e vi as! for confirmation efore
replacing use 1c instead of 1.
r file )o insert another file into the current file.
new file Splits screen into multiple windows and opens the file.
3
4
C P%O-%$MMIN- ON UNIX
5
6
Ex. No. 1.1 Date:
Nested !unctions
$IM/
)o write a 4 program to implement nested functions.
$&-O%IT2M/
#. Start the program.
%. /ead x2 y2 C.
(. 4all the function ratio & '.
+. Bunction ratio& '2 calls the function diff& '.
,. 0isplay the return value.
-. Stop the program.
P%O-%$M/
Dinclude Estdio.hF
DincludeEstdio.hF
main&'
G
int x2y2CH
float ratio&int2int2int'H
printf&"?nter x2y2C values......"'H
scanf&"@d@d@d"2;x2;y2;C'H
printf&"/atio is..@fIn"2ratio&x2y2C''H
J
float ratio&int x2int y2int C'
G
int diff&int2int' H
if&diff&y2C''
return&&float'xA&y-C''H
else
return&K'H
J
diff&int m2 int n'
G
if&m>Ln' return&#'H
else return&K'H
J
7
OUTPUT/
MstaffNlocalhost OP7cc nested.c
MstaffNlocalhost OP7 .Aa.out
?nter x2y2C values......+ - Q
/atio is..-%.KKKKKK
%"U&T/
)hus the 4 program to implement nested functions is written and executed.
"UPP&MNT$%+ P%O-%$M"/
#. Rrite a 4 program to find the sum and difference of two matrices using
functions.
8
%. Rrite a 4 program to find the product of two matrices using functions.
9
Ex. No. 1.2 Date:
%everse t3e "trin1 usin1 %ecursion
$IM/
)o write a 4 program to reverse the given string using recursion.
$&-O%IT2M/
#. Start the program.
%. /ead a string.
(. 4all the function reverse & '.
+. Bunction reverse &' calls itself recursively.
,. 0isplay the reversed string.
-. Stop the program.
P%O-%$M/
DincludeEstdio.hF
main&'
G
void reverse&'H
printf&"?nter 1ine of )ext...In"'H
reverse&'H
printf&"In"'H
J
void reverse&'
G char cH
if&&cLgetchar& ''>LSInS'
reverse&'H
putchar&c'H
J
OUTPUT/
MstaffNlocalhost OP7cc reverse.c
MstaffNlocalhost OP7 .Aa.out
?nter 1ine of )ext...
hai welcome
emoclew iah
%"U&T/
)hus the 4 program to inverse the given string is written and executed.
10
"UPP&MNT$%+ P%O-%$M"/
#. Rrite a 4 program to print the factorial of a given numer using recursion.
%. Rrite 4 program to generate following triangle.
#
# % (
# % ( + ,
# % ( + , - .
11
Ex. No. 1.3 Date:
To(ers of 2anoi
$IM/
)o write a 4 program to implement )owers of Tanoi prolem.
$&-O%IT2M/
#. Start the program.
%. /ead the numer of dis!s.
(. 4all the function honoi & '
+. Bunction honoi & ' calls itself recursively to display the result.
,. Stop the program.
P%O-%$M/
DincludeEstdio.hF
main&'
G
void honoi &int2 char2 char2 char'H
int nH
printf &"Tow many dis!..In"'H
scanf &"@d"2 ;n'H
honoi &n2 S1S2 S/S2 S4S'H
J
void honoi &int n2 char from2 char to2 char temp'
G
if&nFK'
G
honoi&n-#2from2temp2to'H
printf&"5ove dis! @d from @c to @cIn"2n2from2to'H
honoi&n-#2temp2to2from'H
J
J
OUTPUT/
MstaffNlocalhost OP7 cc honoi.c
MstaffNlocalhost OP7 .Aa.out
Tow many dis!..
(
5ove dis! # from 1 to /
12
5ove dis! % from 1 to 4
5ove dis! # from / to 4
5ove dis! ( from 1 to /
5ove dis! # from 4 to 1
5ove dis! % from 4 to /
5ove dis! # from 1 to /
%"U&T/
)hus the 4 program to implement )owers of Tanoi is written and executed.
"UPP&MNT$%+ P%O-%$M/
#. Rrite a 4 program to find the value of
n
4
r
using recursion.
13
Ex. No. 1.4 Date:
Menu Driven Ca4cu4ator usin1 !unctions
$IM/
)o write a 4 program to implement menu driven calculator using functions.
$&-O%IT2M/
#. Start the program.
%. 0isplay the options" &#.Add %.5ul (. 5od +.?xit'
(. 4hoose the option
+. /ead a and
,. *f option is Add2 add a and .
-. *f option is 5ul2 multiply a and .
.. *f option is 5od2 find remainder using a @ .
Q. *f option is ?xit
Stop the program
P%O-%$M/
DincludeEstdio.hF
DincludeEstdli.hF
int add&int a2 int 'H
int mul&int a2 int 'H
int mod&int a2 int 'H
main&'
G
int a2 2 chH
float rH
system&"clear"'H
while&#'
G
printf&"In5enu...In"'H
printf&"#-AdditionIn%-5ultiplicationIn(-5odulusIn+-?xitIn"'H
printf&"?nter Uour 4hoice...."'H
scanf&"@d"2;ch'H
printf&"In ?nter a and values..."'H
scanf&"@d@d"2;a2;'H
switch&ch'
G
14
case #"
rLadd&a2'H
printf&"In Sum of @d and @d is L@f"2a22r'H
rea!H
case %"
rLmul&a2'H
printf&"In 3roduct of @d and @d is L@f"2a22r'H
rea!H
case ("
rLmod&a2'H
printf&"In 5odulus of @d and @d is L@f"2a22r'H
rea!H
case +"
exit&K'H
J
J
J
int add&int a2 int '
G
int resH
resLa9H
return&res'H
J
int mul&int a2 int '
G
int resH
resLaVH
return&res'H
J
int mod&int a2 int '
G
int resH
resLa @H
return&res'H
J
15
OUTPUT/
MstaffNlocalhost OP7 cc menu.c
MstaffNlocalhost OP7 .Aa.out
5enu...
#-Addition
%-5ultiplication
(-5odulus
+-?xit
?nter Uour 4hoice....#
?nter a and values...( +
Sum of ( and + is L..KKKKKK
5enu...
#-Addition
%-5ultiplication
(-5odulus
+-?xit
?nter Uour 4hoice....%
?nter a and values...( .
3roduct of ( and . is L%#.KKKKKK
5enu...
#-Addition
%-5ultiplication
(-5odulus
+-?xit
?nter Uour 4hoice....(
?nter a and values...-- ,
5odulus of -- and , is L#.KKKKKK
5enu...
#-Addition
%-5ultiplication
(-5odulus
+-?xit
?nter Uour 4hoice....+
%"U&T/
)hus the 4 program to implement menu driven calculator using functions is
written and executed.
16
"UPP&MNT$%+ P%O-%$M/
#. ?xtend the aove program to perform sutraction and division.
17
Ex. No. 1.5 Date:
D*namic Memor* $44ocation
$IM/
)o write a 4 program to concatenate two strings using dynamic memory
allocation.
$&-O%IT2M/
#. Start the program.
%. *nitialiCe strings s#2s%2s( with malloc&' function.
(. /ead s#2s%.
+. 4opy s# to s(.
,. Append s% to s(.
-. Stop the program
P%O-%$M/
DincludeEstdio.hF
DincludeEmalloc.hF
Ddefine length (K
main&'
G
char Vs#2Vs%2Vs(2cH
int i2!H
s#L&char V'malloc&length VsiCeof &char''H
s%L&char V'malloc&length VsiCeof &char''H
s(L&char V'malloc&%Vlength VsiCeof &char''H
printf&"In ?nter the string#""'H
scanf&"@s"2s#'H
printf&"In ?nter the string%""'H
scanf&"@s"2s%'H
iLKH
while&&cLV&s#9i''>LSIKS'
G
s(MiPLcH
i99H
J
!LKH
while&&cLV&s%9!''>LSIKS'
18
G
s(Mi9!PLcH
!99H
J
s(Mi9!PLSIKSH
printf&"In concatenated string is @sIn"2s('H
J
OUTPUT/
MstaffNlocalhost OP7 cc strcat.c
MstaffNlocalhost OP7 .Aa.out
?nter the string#"hello
?nter the string%"welcome
concatenated string is hellowelcome
%"U&T/
)hus the 4 program to concatenate two strings using dynamic memory allocation
is written and executed.
"UPP&MNT$%+ P%O-%$M"/
#. Rrite a 4 program for string copy using dynamic memory allocation.
19
%. Rrite a 4 program for string compare using dynamic memory allocation.
20
Ex. No. 1.6 Date:
"ortin1 Names usin1 functions and Pointers
$IM/
)o write a 4 program for sorting names using functions and pointers.
$&-O%IT2M/
#. Start the program.
%. ?nter the value of n.
(. Allocate memory for each name using calloc & ' and read n names.
+. Sort the names using sort & ' function.
,. 0isplay the sorted list of names.
-. Stop the program
P%O-%$M/
DincludeEstdio.hF
DincludeEstdli.hF
DincludeEstring.hF
void sort&char VVname2int n'H
main&'
G
char V namesM%KPH
char tempM%KPH
int n2iH
printf&"In TOR 5AWU ?W)/*?S In"'H
scanf&"@d"2;n'H
printf&"In ?W)?/ )T? @d WA5?S OW? BU OW? In"2n'H
for&iLKHiEnHi99'
G
namesMiPL&char V' calloc&%K2siCeof&char''H
scanf&"@s"2namesMiP'H
J
sort&names2n'H
printf&"In Wames in Sorted form In"'H
for&iLKH iEnHi99'
G
printf&"@s"2namesMiP'H
printf&"In"'H
21
J
J
void sort& char VVname2int n'
G
int i282!H
char VtempH
tempL&char V' calloc&%K2siCeof&char''H
for&iLKHiELn-%Hi99'
for&8Li9#H8ELn-#H899'
if&strcmp&nameMiP2nameM8P'F K'
G
strcpy&temp2nameMiP'H
strcpy&nameMiP2nameM8P'H
strcpy&nameM8P2temp'H
J
J
OUTPUT/
MstaffNlocalhost OP7 cc sort.c
MstaffNlocalhost OP7 .Aa.out
TOR 5AWU ?W)/*?S
-
?W)?/ )T? - WA5?S OW? BU OW?
sri
vidya
arun
ala
neha
uma
Wames in Sorted form
arun
ala
neha
sri
uma
vidya
22
%"U&T/
)hus the 4 program to sort names using functions and pointers is written and
executed.
"UPP&MNT$%+ P%O-%$M/
#. Rrite a 4 program for sorting student record ased on student name.
& 0eclare array of structures with only two memers" name2 mar!. '
23
Ex. No. 1. Date:
Countin1 of C3aracters, 5ords and &ines in a !i4e
$IM/
)o write a 4 program to count the numer of characters2 words and lines in a file.
$&-O%IT2M/
#. Start the program.
%. 0eclare the file pointer ptr.
(. Open the file in read mode.
+. /ead the contents of file character y character.
,. 4hec! the character type and increment the counters accordingly.
-. Stop the program.
P%O-%$M/
DincludeEstdio.hF
main&'
G
int aLK2wLK2dLK2lLK2tLKH
char chH
B*1? VptrH
ptrLfopen&"inf.c"2"r"'H
while&&chLgetc&ptr'' >L ?OB'
G
if&isalpha&ch'' a99H
if&isdigit&ch'' d99H
if&chLLS S 66 chLLSItS' w99H
if&chLLSInS'
G
l99H w99H
J
J
printf&"Wo. of characters are L @dIn"2a'H
printf&"Wo. of words are L @dIn"2w'H
printf&"Wo. of digits are L @dIn"2d'H
printf&"Wo. of lines are L @dIn"2l'H
fclose&ptr'H
J
24
OUTPUT/
MstaffNlocalhost OP7 cc count.c
MstaffNlocalhost OP7 .Aa.out
Wo. of characters are L +Q
Wo. of words are L Q
Wo. of digits are L K
Wo. of lines are L +
%"U&T/
)hus the 4 program for counting of characters2 words and lines in the file is
written and executed.
"UPP&MNT$%+ P%O-%$M"/
#. Rrite a 4 program to replace all the lan! spaces in a file with X7Y symol.
25
Ex. No. 1.! Date:
!i4e Cop* usin1 Command &ine $r1uments
$IM/
)o write a 4 program to copy the contents of one file to another file using
command line arguments.
$&-O%IT2M/
#. Start the program.
%. ?xecute the program with source file name and target file name as command
line arguments.
(. 0eclare the file pointers ptr#2 ptr%.
+. Open the input file in read mode and output file in write mode.
,. /ead the contents of input file and write into output file.
-. Stop the program.
P%O-%$M/
Dinclude Estdio.hF
Dinclude Estdli.hF
int main&int argc2 char VargvMP'
G
long int xH
int iLKH
char c2strM%,PH
B*1? Vptr#2 Vptr%H
ptr#Lfopen&argvM%P2"r"'H
ptr%Lfopen&argvM(P2"w"'H
printf&"In4ommand line arguments" @sIt@sIn"2argvM%P2argvM(P'H
printf&"In4ontents in infile" In"'H
while &&cLgetc&ptr#'' >L ?OB'
putchar&c'H
rewind&ptr#'H
while &&cLgetc&ptr#'' >L?OB'
putc&c2ptr%'H
fclose&ptr#'H
fclose&ptr%'H
printf&"InIn4ontents in outfile" In"'H
26
ptr%Lfopen&argvM(P2"r"'H
while &&cLgetc&ptr%'' >L ?OB'
putchar&c'H
fclose&ptr%'H
return #H
J
OUTPUT/
MstaffNlocalhost OP7 cc filecopy.c
MstaffNlocalhost OP7 .Aa.out filecopy infile.txt outfile.txt
4ommand line arguments" infile.txt outfile.txt
4ontents in infile"
Tello> Relcome to ZS/*?)
4ontents in outfile"
Tello> Relcome to ZS/*?)
%"U&T/
)hus the 4 program for copying the contents of one file to another is written and
executed.
"UPP&MNT$%+ P%O-%$M"/
#. Rrite a 4 program to concatenate two files.
27
%. Rrite a 4 program to convert the contents of a file into uppercase.
28
Ex. No. 1." Date:
"e6uentia4 $ccess !i4e
$IM/
)o write a 4 program to process se<uential access file.
$&-O%IT2M/
#. Start the program.
%. 0eclare a structure called student.
(. 0eclare the file pointer ptr.
+. 0isplay the options" &#.Add %.0elete (. 0isplay +.?xit'
,. 4hoose the option
-. *f option is Add
open the file in append mode
read one student details and append at the end of the file
.. *f option is 0elete
/ead roll numer to delete2 search and find the record
0elete the record and move the remaining records accordingly
Q. *f option is 0isplay
0isplay the details
[. *f option is ?xit
Stop the program
P%O-%$M/
Dinclude Estdio.hF
Dinclude Estdli.hF
void add&'H
void display&'H
void dele&'H
void modify&'H
void search&'H
struct student
G
int rnoH
char nameM%KPH
int ageH
int mar!sH
JsH
29
B*1? VptrH
void main&'
G
int chH
ptrLfopen&"stud.dat"2"w"'H
while&#'
G
printf&"In#. Add"'H
printf&"In%. 0elete"'H
printf&"In(. 0isplay"'H
printf&"In+. ?xit"'H
printf&"In?nter your option &#-+'" "'H
scanf&"@d"2 ;ch'H
switch&ch'
G
case #" add&'H
rea!H
case %" dele&'H
rea!H
case (" display&'H
rea!H
case +" exit&K'H
default"printf&"InIn*nvalid option......In"'H
rea!H
J
J
J
void add&'
G
struct student s#H
ptrLfopen&"stud.dat"2"a9"'H
while&fread&;s#2siCeof&s#'2#2ptr''H
printf&"/oll Wo. ""'H
scanf&"@d"2;s#.rno'H
printf&"Wame ""'H
scanf&"@s"2s#.name'H
30
printf&"Age ""'H
scanf&"@d"2;s#.age'H
printf&"5ar!s ""'H
scanf&"@d"2;s#.mar!s'H
fwrite&;s#2siCeof&s#'2#2ptr'H
fclose&ptr'H
J
void dele&'
G
struct student s#H
int foundLK2rollH
long offsetH
ptrLfopen&"stud.dat"2"r9t"'H
printf&"?nter the /oll no. to delete ""'H
scanf&"@d"2;roll'H
offsetLftell&ptr'H
while &fread&;s#2siCeof&s#'2#2ptr''
G
if &s#.rnoLLroll'
G if &&fread&;s#2siCeof&s#'2#2ptr'' >LWU11'
G
do G
fsee!&ptr2offset2S??Z\S?)'H
fwrite&;s#2siCeof&s#'2#2ptr'H
offsetLftell&ptr'H
fsee!&ptr2siCeof&s#'2S??Z\4U/'H
J while&fread&;s#2siCeof&s#'2#2ptr''H
J
fsee!&ptr2offset2S??Z\S?)'H
putc&WU112ptr'H
foundL#H
rea!H
J
offsetLftell&ptr'H
J
if &foundLLK'
31
printf&"InIn............./ecord Wot Bound..........In"'H
fclose&ptr'H
J
void display&'
G
struct student s#H
long offsetH
ptrLfopen&"stud.dat"2"r"'H
offsetLftell&ptr'H
while&fread&;s#2siCeof&s#'2#2ptr''
G if &s#.rno>L K'
G
printf&"InIn/oll Wo." @d"2s#.rno'H
printf&"InWame. " @s"2s#.name'H
printf&"InAge " @d"2s#.age'H
printf&"In5ar!s " @d"2s#.mar!s'H
printf&"In----------------------------------In"'H
J
J
fclose&ptr'H
J
OUTPUT/
MstaffNlocalhost OP7 cc student.c
MstaffNlocalhost OP7 .Aa.out
#. Add
%. 0elete
(. 0isplay
+. ?xit
?nter your option &#--'" #
/oll Wo. "#
Wame "Arun
Age "%K
5ar!s "[K
#. Add
%. 0elete
(. 0isplay
+. ?xit
?nter your option &#--'" #
/oll Wo. "%
32
Wame "Amutha
Age "%,
5ar!s "QK
#. Add
%. 0elete
(. 0isplay
+. ?xit
?nter your option &#--'" (
/oll Wo." #
Wame. " Arun
Age " %K
5ar!s " [K
----------------------------------
/oll Wo." %
Wame. " Amutha
Age " %,
5ar!s " QK
----------------------------------
#. Add
%. 0elete
(. 0isplay
+. ?xit
?nter your option &#--'" %
?nter the /oll no. to delete "#
#. Add
%. 0elete
(. 0isplay
+. ?xit
?nter your option &#--'" (
/oll Wo." %
Wame. " Anitha
Age " %(
5ar!s " Q,
----------------------------------
#. Add
%. 0elete
(. 0isplay
+. ?xit
?nter your option &#--'"+
%"U&T/
)hus the 4 program for se<uential access file processing is written and executed.
33
"UPP&MNT$%+ P%O-%$M/
#. ?xtend the aove program to modify and search a particular record in a file.
&Tint" *f option is 5odify - #. /ead roll numer to modify2 search and find the record
%. /ead the new details and modify the record with new details
*f option is Search ] #. /ead roll numer to search2 find and display the record'
34
Ex. No. 1.1# Date:
%$NDOM $CC"" !I&
$IM/
)o write a 4 program to process random access file.
$&-O%IT2M/
#. Start the program.
%. 0eclare the file pointer ptr.
(. Open a file in write mode.
+. /ead a string and write in the file.
,. 4lose the file and open it in read mode.
-. /ead and display the contents of the file
.. Use fsee! & '2 rewind & ' and ftell & ' functions to move the file pointer to the
re<uired location.
Q. Stop the program.
P%O-%$M/
Dinclude Estdio.hF
Dinclude Estdli.hF
void main&'
G
long int xH
int iLKH
char c2 strM%,PH
B*1? VptrH
system&"clear"'H
ptrLfopen&"st.txt"2"w"'H
printf&"?nter a string " In"'H
while &&cLgetchar&'' >L SInS'
putc&c2ptr'H
fclose&ptr'H
ptrLfopen&"st.txt"2"r"'H
if &ptr >L WU11'
G
printf&"InIn)he String stored in the file is " " 'H
while &&cLgetc&ptr'' >L ?OB'
putchar&c'H
35
AAfsee!&' is used to move the file pointer to a desired location
fsee!&ptr2K12%'H
AAftell&' returns the current position of the file pointer
xLftell&ptr'H
printf&"In3tr is at the end of file LF )he siCe of Bile is " @ldIn"2x'H
AArewind&' is used to move file pointer to the eginning of the file
rewind&ptr'H
xLftell&ptr'H
printf&"In 3tr is at the egining of the file LF )he siCe of Bile is " @ldIn"2x'H
J
else
printf&"In)he file does not exists"'H
fclose&ptr'H
J
OUTPUT/
MstaffNlocalhost OP7 cc random.c
MstaffNlocalhost OP7 .Aa.out
?nter a string "
welcome
)he String stored in the file is " welcome
3tr is at the end of file LF )he siCe of Bile is " .
3tr is at the egining of the file LF )he siCe of Bile is " K
%"U&T/
)hus the 4 program for random access file processing is written and executed.
36
"UPP&MNT$%+ P%O-%$M/
#. ?xtend the aove program to move the file pointer to the eginning of the file
using fsee!&'.
37
Ex. No. 1.11 Date:
Macro "u7stitution
$IM/
)o write a 4 program to perform macro sustitution.
$&-O%IT2M/
#. Start the program.
%. 0efine the macros using Ddefine statement
(. /ead a numer.
+. 4all the macros y passing the parameter
,. Stop the program.
P%O-%$M/
DincludeEstdio.hF
Ddefine s<&n' &nVn'
Ddefine cue&n' &nVnVn'
Dinclude Estdio.hF
main&'
G
int n2s2cH
printf&"In?nter a numer " "'H
scanf&"@d"2;n'H
sLs<&n'H
cLcue&n'H
printf&"InS<uare value of @d is @dIn"2n2s'H
printf&"In4ue value of @d is @dIn"2n2c'H
J
OUTPUT/
MstaffNlocalhost OP7 .Aa.out
?nter a numer " +
S<uare value of + is #-
4ue value of + is -+
%"U&T/
)hus the 4 program for performing macro sustitution is written and executed.
38
"UPP&MNT$%+ P%O-%$M"/
#. Rrite a 4 program to define a macro for finding the largest of three numers.
%. Rrite a 4 program to define a macro for displaying the given string.
39
40
UNIX COMM$ND"
41
#.1 UNIX Commands
".
No.
Command "*ntax xamp4e Description
# m!dir m!dir dirname M8aganNven!at OP7 m!dir cse
M8aganNven!at OP7 ls
cse
*t creates a new directory
% cd cd dirname M8aganNven!at OP7 cd cse
M8aganNven!at cseP7
*t is used to change the current
directory
( cdI cdI M8aganNven!at studentP7 cdI
M8aganNven!at OP7
*t is used to exit the current
directory.
+. rmdir rmdir dirname M8aganNven!at cseP7 dir
student
M8aganNven!at cseP7 rmdir
student
M8aganNven!at cseP7 dir
*t is used to remove an empty
directory.
, pwd pwd M8aganNven!at cseP7 pwd
AhomeA8aganAcse
*t is used to display the current
wor!ing directory
- rm rm filename M8aganNven!at cseP7 ls
f# f%
M8aganNven!at cseP7 rm f#
M8aganNven!at cseP7 ls
f%
/emove the particular file in
current directory
. cat catFfilename
^
_ctrl90`
M8aganNven!at cseP7 catFf#
M8aganNven!at cseP7 ls
f# f%
*t is used to create a new file
Q cp cp source
destination
M8aganNven!at cseP7 cp f# f%
M8aganNven!at cseP7 cat f%
hello this sample file
copy the source file to the
destination file
[ cat cat filename M8aganNven!at cseP7 cat f%
!sriet at tiruchengode
*t displays the content of the file
#K mv mv source
destination
M8aganNven!at cseP7 mv f#
new#
M8aganNven!at cseP7 ls
f% new# se
5ove source file to the
destination files.
## cmp cmp fn#2fn% M8aganNven!at cseP7 cmp se
seen
se seen differ" yte #2 line #
*t is used to compare two files
#% diff diff fn#2fn% MsenthilNprahu OP7 diff sen# sen%
#c#
*t is used to print the all the
difference etween the two files.
#( tail tail filename MsenthilNprahu cseP7 tail a%
hello
3rint last #K line of the file
#+ wc wc filename M8aganNven!at cseP7 wc se
% , %- se
*t is used count lines2words and
characters of a file
#, wc &i' wc -l filename
&ii' wc -c filename
MsenthilNprahu cseP7 wc -l a%
# a%
MsenthilNprahu cseP7 wc -c a%
- a%
4ount numer of lines of the file.
4ount numer of characters in a
file
42
&iii'wc -w filename
MsenthilNprahu cseP7 wc -w a%
# a%

4ount numer of words in a file.
#- uname uname ]a M8aganNven!at cseP7 uname -a
1inux ven!at (.#.K-..fc#-.i-Q-
D# S53 )ue Wov # %#"KK"#-
U)4 %K## i-Q- i-Q- i(Q-
:WUA1inux
*t is used to !now aout the
operating system details
#. uname uname ]s M8aganNven!at cseP7 uname -s
1inux
*t is used to !now the name of the
operating system.
#Q uname uname ]n Mstaff%Nlocalhost OP7 uname -n
localhost.localdomain
*t is used to !now the name of the
networ! node &host name'.
#[ uname uname ]r Mstaff%Nlocalhost OP7 uname -r
%.-.#Q-%(Q.el,
*t is used to !now the release
numer of the operating system.
%K uname uname ]v Mstaff%Nlocalhost OP7 uname -v
D# S53 Sun 0ec #[ #+"%+"+.
?S) %K#K
*t is used to !now the version of
the operating system.
%# uname uname ]m Mstaff%Nlocalhost OP7 uname -m
i-Q-
*t is used to !now the machine id
%% who who M8aganNven!at cseP7 who
ven!at tty# %K#%-K#-%(
K["%[
8agan ptsAK %K#%-K#-%(
K["(K &#[%.#-Q.,-.#'
*t is used to show who is logged
onto system
%( whoami whoami M8aganNven!at cseP7 whoami
8agan ptsAK %K#%-K#-%(
K["(K &#[%.#-Q.,-.#'
*t is used to !now who owns the
shell
%+ whois whois Mstaff%Nlocalhost OP7 whois
8whois version (.%.(2
4opyright &4' #[[[-%KK, Bree
Software Boundation2 *nc.
)his program is free software
with ABSO1U)?1U WO
RA//AW)U
*t is used to !now *nternet user
name directory service.
%, passwd passwd M8aganNven!at cseP7 passwd
4hanging password for user
8agan.
4hanging password for 8agan.
&current' UW*a password"
Wew password"
/etype new password"
passwd" all authentication to!ens
updated successfully.
*t is used to change the current
password.
%- mail mail user name Mstaff%Nlocalhost OP7 mail staff#
Su8ect" hello
4c" sample
*t is used to write the message to
another user
%. write write username MrootNven!at OPD write 8agan *t is used to write the message to
43
hello another user
%Q cal cal monthyear MrootNven!at OPD cal # %K#%
January %K#%
Su 5o )u Re )h Br Sa
# % ( + , - .
Q [ #K ## #% #( #+
#, #- #. #Q #[ %K %#
%% %( %+ %, %- %. %Q
%[ (K (#
*t is used to print the calendar of
the current year or ?ntered year
%[ echo echo text MrootNven!at OPD echo !sriet
Zsriet
*t is used to display the printed
text document
(K env env MrootNven!at OPD env
3R0LAroot
JAbA\TO5?LAusrA8avaA8d!#...
K\K%AinA8ava
1AW:Len\US.U)B-Q
S?1*WUa\1?b?1\/?=U
?S)?0L
T*S)4OW)/O1Lignoredup
s
ST1b1L#
TO5?LAroot
1O:WA5?Lroot
SST\4OWW?4)*OWL#[%.
#-Q.,-.# +[Q,.
#[%.#-Q.,-.#K# %%
1?SSO3?WL66AusrAinAlesspi
pe.sh @s
*t is used to display environment
(# time time MrootNven!at OPD time
real KmK.KKKs
user KmK.KKKs
sys KmK.KKKs
*t is used to display the timing
programs.
(% df df MrootNven!at OPD df
Bilesystem #Z-loc!s Used
Availale Use@ 5ounted on
rootfs -%%(+#- %Q(%Q+Q
((%.(+Q +-@ A devtmpfs
,((#++ K ,((#++ K@ Adev
tmpfs ,+#(.% K ,+#(.%
K@ AdevAshm tmpfs
,+#(.% +K#,- ,K#%#- Q@
*t summariCes free dis! space
(( man man command
name
MrootNven!at OPD man file
N$M
file c determine file type
"+NOP"I"
file M-chi!l1WnprsvCKP M--appleP
*t is used to display the given
commandYs manual information.
44
M--mime-encodingP M--mime-
typePM-e testnameP M-B separatorP
M-f namefileP M-m magicfilesP
file ...
file -4 M-m magicfilesP
file M--helpP
(+ last last MrootNven!at OPD last
8agan ptsA# #[%.#-Q.,-.#
5on Jan %( #K"K( still logged in
root ptsAK #[%.#-Q.,-.#
5on Jan %( #K"K( still logged in
reoot system oot (.#.K-
..fc#-.i-Q 5on Jan %( #K"K% -
#K"#[ &KK"#.'
*t indicates last login of users.
(, ps ps MrootNven!at OPD ps
3*0 ))U )*5? 450
Q.. ptsAK KK"KK"KK ash
##%% ptsAK KK"KK"KK ps
*t shows the process status.
(- find find file name or
path name
M8aganNven!at cseP7 find f%
f%
*t is used to find whether the
particular file name or path name
is availale or not.
(. w w MrootNven!at OPD w
#K"%+"%Q up %# min2 % users2
load average" K.KK2 K.K%2 K.K,
US?/ ))U B/O5
1O:*WN *01? J43U
343U RTA)
root ptsAK #[%.#-Q.,-.#
#K"K( K.KKs K.%+s K.K#s w
8agan ptsA# #[%.#-Q.,-.#
#K"K( %%.KKs K.##s K.##s ]
ash
*t shows who is on system2 Rhat
command each 8o is executing.
(Q date date MrootNven!at OPD date
5on Jan %( #K"%-"%( *S) %K#%
*t shows date and time
([ id id M8aganNven!at cseP7 id
uidL#KK#&8agan'
gidL#KK%&8agan'
groupsL#KK%&8agan'
contextLunconfined\u"unconfed
\r"unconfined\t"sK-sK"cK.c#K%(
*t is used to show login name2 a
user ]id and group -id
+K du du Mstaff%Nlocalhost OP7 du
Q .A.moCillaAplugins
Q .A.moCillaAextensions
%+ .A.moCilla
Q .Acse
#K+ .
*t is used to summariCe the dis!
space
45
#.# !i4e Mana1ement Commands
# cat F
filename
catFf# M8aganNven!at cseP7 catFf#
M8aganNven!at cseP7 ls
f# f%
)o create a file with some
contents. )o end typing press
Ctr48d. )he 9 symol means
redirecting output to a file. &: for
input'
% cat
filename
cat f% M8aganNven!at cseP7 cat f%!sriet
at tiruchengode
0isplays the file contents.
( cat FF
filename
catFFf% M8aganNven!at cseP7 catFFf%
welcomes u all
M8aganNven!at cseP7 cat f%
!sriet at tiruchengode
welcomes u all
Used to append contents to a file
+ cp ]i sr$
des
cp -i f% f# M8aganNven!at cseP7 cp -i f% f#
cp" overwrite df#Se y
M8aganNven!at cseP7 cat f#
ls
hello this sample file
Rarns the user prior to
overwriting the destination file
, mv old ne% mv f# new# M8aganNven!at cseP7 mv f#
new#
M8aganNven!at cseP7 ls
f% new# se
rename an existing file or
directory. ]i option can also e
used
- mv f1 f2 f3
dir
mv f% new# copied M8aganNven!at cseP7 ls
copied f% new# se
M8aganNven!at cseP7 mv f%
new# copied
M8aganNven!at copiedP7 ls
f% new#
)o move a group of files to a
directory.
. rm file rm new# M8aganNven!at copiedP7 ls
f% new#
M8aganNven!at copiedP7 rm
new#
M8aganNven!at copiedP7 ls
f%
Used to delete a file or group of
files. ]i option can also e used
Q rm V rm V M8aganNven!at copiedP7 rm V
M8aganNven!at copiedP7 ls
)o delete all the files in the
directory.
[ rm ]r V rm ]r V M8aganNven!at cseP7 ls
copied se
M8aganNven!at cseP7 rm -i V
rm" cannot remove dcopiedS" *s a
directory
rm" remove regular file dseSe n
0eletes all files and su-
directories
#K ls ls M8aganNven!at cseP7 ls
copied se
1ists all files and sudirectories
&lue colored' in sorted manner.
## ls name ls se M8aganNven!at cseP7 ls se
se
)o chec! whether a file or
directory exists.
#% ls name; ls seV M8aganNven!at cseP7 ls seV
se seen
Short-hand notation to list out
filenames of a specific pattern.
#( ls ]a ls ]a M8aganNven!at cseP7 ls -a
. .. copied se seen
1ists all files including hidden
files &files eginning with .'
#+ ls ]x
dirname
ls -x M8aganNven!at cseP7 ls -x
copied se seen
)o have specific listing of a
directory.
46
#, ls ]/ Mstaff%Nlocalhost OP7 ls -/
."
#.c cse f#
.Acse"
f# f%
/ecursive listing of all files in the
sudirectories
#- ls ]l ls ]l M8aganNven!at cseP7 ls -l
total #%
drwxrwxr-x. % 8agan 8agan +K[-
Jan %( ##"(K copied
-rw-rw-r--. # 8agan 8agan %-
Jan %( ##"#K se
-rw-rw-r--. # 8agan 8agan (
Jan %( ##"(- seen
1ong listing showing file access
rights &readAwriteAexecute-r(x
for
userAgroupAothers<u1o'.
#. cmp file1
file2
cmp se seen M8aganNven!at cseP7 cmp se
seen
se seen differ" yte #2 line #
Used to compare two files.
0isplays nothing if files are
identical.
#Q chmod
&erm file
chmod uo9x se M8aganNven!at cseP7 chmod
uo9x se
M8aganNven!at cseP7 ls -l
total #%
drwxrwxr-x. % 8agan 8agan +K[-
Jan %( ##"(K copied
-rwxrw-r-x. # 8agan 8agan %-
Jan %( ##"#K se
-rw-rw-r--. # 8agan 8agan ( Jan
%( ##"(- seen
4hanges permission for the
specified file. &rL+2 wL%2 xL#'
chmod .+K file sets all rights for
user2 read only for groups and no
rights for others.
#[ chmod
&erm file
chmod .+K seen M8aganNven!at cseP7 chmod .+K
seen
M8aganNven!at cseP7 ls -l
total #%
drwxrwxr-x. % 8agan 8agan +K[-
Jan %( ##"(K copied
-rwxrw-r-x. # 8agan 8agan %-
Jan %( ##"#K se
-rwxr-----. # 8agan 8agan ( Jan
%( ##"(- seen
4hanges permission for the
specified file. &rL+2 wL%2 xL#'
chmod .+K file sets all rights for
user2 read only for groups and no
rights for others.
%K c c 4opyright #[[#-#[[+2 #[[.2
#[[Q2 %KKK2 %KK+2 %KK- Bree
Software Boundation2 *nc.
)his is free software with
ABSO1U)?1U WO
RA//AW)U.
Bor details type dwarrantyS.
+,9%#
--
Basic calculator. 3ress Ctr48d to
<uit
#.. 1rep command
A fre<uent re<uirement is to loo! for a pattern or expression in a file. UW*a
handles this feature through 1rep and e1rep. 1rep uses an regular expression to display
lines that match and e1rep enales searching for multiple patterns.
47

Command Description
grep Send ofS demo =uotes mandatory for text containing space
grep this demoV Search t'is in multiple files
grep ]c to demo Wumer of occurrence of the word to in the file
grep ]n se<uence demo 0isplay line numers along with matching lines
grep ]v word demo 0isplays lines that does not contain the text %ord
grep ]l vim V 0isplays files containing text vim
grep ]i RO/0 demo Search for text ignoring case differences
grep SfMK-[PS demo 1ines that start with a numer
grep SMK-[P7S demo 1ines that end with a numer
ls -l 6 grep "fd" 0isplay the sudirectory names
grep ]c "f7" demo 0isplay count of lan! lines in the file.
grep "%....7" stud 0isplay lines that ends in the range %KKKK]%[[[[
egrep "lower6U33?/" demo 0isplay lines that match either lo%er or u&&er
egrep "&previous6current' 0isplay lines that match either &revious %ord or
word" demo $urrent %ord
xamp4es/

Mvi8aiNlocalhost regexprP7 1rep t3is demo
t3is line is the #st lower case line in t3is file.
)wo lines aove t3is line is empty.
Mvi8aiNlocalhost regexprP7 1rep =end of= demo
#. e - go to the end of the current word.
%. ? - go to the end of the current RO/0.
Mvi8aiNlocalhost regexprP7 1rep <c to demo
,
Mvi8aiNlocalhost regexprP7 1rep <n se6uence demo
#,"RO/0 - RO/0 consists of a se<uence of non-lan! characters
#-"Rord - word consists of a se<uence of letters2 digits and underscores.
Mvi8aiNlocalhost regexprP7 1rep <v (ord demo
)T*S 1*W? *S )T? #S) U33?/ 4AS? 1*W? *W )T*S B*1?.
this line is the #st lower case line in this file.
)his 1ine Tas All *ts Birst 4haracter Of )he Rord Rith Upper 4ase.
)wo lines aove this line is empty.
vim Rord Wavigation
48
%. ? - go to the end of the current RO/0.
+. B - go to the previous RO/0.
RO/0 - RO/0 consists of a se<uence of non-lan! characters
telnet #.%.#-.+.%,-
Mvi8aiNlocalhost regexprP7 1rep <4 vim ;
demo readme
Mvi8aiNlocalhost regexprP7 1rep <i 5O%D demo
)his 1ine Tas All *ts Birst 4haracter Of )he Rord Rith Upper 4ase.
vim Rord Wavigation
Uou may want to do several navigation in relation to words2 such as"
#. e - go to the end of the current word.
%. ? - go to the end of the current RO/0.
(. - go to the previous word.
+. B - go to the previous RO/0.
RO/0 - RO/0 consists of a se<uence of non-lan! characters
Rord - word consists of a se<uence of letters2 digits and underscores.
Mvi8aiNlocalhost regexprP7 1rep =>?)<@A= demo
#. e - go to the end of the current word.
%. ? - go to the end of the current RO/0.
(. - go to the previous word.
+. B - go to the previous RO/0.
Mvi8aiNlocalhost regexprP7 1rep =?)<@A$= demo
telnet #.%.#-.+.%,-
Mvi8aiNlocalhost vi8aiP7 4s <4 B 1rep C>dC
drwxrwxr-x % vi8ai vi8ai +K[- Apr [ #+"(K regexpr
drwxrwxr-x . vi8ai vi8ai +K[- Apr + #+",. shellscripts
Mvi8aiNlocalhost regexprP7 1rep <c C>$C demo
,
Mvi8aiNlocalhost regexprP7 e1rep C4o(erBUPP%C demo
)T*S 1*W? *S )T? #S) U33?/ 4AS? 1*W? *W )T*S B*1?.
this line is the #st lower case line in this file.
Mvi8aiNlocalhost regexprP7 e1rep CDpreviousBcurrentE (ordC demo
#. e - go to the end of the current word.
(. - go to the previous word.
#.F fi4ter command
Bilters are the central commands of the UW*a tool !it. *t acts on data file where
lines are re$ords2 fields delimited y a character not used y the data &mostly 62 default is
white space'. )he output is a set of records and the input file is unaltered y these
commands.
".No. Command Description
49
1. 3ead
head stud
head -, stud
head -# stud 6 wc ]c
0isplays first #K records y default
0isplays first #K records y default
0isplays first , records
length of first record
#. Tai4
tail stud
tail -, stud 6 tee last,
used to display the last few records &#K records y default'
0isplays last #K records y default
1ast , records listed ; stored in file last5 using tee
.. cut
cut ]d I6 -f #2(2+ stud
cut ]d I6 -f %-+ stud
paste ]d I6 list# list%
used to extract specific fields. )he d option specifies the
delimiter and f for specifying the field list. )he c option may
e used if extraction is done character wise
Bields #2(2+ listed
Bields %2(2+ listed
merges two cut files list1 and list2
F. sort
sort stud
sort ]t I6 9% stud
sort ]c stud
sort ]t I6 9( -+ 9+
stud
sort -t I6 -nr 9+ stud
uni< stud
reorders the file as per AS4** se<uence. )he t option is used
to specify delimiter
Sorted on #st column y default
Sort as per (rd column
4hec! if file is sorted using c option
Sorting on secondary !eys
Sort on numeric field using n option2 r for reverse
0isplay uni<ue entries in a sorted file
G. n4
nl ]s "6" stud
display file content with lines numered. )he s option is used
to specify separator
0isplays entries numered with separator 6
H. tr
tr SMa-CPS SMA-gPS E
stud
translates characters. 4an e used to change text case. *t
wor!s with standard input E
4hanges text to upper case
50
"2&& P%O-%$MMIN-
51
52
Ex. No. 3.1 Date:
Maximum of T3ree Num7ers
$IM/
)o find the maximum among three numers using shell programming.
$&-O%IT2M/
#. Start the program.
%. /ead the three values as a22c.
(.# 4hec! if a is greater than and cH if yes print a as maximumH
(.% else chec! if is greater than c H if yes print as maximumH
otherwise print c as maximum
+. Stop the program
P%O-%$M"
D5aximum of three numers
echo "?nter value for A2 B and 4"
read a c
if test 7a -gt 7 -a 7a -gt 7c
then
echo "7a is maximum numer"
elif test 7 -gt 7c
then
echo "7 is maximum numer"
else
echo "7c is maximum numer"
fi
OUTPUT/
MstaffNlocalhost OP7 sh large
?nter value for A2 B and 4
#% +, %(
+, is maximum numer
%"U&T"
)hus the Shell program for finding the maximum among three numers has een
executed successfully.
53
"UPP&MNT$%+ P%O-%$M""
#. Rrite a shell script to find the minimum of three numers.
%. Rrite a shell script to read a numer and find its s<uare and cue.
54
Ex. No. 3.2 Date:
Temperature Conversion
$IM/
)o convert the given Bahrenheit temperature to 4elsius value using shell
programming.
$&-O%IT2M/
#. Start the program.
%. /ead the Bahrenheit temperature value B.
(. 4onvert to 4elsius value using the formula given elow"
4L &B-(%' V,A[
+. 3rint the 4elsius temperature value.
,. Stop the program
P%O-%$M"
D Bahrenheit to 4elsius 4onversion
echo "?nter B balue"
read f
echo 7f
D real arithmetic is done using c ] asic calculator along with the scale function in echo
command
cLdexpr "scaleL%H & 7f - (% ' V , A [" 6 c d
DcLdexpr 7c IV , A [d
echo "4elsius balue L 7c"
OUTPUT/
MstaffNlocalhost OP7 sh p%%
?nter B balue
.K
.K
4elsius balue L %#.##
%"U&T"
)hus the shell program for converting the given Bahrenheit temperature to
4elsius has een executed successfully.
55
"UPP&MNT$%+ P%O-%$M""
#. Rrite a shell script to find area of a circle.
%. Rrite a shell script to find the simple interest.
56
Ex. No. 3.3 Date:
!indin1 Positive , Ne1ative or Iero
$IM/
)o chec! whether the given numer is positive2 negative or Cero using shell
programming.
$&-O%IT2M/
#. Start the program.
%. /ead a numer n.
(. 4hec! whether n is greater than CeroH if yes print n is positiveH otherwise chec!
n is less than CeroH if yes print n is negative H otherwise print n is Cero
+. Stop the program
P%O-%$M"
D 4hec!ing whether a given numer is positive 2 negative or Cero
echo "?nter a Wumer"
read n
if M 7n -gt K P
then
echo "7n is positive"
elif M 7n -lt K P
then
echo "7n is negative"
else
echo "7n is gero"
fi
OUTPUT/
MstaffNlocalhost OP7 sh p%+
?nter a Wumer
-#%
-#% is negative
%"U&T"
)hus the Shell program for chec!ing whether the given numer is positive2
negative or Cero has een executed successfully.
57
"UPP&MNT$%+ P%O-%$M""
#. Rrite a shell script to chec! whether the given numer is divisile y + and ..
%. Rrite a shell script to chec! whether the given year is leap year or not.
58
Ex. No. 3.4 Date:
Disp4a* t3e O(ner of t3e !i4e
$IM/
)o read a filename from the user and to display the owner of the file using shell
programming.
$&-O%IT2M/
#. Start the program.
%. /ead the file name.
(. Use ls ]l command to list all the files in the directory in detail form and store
the output in a temporary file.
+. Use grep command to loo! for the filename in the list.
,. Use tr command to remove the additional lan! spaces and cut command to
extract the third field which is the owner of the file.
-. 3rint the owner name.
.. Stop the program
P%O-%$M"
D to read a filename from the user and to display the owner of the file
echo _?nter a file name`
read name
ls -l F tmp
if&grep 7name tmp F tmp#'
then
D tr -s 2 --s<ueeCe-repeats replace each input se<uence of a repeated character
D with a single occurrence of that character
D cut -3rints selected parts of lines from each B*1? to standard output.
D cut -d2 --delimiterL0?1*5 use 0?1*5 instead of )AB for field delimiter
D cut -f2 --fieldsL1*S) select only these fieldsH f( - third field
lineLdcat tmp#6tr -s " "6cut -d" " -f(d
fi
chec!Ldecho 7line 6wc -cd
echo " line L 7line"
echo "chec!L 7chec!"
if test 7chec! -gt #
then
echo " the owner of the file 7name is 7line"
else
echo ")he file 7name does not exist"
59
fi
OUTPUT/
MstaffNlocalhost OP7 sh p+++
?nter a file name
file#.txt
line L staff
chec!L -
the owner of the file file#.txt is staff
%"U&T"
)hus the Shell program for reading a filename from the user and to display the
owner of the file has een executed successfully.

"UPP&MNT$%+ P%O-%$M"
#. Rrite a Shell script to read a filename from the user and to display the file permission
of the file.
60
Ex. No. 3.5 Date:
Disp4a*in1 !i4enames
$IM/
)o read an alphaet from the user and to display the filenames starting with the
input using shell programming.
$&-O%IT2M/
#. Start the program.
%. /ead an alphaet ch.
(. Use find ]name command along with the read character and print the output to
a file.
+. 4hec! whether the file siCe is greater than Cero using ]s commandH if so print
the filenames using cat command.
,. Stop the program
P%O-%$M"
D to display all filenames starting with the user input alphaet in the current directory
echo " ?nter an alphaet ""
read ch
find -name "7chV" -print F tmp
if M -s tmp P
then
echo " )he files starting with 7ch are "
cat tmp
else
echo ")here are no files starting with the 7ch"
fi
rm tmp
OUTPUT
MstaffNlocalhost OP7 sh p++-
?nter an alphaet "
t
)he files starting with t are
.Atemp.c
61
.Atext
.At
.Atemp
.Atmp#
.Atmp
%"U&T"
)hus the Shell program for reading an alphaet from the user to display the
filenames starting with the input alphaet has een executed successfully.
"UPP&MNT$%+ P%O-%$M"
#. Rrite a Shell script to read a file name from the user and assign read2 write and
execute permissions for that particular file.
62
Ex. No. 3.6 Date:
mp4o*ee "a4ar* Detai4s
$IM/
)o calculate the employee T/A details using shell programming.
$&-O%IT2M/
#. Start the program.
%. ?nter the employee asic salary.
(. 4alculate 0A as #K@ of asic pay.
+. Use conditions to calculate the T/A ased on the asic pay.
,. Add 0A and T/A to the asic and print the result.
-. Stop the program.
P%O-%$M/
D ?mployee Salary 0etails
echo " ?nter the employee asic salary "
read s
daLdexpr 7s IV #K A #KKd
echo "0AL 7da"
if M 7s -gt ,KKK P
then
hraLdexpr 7s A ,d
echo "T/A L 7hra"
elif M 7s -ge +KKK -a 7s -le ,KKK P
then
hraLdexpr 7s A .d
echo "T/A L 7hra"
else
hraLdexpr 7s A #Kd
echo "T/A L 7hra"
fi
grossLdexpr 7s 9 7da 9 7hrad
echo ":ross salary L 7gross"
63
OUTPUT/
MstaffNlocalhost OP7 sh p%-
?nter the employee asic salary
.KKK
0AL .KK
T/A L #+KK
:ross salary L [#KK
%"U&T"
)hus the Shell program for calculating employee salary has een executed
successfully.
"UPP&MNT$%+ P%O-%$M"
#. Rrite a Shell script to prepare a grocery ill for + items.
64
Ex. No. 3. Date:
"um of ven num7ers
$IM/
)o sum the even numers using shell conditional statement programming.
$&-O%IT2M/
#. Start the program.
%. ?nter the limit. *nitialiCe sum as Cero.
(. Use conditional statement to generate a loop until the limit. :enerate the next
even numer. Add it to the sum.
+. 3rint the sum.
,. Stop the program.
P%O-%$M/
echo "?nter the balue for n"
read n
iL%
sLK
while M 7i -lt 7n P
do
sLdexpr 7s 9 7id
iLdexpr 7i 9 %d
done
echo "Sum of even numers up to 7n is 7s"
OUTPUT/
MstaffNlocalhost OP7 sh p%,
?nter the balue for n
%K
Sum of even numers up to %K is [K
%"U&T/
)hus the Shell program to sum the even numers has een executed successfully.
65
"UPP&MNT$%+ P%O-%$M"
#. Rrite a Shell script to generate Bionacci series.
66
Ex. No. 3.! Date:
"um of t3e Di1its of a Num7er
$IM/
)o sum the digits of a given numers using shell programming.
$&-O%IT2M/
#. Start the program.
%. /ead a numer n. Set sum as Cero.
(. Use conditional statements to generate a loop till the numer ecomes less than
#. ?xtract 1S0 of the numer. Add it to the sum.
+. 3rint the sum.
,. Stop the program.
P%O-%$M/
D Sum of 0igits of a Wumer
echo " ?nter the numer" "
read n
tempL7n
sumLK
while M 7n -gt K P
do
aLdexpr 7n @ #Kd
sumLdexpr 7sum 9 7ad
nLdexpr 7n A #Kd
done
echo "Wumer typed is 7temp and the sum of the digits is 7sum"
OUTPUT/
MstaffNlocalhost OP7 sh p%Q
?nter the numer"
+(-,%
Wumer typed is +(-,% and the sum of the digits is %K
%"U&T/
)hus the Shell program to sum the digits of a given numer has een executed
successfully.
67
"UPP&MNT$%+ P%O-%$M"
#. Rrite a Shell script to reverse the given numer.
68
Ex. No. 3." Date:
!actoria4 of a Num7er
$IM/
)o find the factorial of a numer using shell programming.
$&-O%IT2M/
#. Start the program.
%. /ead a numer.
(. Use looping statement to calculate the factorial value.
+. 3rint the factorial numer.
,. Stop the program.
P%O-%$M/
D Bactorial of a Wumer
echo " ?nter the numer" "
read n
iL#
fL#
while M 7i -le 7n P
do
fLdexpr 7f IV 7id
iLdexpr 7i 9 #d
done
echo ")he factorial of given numer 7n is 7f"
OUTPUT/
MstaffNlocalhost OP7 sh p%.
?nter the numer"
.
)he factorial of given numer . is ,K+K

%"U&T/
)hus the Shell program to find the factorial of a given numer has een executed
successfully.
69
"UPP&MNT$%+ P%O-%$M"
#. Rrite a Shell script to read n numers and find their sum.
70
Ex. No. 3.1# Date:
"trin1 Cop*
$IM/
)o copy the given string to another string using shell programming.
$&-O%IT2M/
#. Start the program.
%. /ead a string.
(. 4alculate the length of the string using wc ]c command
+. Use conditional statement to generate a loop till the length of the string.
?xtract
a character using cut ]c command and append that character to the target
string.
,. 3rint the target string.
-. Stop the program.
P%O-%$M/
D String 4opy
echo " ?nter the string " "
read s#
D wc -c counts the numer of characters in a string
lLdecho 7s# 6 wc -cd
iL#
while M 7i -le 7l P
do
D cut -c removes i
th
character in the string
es%Ldecho 7s# 6 cut -c 7id
iLdexpr 7i 9 #d
cs(Ldecho 7cs(7es%d
done
echo " )he actual string is 7s#"
echo " )he 4opied string is 7cs("
OUTPUT/
MstaffNlocalhost OP7 sh p%#,
?nter the string "
welcome
)he actual string is welcome
71
)he 4opied string is welcome
%"U&T/
)hus the Shell program to copy one string to another string has een executed
successfully.
"UPP&MNT$%+ P%O-%$M""
#. Rrite a Shell script to compare two strings. & hint use L operator '.
%. Rrite a Shell script to find the length of the given string.
72
Ex. No. 3.11 Date:
Menu driven Ca4cu4ator
$IM/
)o develop a menu driven calculator using shell programming.
$&-O%IT2M/
#. Start the program.
%. 0isplay the options. /ead choice.
(. Use case statement to find the entered choice. 3erform the operations ased on
the choice.
,. 3rint the result.
+. Stop the program.
P%O-%$M/
D*llustrate the use of the 4AS? statement
while true
do
echo " 5enu"
echo "#. Addition"
echo "%. Sutraction"
echo "(. 5ultiplication"
echo "+. 0ivision"
echo ",. 5odulus"
echo "-. =uit"
echo -n "?nter your 4hoice"
read ch
case 7ch in
#' read a
cLdexpr 7a 9 7d
echo 7cHH
%' read a
cLdexpr 7a - 7d
echo 7cHH
(' read a
cLdexpr 7a IV 7d
echo 7cHH
+' read a
73
cLdexpr 7a A 7d
echo 7cHH
,' read a
cLdexpr 7a @ 7d
echo 7cHH
-' rea!HH
V' echo "*nvalid 4hoice"
esac
done
OUTPUT
MstaffNlocalhost OP7 sh p%%
5enu
#. Addition
%. Sutraction
(. 5ultiplication
+. 0ivision
,. 5odulus
-. =uit
?nter your 4hoice#
#K %K
(K
5enu
#. Addition
%. Sutraction
(. 5ultiplication
+. 0ivision
,. 5odulus
-. =uit
?nter your 4hoice%
+, %(
%%
%"U&T/
)hus the Shell program to develop a menu driven calculator has een executed
successfully.
74
"UPP&MNT$%+ P%O-%$M"
#. Rrite a Shell script to develop a menu driven program to execute any , UW*a
commands.
75
Ex. No. 3.12 Date:
Po(er !unction
$IM/
)o write a shell program to perform power function.
$&-O%IT2M/
#. Start the program.
%. /ead x and n
(. *nitialiCe iL#2 prodL#.
+. Assign prodL prod V x.
,. *ncrement i y #.
-. /epeat step + ; step , until iELn.
.. 0isplay prod
Q. Stop the program.
P%O-%$M/
echo ?nter the numer
read x
echo ?nter the power
read n
iL#
prodL#
while M 7i -le 7n P
do
prodLdexpr 7prod IV 7xd
iLdexpr 7i 9 #d
done
echo 7x raised to 7n L 7prod
OUTPUT/
MstaffNlocalhost OP7 sh power
?nter the numer
(
?nter the power
(
( raised to ( L %.
%"U&T/
76
)hus the shell program for performing the power function is written and
executed.
"UPP&MNT$%+ P%O-%$M/
#. Rrite a Shell script to find the numer of ordinary files and directories in the current
directory. & hint" use ls ]l 6 grep ]c _fd` command for directory count '
77
Ex. No. 3.13 Date:
Prime Num7er -eneration
$IM/
)o write a shell program to print the prime numers from # to (K.
$&-O%IT2M/
#. Start the program.
%. *nitialiCe iL%2 nL%.
(. Set loop for nE(K.
+. Set loop for iELn-#.
,. *f n@i L K then goto step .H Otherwise goto step ,.
-. *ncrement i and goto step +.
.. *f nLi then print n.
Q. *ncrement n and goto step (.
[. Stop the program.
P%O-%$M/
nL%
echo 7n
while M 7n -le (K P
do
iL%
while M 7i -le dexpr 7n - #d P
do
if M dexpr 7n @ 7id -e< K P
then
rea!
else
iLdexpr 7i 9 #d
fi
done
if M 7n -e< 7i P
then
echo 7n
fi
nLdexpr 7n 9 #d
done
78
OUTPUT/
%
(
,
.
##
#(
#.
#[
%(
%[
%"U&T/
)hus the shell program for printing prime numers from # to (K is written and
executed.
"UPP&MNT$%+ P%O-%$M/
#. Rrite a Shell script to chec! whether the given numer is Armstrong numer or not.
79
Ex. No. 3.14 Date:
Com7inations of Di1its
$IM/
)o write a shell program to print all cominations of #2 % and (.
$&-O%IT2M/
#. Start the program.
%. Set a for loop for iL#to (.
(. Set a for loop for 8#to (.
+. Set a for loop for !L#to (.
,. 0isplay i2 8 and !
-. Stop the program.
P%O-%$M/
clear
for i in # % (
do
for 8 in # % (
do
for ! in # % (
do
echo 7i 78 7!
done
done
done
OUTPUT/
# # #
# # %
# # (
# % #
# % %
# % (
# ( #
# ( %
# ( (
% # #
% # %
80
% # (
% % #
% % %
% % (
% ( #
% ( %
% ( (
( # #
( # %
( # (
( % #
( % %
( % (
( ( #
( ( %
( ( (
%"U&T/
)hus the shell program to print all cominations of #2% and ( is written and
executed.
"UPP&MNT$%+ P%O-%$M/
#. Rrite a shell script to count the numer of digits in the given numer.
81
"UPP&MNT$%+ P%O-%$M""
C P%O-%$MMIN- ON UNIX
82
1. 5rite a C pro1ram to find t3e sum and difference of t(o matrices usin1 functions.
DincludeEstdio.hF
int aM#KPM#KP2 M#KPM#KP2 cM#KPM#KP2dM#KPM#KP2p2<2m2nH
void readmatrix&int M#KPM#KP'H
void add&int M#KPM#KP2 int M#KPM#KP'H
void su&int M#KPM#KP2 int M#KPM#KP'H
void display&int M#KPM#KP'H
int main&'
G
int i282!H
printf&"?nter the siCe of A matrix""'H
scanf&"@d@d"2;p2;<'H
printf&"?nter the siCe of B matrix""'H
scanf&"@d@d"2;m2;n'H
if&pLLm ;; <LLn'
G
printf&"?nter the elements of A matrix In"'H
readmatrix&a'H
printf&"?nter the elements of B matrix In"'H
readmatrix&'H
add&a2'H
su&a2'H
J
else
printf&"In5atrices cannot e added due to siCe mismatch..In"'H
printf&"Addition of A and B matrix In"'H
display&c'H
printf&"Sutraction of A and B matrix In"'H
display&d'H
return #H
J
void readmatrix&int xM#KPM#KP'
G
int i28H
for&iLKHiEpHi99'
for&8LKH8E<H899'
83
scanf&"@d"2;xMiPM8P'H
J
void add&int aM#KPM#KP2int M#KPM#KP'
G
int i28H
for&iLKHiEpHi99'
for&8LKH8E<H899'
cMiPM8PLaMiPM8P9MiPM8PH
J
void su&int aM#KPM#KP2int M#KPM#KP'
G
int i28H
for&iLKHiEpHi99'
for&8LKH8E<H899'
dMiPM8PLaMiPM8P-MiPM8PH
J
void display& int xM#KPM#KP'
G
int i28H
for&iLKHiEpHi99'
G for&8LKH8E<H899'
printf&"@,d"2xMiPM8P'H
printf&"In"'H
J
J
OUTPUT/
MstaffNlocalhost OP7 cc matadd.c
MstaffNlocalhost OP7 .Aa.out
?nter the siCe of A matrix"( (
?nter the siCe of B matrix"( (
?nter the elements of A matrix
+ + +
+ + +
+ + +
?nter the elements of B matrix
84
% % %
% % %
% % %
Addition of A and B matrix
- - -
- - -
- - -
Sutraction of A and B matrix
% % %
% % %
% % %
VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
#. 5rite a C pro1ram to find t3e product of t(o matrices usin1 functions.
DincludeEstdio.hF
int aM#KPM#KP2 M#KPM#KP2 cM#KPM#KP2p2<2m2nH
void readmatrix&int M#KPM#KP'H
void mul&int M#KPM#KP2 int M#KPM#KP'H
void display&int M#KPM#KP'H
int main&'
G
int i282!H
printf&"?nter the siCe of A matrix""'H
scanf&"@d@d"2;p2;<'H
printf&"?nter the siCe of B matrix""'H
scanf&"@d@d"2;m2;n'H
if&<LLm'
G
printf&"?nter the elements of A matrix In"'H
readmatrix&a'H
printf&"?nter the elements of B matrix In"'H
readmatrix&'H
mul&a2'H
J
else
printf&"In5atrices cannot e multiplied....In"'H
printf&"5ultiplication of A and B matrix In"'H
85
display&c'H
return #H
J
void readmatrix&int xM#KPM#KP'
G
int i28H
for&iLKHiEpHi99'
for&8LKH8E<H899'
scanf&"@d"2;xMiPM8P'H
J
void mul&int aM#KPM#KP2int M#KPM#KP'
G
int i282!H
for&iLKHiEpHi99'
for&8LKH8E<H899'
for&!LKH!E<H!99'
cMiPM8PLcMiPM8P9aMiPM!PVM!PM8PH
J
void display& int xM#KPM#KP'
G
int i28H
for&iLKHiEpHi99'
G for&8LKH8E<H899'
printf&"@,d"2xMiPM8P'H
printf&"In"'H
J
J
OUTPUT/
MstaffNlocalhost OP7 cc matmul.c
MstaffNlocalhost OP7 .Aa.out
?nter the siCe of A matrix"( (
?nter the siCe of B matrix"( (
?nter the elements of A matrix
# # #
# # #
# # #
86
?nter the elements of B matrix
# # #
# # #
# # #
5ultiplication of A and B matrix
( ( (
( ( (
( ( (
VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
.. 5rite a C pro1ram to print t3e factoria4 of a 1iven num7er usin1 recursion.
DincludeEstdio.hF
long fact&int'H
int main&'
G
long fH
int nH
printf&"In?nter a numer ""'H
scanf&"@d"2;n'H
f L fact&n'H
printf&")he factorial of @d is @ldIn"2n2f'H
return #H
J
long fact&int n'
G
if&&nLL#'66&nLLK''
return &#'H
else
return &nVfact&n-#''H
J
OUTPUT/
MstaffNlocalhost OP7 cc fact.c
MstaffNlocalhost OP7 .Aa.out
?nter a numer ",
)he factorial of , is #%K
VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
V
87
F. 5rite C pro1ram to 1enerate fo44o(in1 trian14e.
1
1 # .
1 # . F G
1 # . F G H J
DincludeEstdio.hF
int main&'
G int i282!2l2nH
printf&")ype W-no of lines in triangle""'H
scanf&"@d"2;n'H
lL#H
for&iL#28Ln-iHiELnHi9928--'
G
for&!L#H!EL8H!99'
printf&" "'H
for&!L#H!ELlH!99'
printf&"@,d"2!'H
printf&"In"'H
l9L%H
J
return #H
J
MstaffNlocalhost OP7 .Aa.out
)ype W-no of lines in triangle"(
#
#%(
#%(+,
VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
G. 5rite a C pro1ram to find t3e va4ue of
n
C
r
usin1 recursion.
DincludeEstdio.hF
float comi&float2float'H
int main&'
G
float n2rH
88
printf&"enter the value of nIn"'H
scanf&"@f"2;n'H
printf&"enter the value of rIn"'H
scanf&"@f"2;r'H
printf&"the result of &@f2@f' is @fIn"2n2r2comi&n2r''H
return #H
J
float comi&float n2float r'
G
if&&nLLK' 66 &rLLK''
return&#'H
else
return&&nAr'Vcomi&n-#2r-#''H
J
OUTPUT/
MstaffNlocalhost OP7 cc ncr.c
MstaffNlocalhost OP7 .Aa.out
enter the value of n
,
enter the value of r
(
the result of &,.KKKKKK2(.KKKKKK' is #K.KKKKKK
VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
H. 5rite a C pro1ram for strin1 cop* usin1 d*namic memor* a44ocation.
DincludeEstdio.hF
DincludeEmalloc.hF
Ddefine length %-
main&'
G
char Vs#2Vs%2Vs(2cH
int i2!H
s#L&char V'malloc&length VsiCeof &char''H
s%L&char V'malloc&length VsiCeof &char''H
printf&"In ?nter the string#""'H
scanf&"@s"2s#'H
iLKH
89
while&&cLV&s#9i''>LSIKS'
G
s%MiPLcH
i99H
J
s%MiPLSIKSH
printf&"In copied string is @sIn"2s%'H
J
OUTPUT/
MstaffNlocalhost OP7 cc strcpy.c
MstaffNlocalhost OP7 .Aa.out
?nter the string#"Tello
copied string is Tello
VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
J. 5rite a C pro1ram for strin1 compare usin1 d*namic memor* a44ocation.
DincludeEstdio.hF
DincludeEmalloc.hF
Ddefine length %-
int main&'
G
char Vs#2Vs%2cH
int i2!H
s#L&char V'malloc&length VsiCeof &char''H
s%L&char V'malloc&length VsiCeof &char''H
printf&"In ?nter the string#""'H
scanf&"@s"2s#'H
printf&"In ?nter the string%""'H
scanf&"@s"2s%'H
iLKH
while&V&s#9i'LL V&s%9i';; &V&s#9i'>LSIKS;; V&s%9i'>LSIKS''
i99H
if &V&s#9i'LLSIKS;; V&s%9i'LLSIKS'
printf&"In)he two strings are e<ualIn"'H
else
printf&"In)he two strings are not e<ualIn"'H
return #H
90
J
OUTPUT/
MstaffNlocalhost OP7 cc strcmp#.c
MstaffNlocalhost OP7 .Aa.out
?nter the string#"hai
?nter the string%"hai
)he two strings are e<ual
VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
K. 5rite a C pro1ram for sortin1 student record 7ased on student name.
D Dec4are arra* of structures (it3 on4* t(o mem7ers/ name, marL. E
DincludeEstdio.hF
DincludeEstdli.hF
DincludeEstring.hF
struct student
G
char nameM%,PH
int mar!sH
J VsH
void sort&struct student s#M P2 int n'H
int main&'
G
char tempM(KPH
int n2iH
printf&"In TOR 5AWU ?W)/*?S In"'H
scanf&"@d"2;n'H
printf&"In ?W)?/ )T? @d WA5?S and mar!s OW? BU OW? In"2n'H
sL&struct student V' calloc&n2siCeof&struct student ''H
for&iLKHiEnHi99'
scanf&"@s @d"2&s9i'-Fname2;&s9i'-Fmar!s'H
sort&s2n'H
printf&"In Student /ecord in Sorted form In"'H
for&iLKH iEnHi99'
G
printf&"@sIt@d"2&s9i'-Fname2&s9i'-Fmar!s'H
91
printf&"In"'H
J
J
void sort& struct student s#MP2int n'
G
int i282!H
struct student tempH
for&iLKHiELn-%Hi99'
for&8Li9#H8ELn-#H899'
if&strcmp&&s#9i'-Fname2&s#98'-Fname'F K'
G
tempLs#MiPH
s#MiP Ls#M8PH
s#M8PLtempH
J
J
OUTPUT/
TOR 5AWU ?W)/*?S
(
?W)?/ )T? ( WA5?S and mar!s OW? BU OW?
ra8a [K
!umar .K
!alai .K
Student /ecord in Sorted form
!alai .K
!umar .K
ra8a [K
VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
@. 5rite a C pro1ram to rep4ace a44 t3e 74anL spaces in a fi4e (it3 M$N s*m7o4.
DincludeEstdio.hF
main&'
G int aLK2wLK2dLK2lLK2tLKH char chH
B*1? Vptr#2Vptr%H
ptr#Lfopen&"inf.c"2"r"'H
ptr%Lfopen&"outf.c"2"w"'H
92
while&&chLgetc&ptr#'' >L ?OB'
G if &ch>LS S;; ch>LSItS'
putc&ch2ptr%'H
else putc&S7S2ptr%'H
J
fclose&ptr#'H fclose&ptr%'H
J
OUTPUT/
MstaffNlocalhost OP7 cc p(#K.c
MstaffNlocalhost OP7 .Aa.out
MstaffNlocalhost OP7 cat outf.c
7wel7come
hello7hai
hai7hello
VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
1). 5rite a C pro1ram to concatenate t(o fi4es.
Dinclude Estdio.hF
Dinclude Estdli.hF
int main&int argc2 char VargvMP'
G
long int xH
int iLKH
char c2strM%,PH
B*1? Vptr#2 Vptr%2Vptr(H
ptr#Lfopen&argvM%P2"r"'H
ptr%Lfopen&argvM(P2"r"'H
ptr(Lfopen&argvM+P2"w"'H
while &&cLgetc&ptr#'' >L?OB'
putc&c2ptr('H
while &&cLgetc&ptr%'' >L ?OB'
putc&c2ptr('H
fclose&ptr#'H
fclose&ptr%'H
fclose&ptr('H
printf&"InIn4ontents in concatenated file" In"'H
ptr(Lfopen&argvM+P2"r"'H
93
while &&cLgetc&ptr('' >L ?OB'
putchar&c'H
fclose&ptr('H
return #H
J
OUTPUT/
MstaffNlocalhost OP7 .Aa.out filecat.c file#.c file%.c file(.c
4ontents in concatenated file"
Tello
Relcome
)o
ZS/*?)
VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
11. 5rite a C pro1ram to convert t3e contents of a fi4e into uppercase.
DincludeEstdio.hF
main&'
G
char chH
B*1? Vptr#2Vptr%H
ptr#Lfopen&"inf.c"2"r"'H
ptr%Lfopen&"outf.c"2"w"'H
while&&chLgetc&ptr#'' >L ?OB'
G
if &isalpha&ch''
putc&toupper&ch'2ptr%'H
else
putc&ch2ptr%'H
J
fclose&ptr#'H
fclose&ptr%'H
J
OUTPUT/
MstaffNlocalhost OP7 cat inf.c
wel come
hello hai
hai hello
94
MstaffNlocalhost OP7 cc upper.c
MstaffNlocalhost OP7 .Aa.out
MstaffNlocalhost OP7 cat outf.c
R?1 4O5?
T?11O TA*
TA* T?11O
VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
1#. xtend t3e a7ove pro1ram to modif* and searc3 a particu4ar record in a fi4e.
void modify&'
G
struct student s#H
int foundLK2rollH
long offsetH
ptrLfopen&"stud.dat"2"r9t"'H
printf&"?nter the /oll no. to modify ""'H
scanf&"@d"2;roll'H
while &fread&;s#2siCeof&s#'2#2ptr''
G if &s#.rnoLLroll'
G
offsetLftell&ptr'-siCeof&s#'H
fsee!&ptr2offset2S??Z\S?)'H
s#.rnoLrollH
printf&"Wame ""'H
scanf&"@s"2s#.name'H
printf&"Age ""'H
scanf&"@d"2;s#.age'H
printf&"5ar!s ""'H
scanf&"@d"2;s#.mar!s'H
fwrite&;s#2siCeof&s#'2#2ptr'H
foundL#H
rea!H
J
J
if &foundLLK'
printf&"InIn............./ecord Wot Bound..........In"'H
fclose&ptr'H
95
J
void search&'
G
struct student s#H
int foundLK2rollH
ptrLfopen&"stud.dat"2"r"'H
printf&"?nter the /oll no. to search ""'H
scanf&"@d"2;roll'H
while &fread&;s#2siCeof&s#'2#2ptr''
G
if &s#.rnoLLroll'
G
printf&"InIn/ecord Bound............"'H
printf&"In/oll Wo. "@d "2s#.rno'H
printf&"InWame "@s "2s#.name'H
printf&"InAge "@d "2s#.age'H
printf&"In5ar!s "@d "2s#.mar!s'H
foundL#H
rea!H
J
J
if &foundLLK'
printf&"InIn............./ecord Wot Bound..........In"'H
fclose&ptr'H
J
OUTPUT/
#. Add
%. 0elete
(. 5odify
+. Search
,. 0isplay
-. ?xit
?nter your option &#--'" ,
/oll Wo." #
Wame. " Arun
96
Age " %K
5ar!s " [K
----------------------------------
#. Add
%. 0elete
(. 5odify
+. Search
,. 0isplay
-. ?xit
?nter your option &#--'" (
?nter the /oll no. to modify "#
Wame "Zumar
Age "%,
5ar!s "[.
#. Add
%. 0elete
(. 5odify
+. Search
,. 0isplay
-. ?xit
?nter your option &#--'" +
?nter the /oll no. to search "#
/ecord Bound............
/oll Wo. "#
Wame "Zumar
Age "%,
5ar!s "[.
#. Add
%. 0elete
(. 5odify
+. Search
,. 0isplay
-. ?xit?nter your option &#--'"-
VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
97
1.. xtend t3e a7ove pro1ram to move t3e fi4e pointer to t3e 7e1innin1 of t3e fi4e
usin1 fseeLDE.
Dinclude Estdio.hF
Dinclude Estdli.hF
int main&'
G
long int xH
int iLKH
char c2strM%,PH
B*1? VptrH
system&"clear"'H
ptrLfopen&"st.txt"2"w9"'H
printf&"?nter a string " In"'H
while &&cLgetchar&'' >L SInS'
putc&c2ptr'H
fsee!&ptr2K12S??Z\S?)'H
xLftell&ptr'H
printf&"In3tr is at the egining of the file2 3trL @ldIn"2x'H
printf&"InIn)he String stored in the file is " " 'H
while &&cLgetc&ptr'' >L ?OB'
putchar&c'H
fclose&ptr'H
return #
J
OUTPUT/
MstaffNlocalhost OP7 cc p(#,.c
MstaffNlocalhost OP7 .Aa.out
?nter a string "
Tello
3tr is at the egining of the file2 3trL K
)he String stored in the file is " Tello
MstaffNlocalhost OP7
VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
98
1F. 5rite a C pro1ram to define a macro for findin1 t3e 4ar1est of t3ree num7ers.
Dinclude Estdio.hF
Ddefine max&a22c' &&&aF';;&aFc''ea"&Fc'e"c'
int main&'
G
int a22cH
printf&"enter three numers" "'H
scanf&"@d@d@d"2;a2;2;c'H
printf&"1argest " @dIn"2max&a22c''H
J
OUTPUT/
MstaffNlocalhost OP7 cc large.c
MstaffNlocalhost OP7 .Aa.out
enter three numers" , . %
1argest " .
MstaffNlocalhost OP7
VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
1G. 5rite a C pro1ram to define a macro for disp4a*in1 t3e 1iven strin1.
Dinclude Estdio.hF
Ddefine print&msg' printf&msg'
int main&'
G
print&"TelloIn"'H
J
OUTPUT/
MstaffNlocalhost OP7 .Aa.out
Tello
MstaffNlocalhost OP7
VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
99
"UPP&MNT$%+ P%O-%$M""
"2&& P%O-%$MMIN-
100
1. 5rite a s3e44 script to find t3e minimum of t3ree num7ers.
MstaffNlocalhost OP7 cat pa%#
echo "?nter valuefor A2 B and 4"
read a
read
read c
if test 7a -lt 7 -a 7a -lt 7c
then
echo "7a is smallest numer"
elif test 7 -lt 7c
then
echo "7 is smallest numer"
else
echo "7c is smallest numer"
fi
OUTPUT
MstaffNlocalhost OP7 sh pa%#
?nter valuefor A2 B and 4
+,
%
#%
% is smallest numer
VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
#. 5rite a s3e44 script to read a num7er and find its s6uare and cu7e.
MstaffNlocalhost OP7 cat pa%%
D to find s<uare and cue of a numer
echo " enter a numer"
read n
snLdexpr 7n IV 7nd
cnLdexpr 7sn IV 7nd
echo " S<uare of nL7n is 7sn"
echo " cue of nL7n is 7cn"
OUTPUT
MstaffNlocalhost OP7 sh pa%%
enter a numer
+
101
S<uare of nL+ is #-
cue of nL+ is -+
VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
.. 5rite a s3e44 script to find area of a circ4e.
D area of a circle
echo "?nter r balue"
read r
echo 7r
cLdexpr "scaleL%H & (.#+ V 7r V 7r ' " 6 c d
DcLdexpr 7c IV , A [d
echo "Area of a circle L 7c"
OUTPUT
MstaffNlocalhost OP7 sh pa%(
?nter r balue
,
,
Area of a circle L .Q.,K
VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
F. 5rite a s3e44 script to find t3e simp4e interest.
MstaffNlocalhost OP7 cat pa%+
D Simple interest
echo "?nter 3rincipal balue"
read p
echo "?nter year value"
read n
echo " ?nter rate of interest"
read r
cLdexpr "scaleL%H & 7p V 7n V 7r ' A #KK " 6 c d
DcLdexpr 7c IV , A [d
echo "Simple *nterest L 7c"
OUTPUT
MstaffNlocalhost OP7 sh pa%+
?nter 3rincipal balue
#KKKK
?nter year value
102
%
?nter rate of interest
#K
Simple *nterest L %KKK.KK
VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
G. 5rite a s3e44 script to c3ecL (3et3er t3e 1iven num7er is divisi74e 7* F and
J.
MstaffNlocalhost OP7 cat pa%,
D 4hec! whether numer is divisile y + and .
echo "?nter a numer..."
read n
d#Ldexpr 7n @ +d
d%Ldexpr 7n @ .d
if M 7d# -e< K -a 7d% -e< K P
then
echo " numerL7n is divisile y + and ."
else
echo " numerL7n is not divisile y + and ."
fi
OUTPUT
MstaffNlocalhost OP7 sh pa%,
?nter a numer...
%Q
numerL%Q is divisile y + and .
MstaffNlocalhost OP7 sh pa%,
?nter a numer...
%K
numerL%K is not divisile y + and .
VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
H. 5rite a s3e44 script to c3ecL (3et3er t3e 1iven *ear is 4eap *ear or not.
3/O:/A5
MstaffNlocalhost OP7 cat pa%-
D leap year or not
echo "?nter a year..."
read n
103
d#Ldexpr 7n @ +d
if M 7d# -e< K P
then
echo " 7n year is leap year"
else
echo " 7n year is not a leap year"
fi
OUTPUT
sh pa%-
?nter a year...
%K#%
%K#% year is leap year
VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
J. 5rite a "3e44 script to read a fi4ename from t3e user and to disp4a* t3e fi4e
permission of t3e fi4e.
MstaffNlocalhost OP7 cat pa%.
D to read a filename from the user and to display the owner of the file
echo "?nter a file name "
read name
ls -l F tmp
if&grep 7name tmp F tmp#'
then
D tr -s 2 --s<ueeCe-repeats replace each input se<uence of a repeated character
D with a single occurrence of that character
D cut -3rints selected parts of lines from each B*1? to standard output.
D cut -d2 --delimiterL0?1*5 use 0?1*5 instead of )AB for field delimiter
D cut -f2 --fieldsL1*S) select only these fieldsH f# - first field
lineLdcat tmp#6tr -s " "6cut -d" " -f#d
fi
chec!Ldecho 7line 6wc -cd
echo " line L 7line"
echo "chec!L 7chec!"
if test 7chec! -gt #
then
echo " the permissions of the file 7name is 7line"
104
else
echo ")he file 7name does not exist"
fi
OUTPUT
MstaffNlocalhost OP7 sh pa%.
?nter a file name
file#.txt
line L -rw-rw-r--
chec!L ##
the permissions of the file file#.txt is -rw-rw-r$
VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
K. 5rite a "3e44 script to read a fi4e name from t3e user and assi1n read, (rite
and execute permissions for t3at particu4ar fi4e.
D to assign all rights to a file
echo " ?nter a file name""
read fname
ls -l 7fname
chmod ... 7fname
echo "permissions are given"
ls -l 7fname
OUTPUT
?nter a file name"
file#.txt
-rw-rw-r-- # staff staff [% Jan %( #-"## file#.txt
permissions are given
-rwxrwxrwx # staff staff [% Jan %( #-"## file#.txt
@. 5rite a "3e44 script to prepare a 1rocer* 7i44 for F items.
MstaffNlocalhost OP7 cat pa%Q
D :rocery Bill 0etails
echo " ?nter the *tem# =uantity and 3rice "
read <#
read p#
echo " ?nter the *tem% =uantity and 3rice "
read <%
read p%
105
echo " ?nter the *tem( =uantity and 3rice "
read <(
read p(
echo " ?nter the *tem+ =uantity and 3rice "
read <+
read p+
illLdexpr "scaleL%H &7<# V 7p#' 9 &7<% V 7p%' 9 &7<( V 7p(' 9& 7<+ V 7p+'" 6 cd
echo ":rcery Bill L 7ill"
OUTPUT
MstaffNlocalhost OP7 sh pa%Q
?nter the *tem# =uantity and 3rice
%
,.,K
?nter the *tem% =uantity and 3rice
(
-
?nter the *tem( =uantity and 3rice
+
.
?nter the *tem+ =uantity and 3rice
,
Q
:rcery Bill L [..KK
VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
1). 5rite a "3e44 script to 1enerate !i7onacci series.
MstaffNlocalhost OP7 cat pa%[
D Bionacci Wumers
echo "?nter the balue for n"
read n
i#LK
i%L#
i(Ldexpr 7i# 9 7i%d
echo "7i# 7i% 7i("
iL(
while M 7i -lt 7n P
106
do
i#L7i%
i%L7i(
i(Ldexpr 7i# 9 7i%d
iLdexpr 7i 9 #d
echo "7i("
done
OUTPUT
MstaffNlocalhost OP7 sh pa%[
?nter the balue for n
#K
K # #
%
(
,
Q
#(
%#
(+
VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
11. 5rite a "3e44 script to reverse t3e 1iven num7er.
MstaffNlocalhost OP7 cat pa%##
D reverse of a Wumer
echo " ?nter the numer" "
read n
tempL7n
rLK
while M 7n -gt K P
do
aLdexpr 7n @ #Kd
rLdexpr 7r IV #K 9 7ad
nLdexpr 7n A #Kd
done
echo "Wumer typed is 7temp and the reverse is 7r"
107
OUTPUT
MstaffNlocalhost OP7 sh pa%##
?nter the numer"
,+(%#
Wumer typed is ,+(%# and the reverse is #%(+,
VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
1#. 5rite a "3e44 script to c3ecL (3et3er t3e 1iven num7er is $rmstron1
num7er or not.
MstaffNlocalhost OP7 cat pa%#%
D Armstrong Wumer
echo " ?nter the numer" "
read n
tempL7n
rLK
while M 7n -gt K P
do
aLdexpr 7n @ #Kd
rLdexpr 7a IV 7a IV 7a 9 7rd
nLdexpr 7n A #Kd
done
if M 7r -e< 7temp P
then
echo "Wumer typed 7temp is an armstrong numer "
else
echo " numer typed 7temp is not an armstrong numer "
fi
OUTPUT
MstaffNlocalhost OP7 sh pa%#%
?nter the numer"
#,(
Wumer typed #,( is an armstrong numer
MstaffNlocalhost OP7 sh pa%#%
?nter the numer"
#%(+
numer typed #%(+ is not an armstrong numer
VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
108
1.. 5rite a "3e44 script to read n num7ers and find t3eir sum.
MstaffNlocalhost OP7 cat pa%#(
D Sum of W numers
echo " Tow many numers you want to add e... "
read n
iL#
sLK
while M 7i -le 7n P
do
echo " ?nter 7i numer"
read a
sLdexpr 7s 9 7a d
iLdexpr 7i 9 #d
done
echo ")he Sum of 7n numers L 7s"
OUTPUT
MstaffNlocalhost OP7 sh pa%#(
Tow many numers you want to add e...
.
?nter # numer
#
?nter % numer
%
?nter ( numer
(
?nter + numer
+
?nter , numer
,
?nter - numer
-
?nter . numer
.
)he Sum of . numers L %Q
VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
109
1F. 5rite a "3e44 script to compare t(o strin1s. D 3int use O operator E.
MstaffNlocalhost OP7 cat pa%#+
D String 4ompare
echo " ?nter the first string ".. "
read s#
echo "?nter second string ".."
read s%
if M 7s# L 7s% P
then
echo "7s# and 7s% are e<ual"
else
echo "7s# and 7s% are not e<ual "
fi
OUTPUT
MstaffNlocalhost OP7 sh pa%#+
?nter the first string "..
!sr
?nter second string "..
!sriet
!sr and !sriet are not e<ual
VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
1G. 5rite a "3e44 script to find t3e 4en1t3 of t3e 1iven strin1.
MstaffNlocalhost OP7 cat pa%#,
D length of string
echo " ?nter the string " "
read s#
lLdexpr 7s# 6 wc -c d
lLdexpr 7l - #d
echo " )he length of the given string 7s# is 7l"
OUTPUT
MstaffNlocalhost OP7 sh pa%#,
?nter the string "
computer
)he length of the given string computer is Q
VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
110
1H. 5rite a "3e44 script to deve4op a menu driven pro1ram to execute an* G
UNIX commands.
MstaffNlocalhost OP7 cat pa%#-
D 5enu driven program for , unix commands
while true
do
echo " 5enu "
echo " #. )odaySs date"
echo " %. 1ist of users "
echo " (. 4urrent wor!ing directory "
echo " +. 3rocess State "
echo " ,. Bree dis! Space "
echo " -. =uit"
read ch
case 7ch in
#' dateHH
%' whoHH
(' pwdHH
+' psHH
,' dfHH
-' rea!HH
V' echo "*nvalid choice "
esac
done
OUTPUT
MstaffNlocalhost OP7 sh pa%#-
5enu
#. )odaySs date
%. 1ist of users
(. 4urrent wor!ing directory
+. 3rocess State
,. Bree dis! Space
-. =uit
#
)ue Jan %+ #-"#%",, *S) %K#%
5enu
#. )odaySs date
111
%. 1ist of users
(. 4urrent wor!ing directory
+. 3rocess State
,. Bree dis! Space
-. =uit
%
root tty# %K#%-K#-%+ K["#K
staff ptsAK %K#%-K#-%+ #+"++ &#[%.#-Q.,.##'
staff% ptsA# %K#%-K#-%+ #+"+- &#[%.#-Q.,.#+'
staff ptsA% %K#%-K#-%+ #K"(- &#[%.#-Q.,.#%'
5enu
#. )odaySs date
%. 1ist of users
(. 4urrent wor!ing directory
+. 3rocess State
,. Bree dis! Space
-. =uit
(
AhomeAstaff
5enu
#. )odaySs date
%. 1ist of users
(. 4urrent wor!ing directory
+. 3rocess State
,. Bree dis! Space
-. =uit
+
3*0 ))U )*5? 450
#%[++ ptsA% KK"KK"KK ash
#(..[ ptsA% KK"KK"KK !sh
#(.QK ptsA% KK"KK"KK csh
%#+.. ptsA% KK"KK"KK sh
%#-.- ptsA% KK"KK"KK ps
5enu
#. )odaySs date
%. 1ist of users
(. 4urrent wor!ing directory
+. 3rocess State
112
,. Bree dis! Space
-. =uit
-
VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
1J. 5rite a "3e44 script to find t3e num7er of ordinar* fi4es and directories in
t3e current director*. D 3int/ use 4s P4 B 1rep Pc Q>dR command for director*
count
MstaffNlocalhost OP7 cat pa%#.
D Bile and 0irectory 4ount in the current wor!ing directory
var#Ldls -l 6 grep -c "fd"d
var%Ldls -l 6 grep -c "f-"d
echo " Bile count L 7var%"
echo " 0irectory countL 7var#"
OUTPUT
MstaffNlocalhost OP7 sh pa%#.
Bile count L Q.
0irectory countL #
VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
1K. 5rite a s3e44 script to count t3e num7er of di1its in a 1iven num7er
D Wumer of digits in a Wumer
echo " ?nter the numer" "
read n
tempL7n
sdLK
while M 7n -gt K P
do
aLdexpr 7n @ #Kd
sdLdexpr 7sd 9 #d
nLdexpr 7n A #Kd
done
echo "Wumer typed is 7temp and the numer of digits in the given numer is 7sd"
OUTPUT
MstaffNlocalhost OP7 sh pa%#Q
?nter the numer"
,+,++
Wumer typed is ,+,++ and the numer of digits in the given numer is ,
VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
113

Вам также может понравиться