Sie sind auf Seite 1von 44

MASTER OF COMPUTER APPLICATIONS

I SEMESTER
2013 SCHEME 13MCA17 UNIX PROGRAMMING LABORATORY MANUAL

COMPILED BY 1. Mr. B.H.Cha !ra"h#$ar 2. Mr". A"h%& & B Ma!h'" 2013(1)

Unix Programming Laboratory Manual

I Semester MCA 2013-14

UNIX Pr*+ra,,& + La-*ra.*r/ S'-0#1. C*!#2 13MCA17 H*'r"45##$2 3 T*.a7 H*'r"2 )2 A.


$.

I.A. Mar$"2 30 E6a, Mar$"2 30 E6a, H*'r"2 3

x!lore t"e Unix en#ironment


x!lore #i e%itor &it" #im tutor. Per'orm t"e 'ollo&ing o!erations using #i e%itor( but not limite% to) 1. insert *"ara*ter( %elete *"ara*ter( re!la*e *"ara*ter 2. Sa#e t"e 'ile an% *ontinue &or+ing 3. sa#e t"e 'ile a exit t"e e%itor 4. ,uit t"e e%itor -. ,uit &it"out sa#ing t"e 'ile .. rename a 'ile /. insert lines( %elete lines( 0. set line numbers 1. sear*" 'or a !attern 10. mo#e 'or&ar% an% ba*+&ar%

1. a. 2rite a s"ell s*ri!t t"at ta+es a #ali% %ire*tory name as an argument an% re*ursi#ely %es*en% all t"e sub%ire*tories( 'in%s t"e maximum lengt" o' any 'ile in t"at "ierar*"y an% &rites t"is maximum #alue to t"e stan%ar% out!ut. b. 2rite a s"ell s*ri!t t"at a**e!ts a !at" name an% *reates all t"e *om!onents in t"at !at" name as %ire*tories. 3or exam!le( i' t"e s*ri!t is name% m!*( t"en t"e *omman% m!* a4b4*4% s"oul% *reate %ire*tories a( a4b( a4b4*( a4b4*4%. 2. a. 2rite a s"ell s*ri!t t"at a**e!ts t&o 'ile names as arguments( *"e*+s i' t"e !ermissions 'or t"ese 'iles are i%enti*al an% i' t"e !ermissions are i%enti*al( out!ut *ommon !ermissions an% ot"er&ise out!ut ea*" 'ile name 'ollo&e% by its !ermissions. b. 2rite a s"ell s*ri!t &"i*" a**e!ts #ali% log-in names as arguments an% !rints t"eir *orres!on%ing "ome %ire*tories( i' no arguments are s!e*i'ie%( !rint a suitable error message. 3. a. Create a s*ri!t 'ile *alle% 'ile-!ro!erties t"at rea%s a 'ile name entere% an% out!uts it !ro!erties. b. 2rite s"ell s*ri!t to im!lement terminal lo*+ing 5similar to t"e lo*+ *omman%6. It s"oul% !rom!t t"e user 'or a !ass&or%. A'ter a**e!ting t"e !ass&or% entere% by t"e user( it must !rom!t again 'or t"e mat*"ing !ass&or% as *on'irmation an% i' mat*" o**urs( it must lo*+ t"e +ey&or% until a mat*"ing !ass&or% is entere% again by t"e user( 7ote t"at t"e s*ri!t must be &ritten to %isregar% $8 A9( *ontrol-:. 7o time limit nee% be im!lemente% 'or t"e lo*+ %uration :e!t.o' MCA( 8;C 2

Unix Programming Laboratory Manual

I Semester MCA 2013-14

4.

a. 2rite a s"ell s*ri!t t"at a**e!t one or more 'ilenames as argument an% *on#ert all o' t"em to u!!er*ase( !ro#i%e% t"ey exist in *urrent %ire*tory. b. 2rite a s"ell s*ri!t t"at %is!lays all t"e lin+s to a 'ile s!e*i'ie% as t"e 'irst argument to t"e s*ri!t. <"e se*on% argument( &"i*" is o!tional( *an be use% to s!e*i'y in &"i*" t"e sear*" is to begin. I' t"is se*on% argument is not !resent( t"e sear*" is to begin in *urrent &or+ing %ire*tory. In eit"er *ase( t"e starting %ire*tory as &ell as all its sub%ire*tories at all le#els must be sear*"e%. <"e s*ri!t nee% not in*lu%e any error *"e*+ing.

-. a.

2rite a s"ell s*ri!t t"at a**e!ts 'ilenames as arguments an% %is!lays its *reation time i' t"e 'ile exists an% i' t"e 'ile %oes not exists( sen% out!ut error message.

b. 2rite a s"ell s*ri!t to %is!lay t"e *alen%ar 'or *urrent mont" &it" *urrent %ate re!la*e% by = or == %e!en%ing on &"et"er t"e %ate "as one %igit or t&o %igits. .. a. 2rite a s"ell s*ri!t to 'in% a 'ile4s t"at mat*"es a !attern gi#en as *omman% line argument in t"e "ome %ire*tory( %is!lay t"e *ontents o' t"e 'ile an% *o!y t"e 'ile into t"e %ire*tory >4my%ir. b. 2rite a s"ell s*ri!t to list all t"e 'iles in a %ire*tory &"ose 'ilename is at least 10 *"ara*ters. 5Use ex!r *omman% to *"e*+ t"e lengt"6. /. a. 2rite a s"ell s*ri!t t"at gets exe*ute% an% %is!lays t"e message eit"er ?@oo% MorningA or ?@oo% A'ternoonA or ?@oo% #eningA %e!en%ing u!on time at &"i*" t"e user logs in. b. 2rite a s"ell s*ri!t t"at a**e!t a list o' 'ilenames as its argument( *ount an% re!ort o**urren*e o' ea*" &or% t"at is !resent in t"e 'irst argument 'ile on ot"er argument 'iles. 0. a. 2rite a s"ell s*ri!t t"at %etermine t"e !erio% 'or &"i*" a s!e*i'ie% user is &or+ing on system an% %is!lay a!!ro!riate message. b. 2rite a s"ell s*ri!t t"at re!orts t"e logging in o' a s!e*i'ie% user &it"in one minute a'ter "e4s"e log in. <"e s*ri!t automati*ally terminate i' s!e*i'ie% user %oes not log in %uring a s!e*i'ie% !erio% o' time. 1. a. 2rite a s"ell s*ri!t t"at a**e!ts t"e 'ile name( starting an% en%ing line number as an argument an% %is!lay all t"e lines bet&een t"e gi#en line number. b. 2rite a s"ell s*ri!t t"at 'ol%s long lines into 40 *olumns. <"us any line t"at ex*ee%s 40 *"ara*ters must be bro+en a'ter 40t"( a ?BA is to be a!!en%e% as t"e in%i*ation o' 'ol%ing an% t"e !ro*essing is to be *ontinue% &it" t"e resi%ue. <"e in!ut is to be su!!lie% t"roug" a text 'ile *reate% by t"e user.

:e!t.o' MCA( 8;C

Unix Programming Laboratory Manual

I Semester MCA 2013-14

10. a. 2rite an a&+ s*ri!t t"at a**e!ts %ate argument in t"e 'orm o' mm-%%-yy an% %is!lays it in t"e 'orm o' %ay( mont"( an% year. <"e s*ri!t s"oul% *"e*+ t"e #ali%ity o' t"e argument an% in t"e *ase o' error( %is!lay a suitable message. b. 2rite an a&+ s*ri!t to %elete %u!li*ate% line 'rom a text 'ile. <"e or%er o' t"e original lines must remain un*"ange%. 11. a. 2rite an a&+ s*ri!t to 'in% out total number o' boo+s sol% in ea*" %is*i!line as &ell as total boo+ sol% using asso*iate array %o&n table as gi#en belo&. le*tri*al 34 Me*"ani*al ./ le*tri*al 00 Com!uter S*ien*e 43 Me*"ani*al .Ci#il 10 Com!uter S*ien*e .4 b. 2rite an a&+ s*ri!t to *om!ute gross salary o' an em!loyee a**or%ing to rule gi#en belo&. I' basi* salary is C 10000 t"en D8AE1-F o' basi* G :AE4-F o' basi*. I' basi* salary is HE10000 t"en D8AE20F o' basi* G :AE-0F o' basi*. N*.#2 I .h# #6a,& a.&* #a1h ".'!# . 8&1$" * # 9'#".&* :r*, a 7*. *: all .h# 11 9'#".&* ". ;'#".&* A < B *. .* -# & 17'!#! :*r #6a,& a.&* .

:e!t.o' MCA( 8;C

Unix Programming Laboratory Manual CONTENTS SL. N*. A.


$.

I Semester MCA 2013-14

E68#r&,# ." x!lore t"e Unix en#ironment


x!lore #i e%itor &it" #im tutor. Per'orm t"e 'ollo&ing o!erations using #i e%itor( but not limite% to) 1. insert *"ara*ter( %elete *"ara*ter( re!la*e *"ara*ter 2. Sa#e t"e 'ile an% *ontinue &or+ing 3. sa#e t"e 'ile a exit t"e e%itor 4. ,uit t"e e%itor -. ,uit &it"out sa#ing t"e 'ile .. rename a 'ile /. insert lines( %elete lines( 0. set line numbers 1. sear*" 'or a !attern 10. mo#e 'or&ar% an% ba*+&ar%

Pa+# N*.
11 12

1. a

2rite a s"ell s*ri!t t"at ta+es a #ali% %ire*tory name as an argument an% re*ursi#ely %es*en% all t"e sub%ire*tories( 'in%s t"e maximum lengt" o' any 'ile in t"at "ierar*"y an% &rites t"is maximum #alue to t"e stan%ar% out!ut. 2rite a s"ell s*ri!t t"at a**e!ts a !at" name an% *reates all t"e *om!onents in t"at !at" name as %ire*tories. 3or exam!le( i' t"e s*ri!t is name% m!*( t"en t"e *omman% m!* a4b4*4% s"oul% *reate %ire*tories a( a4b( a4b4*( a4b4*4%. 2rite a s"ell s*ri!t t"at a**e!ts t&o 'ile names as arguments( *"e*+s i' t"e !ermissions 'or t"ese 'iles are i%enti*al an% i' t"e !ermissions are i%enti*al( out!ut *ommon !ermissions an% ot"er&ise out!ut ea*" 'ile name 'ollo&e% by its !ermissions.

14

1-

2. a

1.

2rite a s"ell s*ri!t &"i*" a**e!ts #ali% log-in names as arguments an% !rints t"eir *orres!on%ing "ome %ire*tories( i' no arguments are s!e*i'ie%( !rint a suitable error message. 3. a Create a s*ri!t 'ile *alle% 'ile-!ro!erties t"at rea%s a 'ile name entere% an% out!uts it !ro!erties.
$ b s"oul% !rom!t t"e user 'or a !ass&or%. A'ter a**e!ting t"e !ass&or% entere% by t"e

1/ 11

2rite s"ell s*ri!t to im!lement terminal lo*+ing 5similar to t"e lo*+ *omman%6. It

20

user( it must !rom!t again 'or t"e mat*"ing !ass&or% as *on'irmation an% i' mat*" o**urs( it must lo*+ t"e +ey&or% until a mat*"ing !ass&or% is entere% again by t"e user( 7ote t"at t"e s*ri!t must be &ritten to %isregar% $8 A9( *ontrol-:. 7o time limit nee% be im!lemente% 'or t"e lo*+ %uration 4. a 2rite a s"ell s*ri!t t"at a**e!t one or more 'ilenames as argument an% *on#ert all o' :e!t.o' MCA( 8;C -

21

Unix Programming Laboratory Manual t"em to u!!er*ase( !ro#i%e% t"ey exist in *urrent %ire*tory.

I Semester MCA 2013-14

b t"e s*ri!t. <"e se*on% argument( &"i*" is o!tional( *an be use% to s!e*i'y in &"i*"

2rite a s"ell s*ri!t t"at %is!lays all t"e lin+s to a 'ile s!e*i'ie% as t"e 'irst argument to

22

t"e sear*" is to begin. I' t"is se*on% argument is not !resent( t"e sear*" is to begin in *urrent &or+ing %ire*tory. In eit"er *ase( t"e starting %ire*tory as &ell as all its sub%ire*tories at all le#els must be sear*"e%. <"e s*ri!t nee% not in*lu%e any error *"e*+ing. -. a 2rite a s"ell s*ri!t t"at a**e!ts 'ilenames as arguments an% %is!lays its *reation time i' t"e 'ile exists an% i' t"e 'ile %oes not exists( sen% out!ut error message. 2rite a s"ell s*ri!t to %is!lay t"e *alen%ar 'or *urrent mont" &it" *urrent %ate re!la*e% by = or == %e!en%ing on &"et"er t"e %ate "as one %igit or t&o %igits. .. a 2rite a s"ell s*ri!t to 'in% a 'ile4s t"at mat*"es a !attern gi#en as *omman% line argument in t"e "ome %ire*tory( %is!lay t"e *ontents o' t"e 'ile an% *o!y t"e 'ile into t"e %ire*tory >4my%ir.
b

23 24 2-

2rite a s"ell s*ri!t to list all t"e 'iles in a %ire*tory &"ose 'ilename is at least 10 *"ara*ters. 5Use ex!r *omman% to *"e*+ t"e lengt"6. /. a 2rite a s"ell s*ri!t t"at gets exe*ute% an% %is!lays t"e message eit"er ?@oo% MorningA or ?@oo% A'ternoonA or ?@oo% #eningA %e!en%ing u!on time at &"i*" t"e user logs in.
b b

2. 2/

2rite a s"ell s*ri!t t"at a**e!t a list o' 'ilenames as its argument( *ount an% re!ort o**urren*e o' ea*" &or% t"at is !resent in t"e 'irst argument 'ile on ot"er argument 'iles. 2rite a s"ell s*ri!t t"at %etermine t"e !erio% 'or &"i*" a s!e*i'ie% user is &or+ing on system an% %is!lay a!!ro!riate message. 2rite a s"ell s*ri!t t"at re!orts t"e logging in o' a s!e*i'ie% user &it"in one minute

20

0. a

21

b a'ter "e4s"e log in. <"e s*ri!t automati*ally terminate i' s!e*i'ie% user %oes not log in

30 31

%uring a s!e*i'ie% !erio% o' time. 1. a 2rite a s"ell s*ri!t t"at a**e!ts t"e 'ile name( starting an% en%ing line number as an argument an% %is!lay all t"e lines bet&een t"e gi#en line number.
b 40 *"ara*ters must be bro+en a'ter 40 t"( a ?BA is to be a!!en%e% as t"e in%i*ation o'

2rite a s"ell s*ri!t t"at 'ol%s long lines into 40 *olumns. <"us any line t"at ex*ee%s 'ol%ing an% t"e !ro*essing is to be *ontinue% &it" t"e resi%ue. <"e in!ut is to be su!!lie% t"roug" a text 'ile *reate% by t"e user.

32

10. a

2rite an a&+ s*ri!t t"at a**e!ts %ate argument in t"e 'orm o' mm-%%-yy an% %is!lays it in t"e 'orm o' %ay( mont"( an% year. <"e s*ri!t s"oul% *"e*+ t"e #ali%ity o' t"e argument an% in t"e *ase o' error( %is!lay a suitable message. b 2rite an a&+ s*ri!t to %elete %u!li*ate% line 'rom a text 'ile. <"e or%er o' t"e original .

34 34

:e!t.o' MCA( 8;C

Unix Programming Laboratory Manual


11. a

I Semester MCA 2013-14


3-

lines must remain un*"ange%. 2rite an a&+ s*ri!t to 'in% out total number o' boo+s sol% in ea*" %is*i!line as &ell as total boo+ sol% using asso*iate array %o&n table as gi#en belo&. le*tri*al 34 Me*"ani*al ./ le*tri*al 00 Com!uter S*ien*e 43 Me*"ani*al .Ci#il 10 Com!uter S*ien*e .4 2rite an a&+ s*ri!t to *om!ute gross salary o' an em!loyee a**or%ing to rule gi#en belo&. I' basi* salary is C 10000 t"en D8AE1-F o' basi* G :AE4-F o' basi*. I' basi* salary is HE10000 t"en D8AE20F o' basi* G :AE-0F o' basi*.
A%%itional xer*ises ;i#a ;o*e ,uestions Lab *y*les

3.

13 14 1-

30 31 41

:e!t.o' MCA( 8;C

Unix Programming Laboratory Manual

I Semester MCA 2013-14

Dos and Dont in the Laboratory DO ..


Come !re!are% to t"e Lab. Submit your 8e*or%s to t"e Le*turer an% sign in t"e Log $oo+ on entering t"e Lab. 3ollo& t"e Lab exer*ise *y*les as instru*te% by t"e :e!artment. ;iolating t"e same &ill result in %e%u*tion o' mar+s. Use t"e same login5i' any6 assigne% to you. Put t"e *"airs ba*+ to its !osition be'ore you lea#e. $a*+log exer*ises to be exe*ute% a'ter *om!leting regular exer*ises.

DONT ..
Mo#e aroun% in t"e lab %uring t"e lab session. <am!er System 3iles or <ry to a**ess t"e Ser#er. 2rite :ata S"eets or 8e*or%s in t"e Lab. C"ange t"e system assigne% to you &it"out t"e noti*e o' t"e Lab Sta''. <ea*"ing your 'rien%s.

:e!t.o' MCA( 8;C

Unix Programming Laboratory Manual

I Semester MCA 2013-14

I .r*!'1.&* .* UNIX F#a.'r#" *: UNIX


Multiuser system Multi-tas+ing system <"e U7II tool+it Pattern mat*"ing Programming 'a*ility %o*umentation

U &6 C*,,a ! ".r'1.'r#


A U7II *omman% is an a*tion re,uest gi#en to t"e U7II s"ell 'or exe*ution. All U7II *omman%s a!!ly an a*tion or a series o' a*tions to some in!ut %ata an% *reate some out!ut %ata( an% are *ase sensiti#e. <"e *omman% 'ormat is J =#r- >*8.&* "? >ar+',# ."? &"ere =#r- is t"e *omman% name *8.&* " mo%i'y "o& t"e a*tion is a!!lie% an% ar+',# ." !ro#i%e a%%itional in'ormation to t"e *omman% <"ere are a 'e& *omman%s 5li+e 8%! 6 t"at %onKt ta+e any arguments( some *omman%s 5li+e %h*@ 7" 6 t"at may or may not ta+e any arguments an% some *omman%s 5li+e 1'. 6 t"at *om!ulsorily ta+e arguments.

C*,,a !" .* E6#1'.# UNIX Pr*+ra,"


%itor ) ;im or ;i <o o!en a 'ile ) #im 'ilename.s" or #i 'ilename.s" <o xe*ute S"ell S*ri!t ) s" 'ilename.s" <o xe*ute an A29 S*ri!t ) a&+ L' 'ilename.a&+

:e!t.o' MCA( 8;C

Unix Programming Laboratory Manual

I Semester MCA 2013-14

SOLUTIONS

:e!t.o' MCA( 8;C

10

Unix Programming Laboratory Manual A. E687*r# .h# U &6 # =&r* ,# .

I Semester MCA 2013-14

<"e tra%itional Unix en#ironment is a CLI 5*omman% line inter'a*e6( &"ere t"e user *an ty!e *omman%s to tell t"e *om!uter &"at to %o. Un%er Ubuntu t"ere are @UIs 5gra!"i*al user inter'a*es6( &"ere a user *an !oint an% *li*+ an% %rag. A <erminal is #ery use'ul tool in U7II. T* *8# T#r,& a7@ 8r#"" C.r7AA7.AT * /*'r $#/-*ar!. Alternati#ely a terminal *an be o!ene% 'rom A!!li*ations H A**essories H <erminal( &"i*" is lo*ate% in main menu. Mn*e a terminal is o!ene%( use t"e login *re%entials gi#en by t"e system a%ministrator an% login. Mn*e su**ess'ully logge% in( a *omman% !rom!t a!!ears on t"e terminal. <ry out t"e 'ollo&ing *omman%s an% ta+e a tour to 'amiliariNe U7II en#ironment. 8%!) <"e 8%! *omman% &ill allo& you to +no& in &"i*" %ire*tory youOre lo*ate% 5 8%! stan%s 'or P!rint &or+ing %ire*toryP6. 7") <"e 7" *omman% &ill s"o& you 5OlistO6 t"e 'iles in your *urrent %ire*tory. Use% &it" *ertain o!tions( you *an see siNes o' 'iles( &"en 'iles &ere ma%e( an% !ermissions o' 'iles. 1!) <"e 1! *omman% &ill allo& you to *"ange %ire*tories. 2"en you o!en a terminal you &ill be in your "ome %ire*tory. <o mo#e aroun% t"e 'ile system you &ill use 1!. xam!les)

<o na#igate into t"e root %ire*tory( use B1! 4B <o na#igate to your "ome %ire*tory( use B1!B or B1! CB <o na#igate u! one %ire*tory le#el( use B1! ..B <o na#igate to t"e !re#ious %ire*tory 5or ba*+6( use B1! (B

1a.) *on*atenates 'iles. 1a. D :&7# a,# &ill *reate a 'ile an% &"ate#er is ty!e% later is treate% as *ontents o' t"e 'ile. Use 1.r7(! to exit t"e 'ile. 1a. :&7# a,# &ill %is!lay t"e *ontents o' t"e 'ile. 18) <"e 18 *omman% &ill ma+e a *o!y o' a 'ile. xam!le) B18 :&7# :**B &ill ma+e an exa*t *o!y o' P'ileP an% name it P'ooP( but t"e 'ile P'ileP &ill still be t"ere. ,=) <"e ,= *omman% &ill mo#e a 'ile to a %i''erent lo*ation or &ill rename a 'ile. xam!les are as 'ollo&s) B,= :&7# :**B &ill rename t"e 'ile P'ileP to P'ooP. <"e 'ile ?'ileA&ill no longer be a#ailable. r,) Use t"is *omman% to remo#e or %elete a 'ile in your %ire*tory. r,!&r) <"e r,!&r *omman% &ill %elete an empty %ire*tory. <o %elete a %ire*tory an% all o' its *ontents re*ursi#ely(

:e!t.o' MCA( 8;C

11

Unix Programming Laboratory Manual

I Semester MCA 2013-14

,$!&r) <"e ,$!&r *omman% &ill allo& you to *reate %ire*tories. xam!le) B,$!&r ,'"&1B &ill *reate a %ire*tory *alle% Pmusi*P. ,a ) <"e ,a *omman% is use% to s"o& you t"e manual o' ot"er *omman%s. <ry B,a ,a B to get t"e man !age 'or ,a itsel'. %h* L %is!lays &"o all are t"e logge% in users. <ry t"e #ariants %h*a,& an% %h* a, & !a.# --- s"o&s t"e *urrent %ate an% time 1a7 --- s"o&s a *alen%ar o' t"e *urrent mont". Use e.g.( 1a7 10 2013 to get t"e *alen%ar 'or M*tober 2013( or 1a7 2013 to get t"e &"ole year.
B. E687*r# =& #!&.*r %&.h =&, .'.*r. P#r:*r, .h# :*77*%& + *8#ra.&* " '"& + =& #!&.*r@ -'. *. 7&,&.#! .*2 1. & "#r. 1hara1.#r@ !#7#.# 1hara1.#r@ r#87a1# 1hara1.#r 2. Sa=# .h# :&7# a ! 1* .& '# %*r$& + 3. "a=# .h# :&7# a ! #6&. .h# #!&.*r ). 9'&. .h# #!&.*r 3. 9'&. %&.h*'. "a=& + .h# :&7# E. r# a,# a :&7# 7. & "#r. 7& #"@ !#7#.# 7& #"@ F. "#. 7& # ',-#r" G. "#ar1h :*r a 8a..#r 10. ,*=# :*r%ar! a ! -a1$%ar!

U7II !ro#i%es #i e%itor 'or $S: system L *reate% by $ill Qoy. $ram Moolenaar im!ro#e% t"e #i e%itor an% *alle% it as #im 5#i im!ro#e%6 on Linux MS. <"ere are 3 mo%es in #i e%itor C*,,a ! ,*!# ) *o!y an% %elete text( *anKt enter or re!la*e text I 8'. ,*!# #6 ,*!# )e#ery +ey !resse% is a text ) sa#e an% !er'orm substitution by !ressing ) an% get ba*+ to %e'ault *omman% mo%e

It is !ossible to %is!lay t"e mo%e in &"i*" is user is in by ty!ing( )set s"o&mo%e Messages li+e I7S 8< MM: ( 8 PLAC MM: ( CDA7@ MM: ( et* &ill a!!ear in t"e last line.
Insert mo%e *omman%s

3U7C<IM7 inserts text

i :e!t.o' MCA( 8;C

12

Unix Programming Laboratory Manual a I A o M r s S % x %% yy .%% a!!en%s text inserts at beginning o' line a!!en%s text at en% o' line o!ens line belo& o!ens line abo#e re!la*es a single *"ara*ter re!la*es &it" a text re!la*es entire line %elete %eletes a single *"ara*ter %elete entire line *o!y entire line %eletes t"e *urrent line an% 'i#e lines belo&

I Semester MCA 2013-14

E6(,*!# 1*,,a !"2 Sa=& + .#6. a ! 9'&..& + )2 )x )&, )& <filename> )&R <filename> ), ),R )s" )re*o#er Na=&+a.&* A *omman% mo%e *omman% %oesnKt s"o& u! on s*reen but sim!ly !er'orms a 'un*tion. <o mo#e t"e *ursor in 'our %ire*tions( + S " l mo#es *ursor u! mo#es *ursor %o&n mo#es *ursor le't mo#es *ursor rig"t sa#es 'ile an% remains in e%iting mo%e sa#es an% ,uits e%iting mo%e sa#es an% ,uits e%iting mo%e sa#e as sa#e as( but o#er&rites existing 'ile ,uits e%iting mo%e ,uits e%iting mo%e by reSe*ting *"anges ma%e es*a!es to U7II s"ell re*o#ers 'ile 'rom a *ras"

S#ar1h& + :*r a Pa..#r 4 T sear*" 'or&ar% sear*" ba*+&ar% 13

:e!t.o' MCA( 8;C

Unix Programming Laboratory Manual S1r*77& + *trl-' s*rolls 'or&ar% *trl-b s*rolls ba*+&ar% *trl-% s*rolls "al' !age 'or&ar% *trl-u s*rolls "al' !age ba*+&ar% S'-".&.'.&* "#ar1h a ! r#87a1# )a%%ress4sour*eU!attern4targetU!attern4'lags xam!les) )1(Js4%ire*tor4member4g )1(-0s4unsigne%44g )3(10s4%ire*tor4member4g ).s4%ire*tor4member4g )Js4%ire*tor4member4g L& # ',-#r" )set number *an also use F instea% o' 1(J %eletes unsigne% e#ery&"ere in lines 1 to -0 substitute lines 3 t"roug" 10 only t"e *urrent line only t"e last line s"o& line numbers

I Semester MCA 2013-14

((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((
1aH 5r&.# a "h#77 "1r&8. .ha. .a$#" a =a7&! !&r#1.*r/ a,# a" a ar+',# . a ! r#1'r"&=#7/ !#"1# ! a77 .h# "'-!&r#1.*r&#"@ :& !" .h# ,a6&,', 7# +.h *: a / :&7# & .ha. h&#rar1h/ a ! %r&.#" .h&" ,a6&,', =a7'# .* .h# ".a !ar! *'.8'.

*lear i' V JW -ne 1 X t"en e*"o ?In#ali% 7umber o' arguments !asse%A exit 'i *% J1 e*"o <"e %ire*tory name is J1 set -- Y ls -l8Z gre! -# P[%P Zsort -+ - -rnY e*"o PsiNe o' t"e largest 'ile J1 is J- blo*+sP R' 2 s" 1a.s" unixUlabU!rogs <"e %ire*tory name is unixUlabU!rogs siNe o' t"e largest 'ile exam!le1.s" is 2.. blo*+s D#"1r&8.&* IJ stan%s 'or list o' all t"e *omman% line arguments( 5similar to ar+=> ? in C6 IK 'or t"e number o' arguments !asse% along &"ile running t"e !rogram 5 similar to ar+1 in C6

:e!t.o' MCA( 8;C

14

Unix Programming Laboratory Manual

I Semester MCA 2013-14

S*r. 2 sorts t"e lines o' t"e s!e*i'ie% 'iles( ty!i*ally in al!"abeti*al or%er. Its syntax is) F sort VCo!tionsHX VC'iel%-s!e*i'ierHX VC'ilename5s6HX 1! 5*"ange V*urrent &or+ingX %ire*tory6 1! L8a.hD ) *"anges your *urrent &or+ing %ire*tory to !at" 5&"i*" *an be an absolute or a relati#e !at"6. Mne o' t"e most *ommon relati#e !at"s to use is O..O i.e. t"e !arent %ire*tory o' t"e *urrent %ire*tory. 1! resets your *urrent &or+ing %ire*tory to users "ome %ire*tory 5use'ul i' you are lost an% &ant to rea*" "ome %ire*tory 6. 1-H 5r&.# a "h#77 "1r&8. .ha. a11#8." a 8a.h a,# a ! 1r#a.#" a77 .h# 1*,8* # ." & .ha. 8a.h a,# a" !&r#1.*r&#". F*r #6a,87#@ &: .h# "1r&8. &" a,#! ,81@ .h# .h# 1*,,a ! ,81 a4-414! "h*'7! 1r#a.# !&r#1.*r&#" a@ a4-@ a4-41@ a4-414!. tem!EJI3S I3SE4 'or i in J= %o i' V -% Ji X \ t"en *% Ji else m+%ir Ji *% Ji 'i %one I3SEJtem! %EY!&%Y e*"o ?<"e *urrent %ire*tory stru*ture *reate% is J%A R' 2 s" 1b.s" a4b4*4% <"e *urrent %ire*tory stru*ture *reate% is 4"ome4abm4unixUlabU!rogs4a4b4*4% D#"1r&8.&* ,$!&r Mma+e %ire*tory6 m+%ir directory *reates a sub%ire*tory *alle% directory in t"e *urrent &or+ing %ire*tory. ]ou *an only *reate sub%ire*tories in a %ire*tory i' you "a#e &rite !ermission on t"at %ire*tory. 8%! ) :is!lays *urrent &or+ing %ire*tory IFS or internal 'iel% se!arator is a #ariable. <"e s"ell uses t"e #alue store% in I3S( &"i*" is t"e s!a*e( tab( an% ne&line *"ara*ters by %e'ault( to %elimit &or%s 'or t"e rea% an% set *omman%s( &"en !arsing :e!t.o' MCA( 8;C 1-

Unix Programming Laboratory Manual

I Semester MCA 2013-14

out!ut 'rom *omman% substitution( an% &"en !er'orming #ariable substitution. I3S *an be re%e'ine% to !arse one or more lines o' %ata &"ose 'iel%s are not %elimite% by t"e %e'ault &"ite-s!a*e *"ara*ters -----------------------------------------------------------------------------------------------------------------------------2aH 5r&.# a "h#77 "1r&8. .ha. a11#8." .%* :&7# a,#" a" ar+',# ."@ 1h#1$" &: .h# 8#r,&""&* " :*r .h#"# :&7#" ar# &!# .&1a7 a ! &: .h# 8#r,&""&* " ar# &!# .&1a7@ *'.8'. 1*,,* 8#r,&""&* " a ! *.h#r%&"# *'.8'. #a1h :&7# a,# :*77*%#! -/ &." 8#r,&""&* ". i' V JW -e, 0 X \ t"en e*"o ?7o arguments entere%A exit eli' V JW -lt 2 X \ t"en e*"o ? nter 2 'ilenames to *om!areA exit else '1EYls -l J1Z*ut -* 2-10Y '2EYls -l J2Z*ut -* 2-10Y i' V PJ'1P EE PJ'2P X \ t"en e*"o ?3ile !ermission are i%enti*alA e*"o J'1 else e*"o ?3ile !ermission are not i%enti*alA e*"o ?<"e !ermission o' 'irst 'ile J1 is ) J'1^ e*"o ?<"e !ermission o' t"e se*on% 'ile J2 is ) J'2^ 'i 'i R' 2 s" 2a.s" 1a.s" 1b.s" !ermissions are i%enti*al r&-r--r_ s" 2a.s" 1a.s" sam!le.s" 3ile !ermission are not i%enti*al <"e !ermission o' 'irst 'ile 1a.s" is ) r&-r&-rL <"e !ermission o' t"e se*on% 'ile sam!le.s" is ) r&xr&-rL D#"1r&8.&* &:(.h# (#7"# <"e syntax o' t"e i'-t"en-else *onstru*t is i' V ex!ression X t"en :e!t.o' MCA( 8;C 1.

Unix Programming Laboratory Manual sim!le-*omman% 'i *r i' V ex!ression X t"en *omman%list-1 else *omman%list-2 'i

I Semester MCA 2013-14

<"e ex!ression &ill be e#aluate% an% a**or%ing to its #alue( t"e commandlist-1 or t"e commandlist-2 &ill be exe*ute%. 7" *omman% &it" o!tion Ll %is!lays all 'ile attributes. 1'. *omman% &it" L* o!tion extra*ts *"ara*ter( range o' *"ara*ters mentione% in t"e argument as !osition o' *"ara*ters to *ut *omman%. 2-H 5r&.# a "h#77 "1r&8. %h&1h a11#8." =a7&! 7*+(& a,#" a" ar+',# ." a ! 8r& ." .h#&r 1*rr#"8* !& + h*,# !&r#1.*r&#"@ &: * ar+',# ." ar# "8#1&:&#!@ 8r& . a "'&.a-7# #rr*r ,#""a+#. i' V JW -e, 0 X \ t"en e*"o ?7o argumentsA exit 'i 'or nam in J= %o yEYgre! PJnamP 4et*4!ass&% Z *ut -% P)P -'1Y i' V -n ?JyA X t"en i' V PJyP E PJnamP X t"en xEY gre! PJnamP 4et*4!ass&% Z *ut -% P)P -'.Y e*"o P"ome %ire*tory o' Jnam is JxP else e*"o PJnam %oesnOt "a#e an a**ount P 'i 'i %one R' ) s" 2b.s" m*a131 "ome %ire*tory o' m*a101 is 4"ome4m*a101

:e!t.o' MCA( 8;C

1/

Unix Programming Laboratory Manual s" 2b.s" !roSe*t1 !roSe*t1 %oes not "a#e an a**ount

I Semester MCA 2013-14

D#"1r&8.&* ) +r#8 2 <"is *omman% is use% to sear*"( sele*t an% !rint s!e*i'ie% re*or%s or lines 'rom an in!ut 'ile gre! V o!tions X !attern V 'ilename1 X V 'ilename2X` :*r 7**8" 2 use% to loo! t"roug" a list o' 'iles( exe*uting some *omman%s on ea*" 'ile. Syntax is 'or variable in list %o statements 5re'erring to Jvariable6 %one <"e 4#.148a""%! *ontains one entry !er line 'or ea*" user 5or user a**ount6 o' t"e system. All 'iel%s are se!arate% by a *olon 5)6 symbol. @enerally( !ass&% 'ile entry loo+s as 'ollo&s &"ere t"e sixt" 'iel% s"o&s t"e users "ome %ire*tory.

1. 2.

<"e 'irst 'iel% is t"e user name( i.e. t"e string a user &oul% ty!e in &"en logging into t"e o!erating system) t"e 7*+ a,#. a*" re*or% in t"e 'ile must "a#e a uni,ue user name 'iel%. <"e se*on% 'iel% stores in'ormation use% to #ali%ate a userOs 8a""%*r!\ "o&e#er in most mo%ern uses t"is 'iel% is usually set to PxP 5or some ot"er in%i*ator6 &it" t"e a*tual !ass&or% in'ormation being store% in a se!arate s"a%o& !ass&or% 'ile. Setting t"is 'iel% to an asteris+ P=P is t"e ty!i*al &ay to %ea*ti#ate an a**ount to !re#ent it being use%. <"e t"ir% 'iel% is t"e '"#r &!# .&:&#r( t"e number t"at t"e o!erating system uses 'or internal !ur!oses. It %oes not "a#e to be uni,ue. <"e 'ourt" 'iel% is t"e +r*'8 &!# .&:&#r. <"is number i%enti'ies t"e !rimary grou! o' t"e user\ all 'iles t"at are *reate% by t"is user may initially be a**essible to t"is grou!. <"e 'i't" 'iel%( *alle% t"e @e*os 'iel%( is *ommentary t"at %es*ribes t"e !erson or a**ount. <y!i*ally( t"is is a set o' *omma-se!arate% #alues in*lu%ing t"e userOs 'ull name an% *onta*t %etails. <"e sixt" 'iel% is t"e !at" to t"e userOs "ome %ire*tory. <"e se#ent" 'iel% is t"e !rogram t"at is starte% e#ery time t"e user logs into t"e system. 3or an intera*ti#e user( t"is is usually one o' t"e systemOs *omman% line inter!reters or s"ell :e!t.o' MCA( 8;C 10

3. 4. -. .. /.

Unix Programming Laboratory Manual

I Semester MCA 2013-14

3a. Cr#a.# a "1r&8. :&7# 1a77#! :&7#(8r*8#r.&#" .ha. r#a!" a :&7# a,# # .#r#! a ! *'.8'." &. 8r*8#r.&#". e*"o ? nter a 'ilenameN rea% 'ile i' V -' J'ile X t"en set -- Yls -l J'ileY e*"o name o' 'ile J1 e*"o 'ile !ermission J1 e*"o number o' lin+ J2 e*"o user name J3 e*"o o&ner name J4 e*"o blo*+ siNe Je*"o %ate o' mo%i'i*ation J. e*"o time o' mo%i'i*ation J/ else e*"o <"e 'ile J'ile %oes not exist 'i R' ) enter a 'ilename 1a.s" name o' 'ile 1a.s" 'ile !ermission -r&-r--r-number o' lin+s 1 user name abm o&ner name abm blo*+ siNe -01 %ate o' mo%i'i*ation 2013-0.-20 time o' mo%i'i*ation 01)34 enter a 'ilename *"e*+'ile <"e 'ile *"e*+'ile %oes not exist D#"1r&8.&* &: > (: I:&7# ? -' o!tion is use% *"e*+ &"et"er t"e 'ile exists in t"e *urrent %ire*tory or not. "#. (( O7" (7 I:&7#O ) <"is syntax set t"e 'ile attributes as !ositional !arameters. V--X s!e*i'ies not *"ange any o' t"e 'lags\ use'ul in setting J1 to -. J1 to J0 are !ositional !arameters.

:e!t.o' MCA( 8;C

11

Unix Programming Laboratory Manual

I Semester MCA 2013-14

3-.5r&.# "h#77 "1r&8. .* &,87#,# . .#r,& a7 7*1$& + M"&,&7ar .* .h# 7*1$ 1*,,a !H. I. "h*'7! 8r*,8. .h# '"#r :*r a 8a""%*r!. A:.#r a11#8.& + .h# 8a""%*r! # .#r#! -/ .h# '"#r@ &. ,'". 8r*,8. a+a& :*r .h# ,a.1h& + 8a""%*r! a" 1* :&r,a.&* a ! &: ,a.1h *11'r"@ &. ,'". 7*1$ .h# $#/%*r! ' .&7 a ,a.1h& + 8a""%*r! &" # .#r#! a+a& -/ .h# '"#r@ N*.# .ha. .h# "1r&8. ,'". -# %r&..# .* !&"r#+ar! BREAP@ 1* .r*7(D. N* .&,# 7&,&. ##! -# &,87#,# .#! :*r .h# 7*1$ !'ra.&* . *lear stty -e*"o e*"o P nter !ass&or% to lo*+ t"e terminalP rea% !ass1 e*"o P 8e-enter t"e !ass&or%P rea% !ass2 i' V PJ!ass1P E PJ!ass2P X t"en e*"o P<erminal is lo*+e%P e*"o P nter !ass&or% to unlo*+P tra! ? A1 2 3 1 1- 10 &"ile true %o rea% !ass3 i' V ?J!ass1A E ?J!ass3A X t"en e*"o P<erminal unlo*+e%P stty e*"o exit else e*"o PPass&or% Mismat*"..<ry AgainP 'i %one else e*"o PPass&or% Mismat*"P stty e*"o 'i R' 2 nter !ass&or% to lo*+ t"e terminal)==== 8e-enter t"e !ass&or%)==== <erminal is lo*+e% nter t"e !ass&or% to unlo*+)==== <erminal unlo*+e% nter !ass&or% to lo*+ t"e terminal)======== 8e-enter t"e !ass&or%)==== Pass&or% Mismat*"

:e!t.o' MCA( 8;C

20

Unix Programming Laboratory Manual

I Semester MCA 2013-14

D#"1r&8.&* S../ L*"anges t"e setting o' terminals as s!e*i'ie% as argument to it S../ #1h* ma+es t"e *"ara*ters ty!e% in#isible on t"e s*reen <"e .ra8 *omman% !ro#i%es t"e s*ri!t to *a!tures an interru!t 5signal6 an% t"en *lean it u! &it"in t"e s*ri!t. <"e syntax is as 'ollo&s tra! arg signal tra! Oa*tionO signal1 signal2 signal7 x) tra! Oa*tionO SI@I7< tra! Oa*tionO SI@< 8M SI@I7< SI@3P SI@S<P tra! Oa*tionO 1- 2 0 20 -----------------------------------------------------------------------------------------------------------------------------)a. 5r&.# a "h#77 "1r&8. .ha. a11#8. * # *r ,*r# :&7# a,#" a" ar+',# . a ! 1* =#r. a77 *: .h#, .* '88#r1a"#@ 8r*=&!#! .h#/ #6&". & 1'rr# . !&r#1.*r/. *lear i' V JW -e, 0 X t"en e*"o ? rror) Insu''i*ient arguments`A exit 'i 'or i in J= %o i' V -' Ji X t"en e*"o it is a #ali% 'ile e*"o Contents o' 'ile be'ore *on#erting *at Ji e*"o Contents o' 'ile a'ter *on#erting tr OVa-NXO OVA-aXO C Ji Z tee Ji +EYls Ji Z tr OVa-NXO OVA-aXOY m# Ji J+ e*"o 'ile Ji rename% as J+ e*"o ? %ire*tory listingA ls else e*"o 'ile %oes not exist 'i %one RUN2 s" 4a.s" rror) Insu''i*ient arguments` S" 4a.s" %irname It is a #ali% 'ile :e!t.o' MCA( 8;C 21

Unix Programming Laboratory Manual Contents o' 'ile be'ore *on#erting <"is is lab !rogram Contents o' 'ile a'ter *on#erting <DIS IS LA$ P8M@8AM %irname is rename% as :I87AM :ire*tory listing :I87AM 1a.s" D#"1r&8.&*

I Semester MCA 2013-14

.r 1*,,a ! 2 <"e tr 'ilter mani!ulates in%i#i%ual *"ara*ters in a line. It translates *"ara*ters using one or t&o *om!a*t ex!ressions .r options expression1expression2 standard input <"is *omman% translates ea*" *"ara*ter in expression1 to its *ounter!art in ex!ression2. ,= 1*,,a ! 2 <"e m# *omman% is use% to mo#e or rename 'iles an% %ire*tories. <"is *omman% ta+es a minimum o' 2 arguments )-. 5r&.# a "h#77 "1r&8. .ha. !&"87a/" a77 .h# 7& $" .* a :&7# "8#1&:&#! a" .h# :&r". ar+',# . .* .h# "1r&8.. Th# "#1* ! ar+',# .@ %h&1h &" *8.&* a7@ 1a -# '"#! .* "8#1&:/ & %h&1h .h# "#ar1h &" .* -#+& . I: .h&" "#1* ! ar+',# . &" *. 8r#"# .@ .h# "#ar1h &" .* -#+& & 1'rr# . %*r$& + !&r#1.*r/. I #&.h#r 1a"#@ .h# ".ar.& + !&r#1.*r/ a" %#77 a" a77 &." "'-!&r#1.*r&#" a. a77 7#=#7" ,'". -# "#ar1h#!. Th# "1r&8. ##! *. & 17'!# a / #rr*r 1h#1$& +. i' V JW -e, 1 X t"en tmEY!&%Y e*"o P:ire*tory not gi#en. Using P2:) JtmP else tmEJ2 e*"o Psear*"ing 'rom t"e gi#en %ire*tory) JtmP 'i t1EYls -ali8 Z gre! PJ1P Z *ut -% P P -'1 Z "ea% -1Y e*"o Pt"e lin+e% 'iles areP ls -liar8 Jtm Z gre! PJt1P R' 2 s" 4b.s" test sam!le Sear*"ing 'rom t"e gi#en %ire*tory) sam!le <"e lin+e% 'iles are 1/1230 -r&xr--r-- 4 abm m*a 11 Qul 23 12)3- lin+e%3

:e!t.o' MCA( 8;C

22

Unix Programming Laboratory Manual ln lin+e%3 lin+e%4 s" 4b.s" test sam!le Sear*"ing 'rom t"e gi#en %ire*tory) sam!le <"e lin+e% 'iles are 1/1230 -r&xr--r-- 4 abm m*a 11 Qul 23 12)3- lin+e%3 1/1230 -r&xr--r-- 4 abm m*a 11 Qul 23 12)3- lin+e%4 s" 4b.s" test :ire*tory not gi#en. Using P2:) 4"ome4abm4unixUlabU!rogs t"e lin+e% 'iles are 1/1230 -r&xr--r-- 3 abm 'a*ulty 11 Qul 23 12)3- test 1/1230 -r&xr--r-- 3 abm 'a*ulty 11 Qul 23 12)3- lin+e%3 1/1230 -r&xr--r-- 3 abm 'a*ulty 11 Qul 23 12)3- lin+e%4 D#"1r&8.&* 7" 1*,,a ! ls lists t"e *ontents o' a %ire*tory.

I Semester MCA 2013-14

+r#82 <"e Unix gre! *omman% "el!s you sear*" 'or strings in a 'ile. <"e +r#8 'ilter sear*"es t"e *ontents o' one or more 'iles 'or a !attern an% %is!lays only t"ose lines mat*"ing t"at !attern 7 ) ln *omman% *reates a "ar% lin+ to t"e 'ile. It is same as s"ort*ut to t"e 'ile. ---------------------------------------------------------------------------------------------------------------------------3a. 5r&.# a "h#77 "1r&8. .ha. a11#8." :&7# a,#" a" ar+',# ." a ! !&"87a/" &." 1r#a.&* .&,# &: .h# :&7# #6&"." a ! &: .h# :&7# !*#" *. #6&"."@ "# ! *'.8'. #rr*r ,#""a+#. e*"o nter t"e 'ilename to 'in% t"e *reation time) rea% 'ilename i' V -' J'ilename X t"en set -- Yls -l J'ilenameY e*"o 'ile J'ilename &as *reate% on J. J/ J0 else e*"o 'ile J'ilename %oes not exist in t"e *urrent %ire*tory 'i R' 2 s" -a.s" nter t"e 'ilename to 'in% t"e *reation time) :e!t.o' MCA( 8;C 23

Unix Programming Laboratory Manual test 'ile test &as *reate% on Qul 23 12)3s" -a.s" nter t"e 'ilename to 'in% t"e *reation time) *"e*+ 'ile *"e*+ %oes not exist in t"e *urrent %ire*tory D#"1r&8.&*

I Semester MCA 2013-14

3ile tests ) 'ile tests are *on%u*te% 'or *"e*+ing t"e status o' 'iles an% %ire*tories i' V -' 'ilename X <"is *on%ition returns true #alue i' 'ile exists an% is a regular 'ile i' V -% 'ilename X <"is *on%ition returns true #alue i' 'ile exists an% is a %ire*tory 'ile 3-. 5r&.# a "h#77 "1r&8. .* !&"87a/ .h# 1a7# !ar :*r 1'rr# . ,* .h %&.h 1'rr# . !a.# r#87a1#! -/ J *r JJ !#8# !& + * %h#.h#r .h# !a.# ha" * # !&+&. *r .%* !&+&.". set Y%ateY i' V J3 -le 1 X t"en nEY*al Z tail -n b3 Z gre! -n PJ3PZ *ut -% P)P -'1 Z "ea% -n 1Y nEYex!r Jn b 2Y *al Z se% PJn s4J34=4P else *al Z se% Ps4J34==4P 'i R#"'7. 2 2 s" -b.s" Quly 2013
Su Mo <u 2e <" 3r Sa

1 2 3 4 - . / 0 1 10 11 12 13 14 1- 1. 1/ 10 11 20 21 22 == 24 2- 2. 2/ 20 21 30 31 D#"1r&8.&* !a.# S"o&s *urrent %ate an% time. :e!t.o' MCA( 8;C 24

Unix Programming Laboratory Manual

I Semester MCA 2013-14

"#!) a non-intera*ti#e text 'ile e%itor. It re*ei#es text in!ut( &"et"er 'rom stdin or 'rom a 'ile( !er'orms *ertain o!erations on s!e*i'ie% lines o' t"e in!ut( one line at a time( t"en out!uts t"e result to stdout or to a 'ile. Se% %etermines &"i*" lines o' its in!ut t"at it &ill o!erate on 'rom t"e address range !asse% to it. S!e*i'y t"is a%%ress range eit"er by line number or by a !attern to mat*" Ea. 5r&.# a "h#77 "1r&8. .* :& ! a :&7#4" .ha. ,a.1h#" a 8a..#r +&=# a" 1*,,a ! 7& # ar+',# . & .h# h*,# !&r#1.*r/@ !&"87a/ .h# 1* .# ." *: .h# :&7# a ! 1*8/ .h# :&7# & .* .h# !&r#1.*r/ C4,/!&r. i' V JW -ne 1 X t"en e*"o Insu''i*ient Arguments exit 'i %irEmy%ir i' V -% J%ir X t"en e*"o P%ire*tory J%ir alrea%y exists.. 7ot *reating againP else m+%ir J%ir 'i e*"o <"e 'iles mat*"ing t"e !attern J1 are) ls =J1= H 'ilenames.txt *at 'ilenames.txt e*"o <"e *ontents o' t"e 'iles are 'or i in Y*at 'ilenames.txtY %o e*"o P'ilename) JiP e*"o ?-----------------------------------------? *at Ji e*"o P---------------------------------------------------------------------------------------------P *! Ji .4my%ir4Ji %one e*"o P<"e 'iles *o!ie% into >4my%irP *% my%ir ls -l RUN2 s" .a.s" est <"e 'iles mat*"ing t"e !attern est are) test :e!t.o' MCA( 8;C 2-

Unix Programming Laboratory Manual test.s" ?<"e *ontents o' t"e 'iles areA ?'ilename) testA Dello.. &el*ome to unix 'ilename) test.s" e*"o ?t"is is sam!le !rogram 'or .a.s"A *lear ?<"e 'iles *o!ie% into 4"ome4abm4my%irA total 2 -r&-r--r-- 1 m*a 7one -4- Qul 22 11)-. test -r&-r--r-- 1 m*a 7one 1.0 Qul 22 11)-. test.s"

I Semester MCA 2013-14

E-. 5r&.# a "h#77 "1r&8. .* 7&". a77 .h# :&7#" & a !&r#1.*r/ %h*"# :&7# a,# &" a. 7#a". 10 1hara1.#r". MU"# #68r 1*,,a ! .* 1h#1$ .h# 7# +.hH. *lear 'or i in YlsY %o lenEYex!r PJiP ) O.=OY i' V Jlen -ge 10 X t"en e*"o Ji HH out'ile 'i %one e*"o ?<"e 'ile names "a#ing more t"an 10 *"ara*ters are ? *at out'ile R' 2 tou*" %e!artments r#*em*a%e!t s" .b.s" <"e 'ile names "a#ing more t"an 10 *"ara*ters are %e!artments r#*em*a%e!t D#"1r&8.&* #68r 2 <"is *omman% *an be use% to !er'orm string mani!ulations li+e to 'in% lengt" o' string. syntax to 'in% lengt" o' string) ex!r lengt" Jstringname ------------------------------------------------------------------------------------------------------------------------------

:e!t.o' MCA( 8;C

2.

Unix Programming Laboratory Manual

I Semester MCA 2013-14

7a. 5r&.# a "h#77 "1r&8. .ha. +#." #6#1'.#! a ! !&"87a/" .h# ,#""a+# #&.h#r QG**! M*r & +N *r QG**! A:.#r ** N *r QG**! E=# & +N !#8# !& + '8* .&,# a. %h&1h .h# '"#r 7*+" & . "ourno&EY%ate Z *ut L* 12-13Y userEYe*"o JDMM Z *ut L% A4A L' 4Y *ase J"ourno& in V0-1XV0-1XZ0V2-1X6 e*"o ?@oo% Morning Mr4Ms ) JuserA\\ 1V2--X6e*"o ?@oo% A'ternoon Mr4Ms )JuserA\\ 1V.-1X6e*"o ?@oo% #ening Mr4Ms )JuserA\\ esa* R#"'7. 2 2 J s" .bas"U!ro'ile goo% morning abm D#"1r&8.&* 2 *ase *omman% ) <"is *omman% !ro#i%es multi &ay %e*ision ma+ing 'a*ility.It &or+s on !attern mat*"ing.<"e general 'ormat is *ase string value in pattern16 *omman% *omman% ----------*omman% \\ pattern 26 *omman% *omman% ----------*omman% \\ --------pattern 76 *omman% *omman% ----------*omman% \\ esac 2"en s"ell *omes a*ross a case *onstru*t( t"e be"a#iour o' *ontrol 'lo& &ill be as 'ollo&s.<"e string value t"at a!!ears imme%iately a'ter t"e +ey&or% *ase is *om!are% in turn against ea*" pattern . As soon as a mat*" is 'oun%( all t"e *omman%s 'ollo&ing t"e !attern till t"e imme%iate next %ouble semi *olon5\\6 are exe*ute% an% t"en t"e *ontrol goes beyon% t"e esac

:e!t.o' MCA( 8;C

2/

Unix Programming Laboratory Manual

I Semester MCA 2013-14

7-. 5r&.# a "h#77 "1r&8. .ha. a11#8. a 7&". *: :&7# a,#" a" &." ar+',# .@ 1*' . a ! r#8*r. *11'rr# 1# *: #a1h %*r! .ha. &" 8r#"# . & .h# :&r". ar+',# . :&7# * *.h#r ar+',# . :&7#". i' V JW -lt 2 X t"en e*"o P rror ) Insu''i*ient number o' argumentsP exit 'i strEY*at J1 Z tr OBnO O OY 'or i in J= %o e*"o PIn Ji 'ile t"e *ount is......P 'or a in Jstr %o e*"o P2or% E Ja( Count E Ygre! -* PJaP JiYP %one e*"o P----------------------------------------------------------------P %one R' 2 *at H test1 tiger lion ele!"ant *at H test2 tiger <I@ 8 !ea*o*+ *at H test3 tiger lion tiger 'ox s" /b.s" test1 test2 test3 In test1 'ile t"e *ount is...... 2or% E tiger( Count E 1 2or% E lion( Count E 1 2or% E ele!"ant( Count E 1 ---------------------------------------------------------------In test2 'ile t"e *ount is...... 2or% E tiger( Count E 1 2or% E lion( Count E 0 2or% E ele!"ant( Count E 0 ---------------------------------------------------------------In test3 'ile t"e *ount is...... 2or% E tiger( Count E 2 :e!t.o' MCA( 8;C 20

Unix Programming Laboratory Manual 2or% E lion( Count E 1 2or% E ele!"ant( Count E 0 s" /b.s" test1 rror ) Insu''i*ient number o' arguments

I Semester MCA 2013-14

-------------------------------------------------------------------------------------------------------------------------Fa. 5r&.# a "h#77 "1r&8. .ha. !#.#r,& # .h# 8#r&*! :*r %h&1h a "8#1&:&#! '"#r &" %*r$& + * "/".#, a ! !&"87a/ a88r*8r&a.# ,#""a+#. e*"o P nter t"e Login 7ame o' a UserP rea% user *ountEY&"o Z gre! -&o PJuserP Z &* -lY i' V J*ount -e, 0 X t"en e*"o Pin#ali% userP exit 'i i' V J*ount -gt 1 X t"en e*"o PMulti!le LoginP exit 'i ltEY&"o Ztr -s P P Z gre! PJuserP Z *ut -% P P -'4Y l"EYe*"o Jlt Z *ut -% P)P -'1Y lmEYe*"o Jlt Z *ut -% P)P -'2Y *"EY%ate bFDY *mEY%ate bFMY i' V Jl" E J*" X t"en "rE0 i' V J*m -gt Jlm X t"en minEYex!r J*m - Jlm Y else minEYex!r J*m - Jlm b .0Y 'i e*"o P <"e user is logge% in 'rom J"r "our Jmin minutesP exit 'i

:e!t.o' MCA( 8;C

21

Unix Programming Laboratory Manual i' V J*m -gt Jlm X t"en minEYex!r J*m - Jlm Y "rEYex!r J*" - Jl"Y else minEYex!r J*m - Jlm b .0Y "rEYex!r J*" -Jl" - 1Y 'i e*"o P <"e user is logge% in 'rom J"r "our Jmin minutesP R' 2 S" 0a.s" nter t"e Login 7ame o' a User m*a211 <"e user is logge% in 'rom 1 "our 20 minutes S" 0a.s" nter t"e Login 7ame o' a User Ab* In#ali% user

I Semester MCA 2013-14

F-. 5r&.# a "h#77 "1r&8. .ha. r#8*r." .h# 7*++& + & *: a "8#1&:&#! '"#r %&.h& * # ,& '.# a:.#r h#4"h# 7*+ & . Th# "1r&8. a'.*,a.&1a77/ .#r,& a.# &: "8#1&:&#! '"#r !*#" *. 7*+ & !'r& + a "8#1&:&#! 8#r&*! *: .&,#. e*"o nter t"e login name o' t"e user) rea% user timeE0 &"o Z gre! PJuserP H4%e#4null i' V JT -e, 0 X t"en e*"o PJuser "as alrea%y logge% in.. @i#e a %i''erent user nameP exit 'i &"ile V true X %o slee! 1 timeEYex!r Jtime b 1Y &"o Z gre! PJuserP H4%e#4null i' V JT -e, 0 X t"en e*"o PJuser logge% in Jtime se*on%s...P :e!t.o' MCA( 8;C 30

Unix Programming Laboratory Manual exit 'i i' V Jtime -e, .1 X t"en e*"o PJuser %i% not login &it"in 1 minuteP exit 'i %one R' 2 2 nter t"e login name o' t"e user m*a211 m*a211 logge% in 2- se*on%s nter t"e login name o' t"e user) abm abm "as alrea%y logge% in.. @i#e a %i''erent user name nter t"e login name o' t"e user m*a2-0 m*a2-0 %i% not login &it"in 1 minute D#"1r&8.&*

I Semester MCA 2013-14

"7##8 *omman% ) Using t"is *omman% t"e user *an ma+e t"e system slee!( t"at is ( !ause 'or some 'ixe% !erio% o' time. ---------------------------------------------------------------------------------------------------------------------------Ga. 5r&.# a "h#77 "1r&8. .ha. a11#8." .h# :&7# a,#@ ".ar.& + a ! # !& + 7& # ',-#r a" a ar+',# . a ! !&"87a/ a77 .h# 7& #" -#.%## .h# +&=# 7& # ',-#r. i' V JW -ne 3 X t"en e*"o P rror ) In#ali% number o' arguments.P exit 'i i' V J2 -gt J3 X t"en e*"o P rror ) In#ali% range #alue.P exit 'i lEYex!r J3 - J2 b 1Y *at J1 Z tail Ln bJ2 Z "ea% -Jl

:e!t.o' MCA( 8;C

31

Unix Programming Laboratory Manual R' 2 s" 1a.s" test1 - / ab* 1234 %e' -./0 g"i 11011 s" 1a.s" test1 1- / rror ) In#ali% range #alue

I Semester MCA 2013-14

D#"1r&8.&* 2
h#a! 1*,,a ! ) <"is *omman% is use% to %is!lay at t"e beginning o' one or more 'iles. $y %e'ault it %is!lays 'irst 10 lines o' a 'ile. syntax is) head [ count option ] filename

.a&7 1*,,a ! ) <"is *omman% is use% to %is!lay last 'e& lines at t"e en% o' a 'ile. . $y %e'ault it %is!lays last 10 lines o' a 'ile tail V b4- start X 'ilename start is starting line number tail -- 'ilename ) It %is!lays last - lines o' t"e 'ile tail b- 'ilename ) It %is!lays all t"e lines (beginning 'rom line number - to en% o' t"e 'ile

G-. 5r&.# a "h#77 "1r&8. .ha. :*7!" 7* + 7& #" & .* )0 1*7', ". Th'" a / 7& # .ha. #61##!" )0 1hara1.#r" ,'". -# -r*$# a:.#r )0.h@ a QRN &" .* -# a88# !#! a" .h# & !&1a.&* *: :*7!& + a ! .h# 8r*1#""& + &" .* -# 1* .& '#! %&.h .h# r#"&!'#. Th# & 8'. &" .* -# "'887&#! .hr*'+h a .#6. :&7# 1r#a.#! -/ .h# '"#r. e*"o P nter t"e 'ilenameP rea% 'ile linesEY&* -l C J'ileY e*"o P3ile J'ile "as Jlines Lines....P *ountE1 i' V -' tem!.txt X t"en rm tem!.txt 'i &"ile V J*ount -le Jlines X :e!t.o' MCA( 8;C 32

Unix Programming Laboratory Manual %o lbE1 ubE40 *"arE0 textEYtail -n bJ*ount J'ile Z "ea% -1Y *"arEYe*"o Jtext Z &* -*Y We*"o P<"ere are J*"ar *"ara*ters in t"is lineP *"artem!EJ*"ar i' V J*"ar -le 40 X t"en e*"o Jtext HH tem!.txt else &"ile V J*"ar -gt 40 X %o tEYe*"o PJtextP Z *ut -* Jlb-JubY e*"o PJt BBP HH tem!.txt *"arEYex!r J*"ar - 40Y i' V J*"ar -le 40 X t"en e*"o PJtextP Z *ut -* Jub-J*"artem! HH tem!.txt 'i lbEYex!r Jlb b 40Y ubEYex!r Jub b 40Y %one 'i *ountEYex!r J*ount b 1Y %one e*"o P Contents o' t"e 'ile a'ter 'ol%ing are) P *at tem!.txt R' 2

I Semester MCA 2013-14

Js" 1b.s" nter t"e 'ilename 'ol%Usam!le 3ile 'ol%Usam!le "as 3 Lines.... Contents o' t"e 'ile a'ter 'ol%ing are) 1234-./010 1234-./010 1234-./010 1234-./ B /010 t"ere is a sam!le line insi%e t"is to *" B "e*+ 'ile 'ol%ing !rogram ---------------------------------------------------------------------------------------------------------------------------:e!t.o' MCA( 8;C 33

Unix Programming Laboratory Manual

I Semester MCA 2013-14

10a. 5r&.# a a%$ "1r&8. .ha. a11#8." !a.# ar+',# . & .h# :*r, *: ,,(!!(// a ! !&"87a/" &. & .h# :*r, *: !a/@ ,* .h@ a ! /#ar. Th# "1r&8. "h*'7! 1h#1$ .h# =a7&!&./ *: .h# ar+',# . a ! & .h# 1a"# *: #rr*r@ !&"87a/ a "'&.a-7# ,#""a+#. $ @I7 c system5P*learP6\ %aEP312031303130313130313031P moEPQA73 $MA8AP8MA]QU7QULAU@S PMC<7M;: CP mmEsubstr5A8@;V1X(1(26 %%Esubstr5A8@;V1X(4(26 yyEsubstr5A8@;V1X(/(46 i'5%% H substr5%a(2=mm-1(26 ZZ 5mmH126 ZZ A8@C RE26 !rint PIn#ali% %ateP else !rint' P<"e %ay is F%Bn <"e mont" is FsBn <"e year is F%BnP(%%(substr5mo(3=mm-2(36(yy d R' 2 Ja&+ -' 10a.a&+ 12-0/-2013 <"e %ay is / <"e mont" is : C <"e year is 2013

D#"1r&8.&*
A&+ s*ri!ts *onsist o' 3 !arts 1. $e'ore 5%enote% using) $ @I76 - exe*ute% !rior to t"e 'irst line o' t"e in!ut being rea% 2. :uring 5Main A&+ loo!6 - 3o*uses on loo!ing t"roug" lines o' in!ut 3. A'ter 5%enote% using) 7:6 exe*ute% a'ter last line o' in!ut "as been !ro*esse% 7ote ) $ @I7 an% 7: *omments are o!tional 10-. 5r&.# a a%$ "1r&8. .* !#7#.# !'87&1a.#! 7& # :r*, a .#6. :&7#. Th# *r!#r *: .h# *r&+& a7 7& #" ,'". r#,a& ' 1ha +#!. c aVnbbXEJ0 d 7: c 'or5iE0\iCn\ibb6 c 'lagE0\ 'or5SE0\SCi\Sbb6 :e!t.o' MCA( 8;C 34

Unix Programming Laboratory Manual c i'5 aViX EE aVSX6 c 'lagE1\ brea+\ d d i' 5'lag EE 06 !rint' ?Fs BnA( aViX d d R' 2 J *at test *ollege *ollege bangalore

I Semester MCA 2013-14

J a&+ (' 10b.a&+ test *ollege $angalore -------------------------------------------------------------------------------------------------------------------------11a. 5r&.# a a%$ "1r&8. .* :& ! *'. .*.a7 ',-#r *: -**$" "*7! & #a1h !&"1&87& # a" %#77 a" .*.a7 -**$ "*7! '"& + a""*1&a.# arra/ !*% .a-7# a" +&=# -#7*%. E7#1.r&1a7 3) M#1ha &1a7 E7 E7#1.r&1a7 F0 C*,8'.#r S1&# 1# )3 M#1ha &1a7 E3 C&=&7 GF C*,8'.#r S1&# 1# E) $ @I7 c!rint ?<M<AL 7UM$ 8 M3 $MM9S SML: I7 ACD CA< @M8]Ad c boo+sVJ1XbEJ2d 7: c 'or 5item in boo+s6 c !rint' 5? Fs sol%E F%BnA(item(boo+sVitemXd total bEboo+sVitemX d

:e!t.o' MCA( 8;C

3-

Unix Programming Laboratory Manual !rint'5A<otal boo+s sol%EF%A(total6 d R' 2 <M<AL 7UM$ 8 M3 $MM9S SML: I7 ACD CA< @M8] ele*tri*al me*"ani*al *om!uters *i#il 114 13/ 10/ 110

I Semester MCA 2013-14

<otal boo+s sol% E --.

D#"1r&8.&*
Asso*iati#e arrays are li+e tra%itional arrays ex*e!t t"ey uses strings as t"eir in%exes rat"er t"an numbers. 2"en using an asso*iati#e array( you *an mimi* tra%itional array by using numeri* string as in%ex.
Syntax: arrayname[string]=value

In t"e abo#e a&+ syntax) arra/ a,# is t"e name o' t"e array( ".r& + is t"e in%ex o' an array( =a7'# is any #alue assigning to t"e element o' t"e array. <o a**ess all t"e array elements( you *an use a loo! to go t"roug" all t"e in%exes o' an array as s"o&n belo&. Syntax) 'or 5#ar in arrayname6 a*tions In t"e abo#e a&+ syntax) =ar is any #ariable name & is a +ey&or% arra/ a,# is t"e name o' t"e array. a1.&* " are list o' statements to be !er'orme%. I' you &ant to !er'orm more t"an one a*tion( it "as to be en*lose% &it"in bra*es. 11-. 5r&.# a a%$ "1r&8. .* 1*,8'.# +r*"" "a7ar/ *: a #,87*/## a11*r!& + .* r'7# +&=# -#7*%. I: -a"&1 "a7ar/ &" L 10000 .h# HRAS13T *: -a"&1 < DAS)3T *: -a"&1. I: -a"&1 "a7ar/ &" DS10000 .h# HRAS20T *: -a"&1 < DAS30T *: -a"&1 $ @I7 c !rint' P nter t"e $asi* Pay ) 8s. P

:e!t.o' MCA( 8;C

3.

Unix Programming Laboratory Manual getline b! C P4%e#4ttyP i'5b!C100006 c d else c "raE.2=b! %aE.-=b! d gsEb!b"rab%a !rint' P@ross Salary E 8s. F.2'BnP( gs d R' 2 Ja&+ L' 11b.a&+ nter t"e $asi* Pay ) 8s. 10000 @ross Salary E 8s. 1/000 "raE.1-=b! %aE.4-=b!

I Semester MCA 2013-14

D#"1r&8.&*
<"e getline *omman% *an be use% &it"out arguments to rea% in!ut 'rom t"e *urrent in!ut 'ile. All it %oes in t"is *ase is rea% t"e next in!ut re*or% an% s!lit it u! into 'iel%s. <"is is use'ul i' youO#e 'inis"e% !ro*essing t"e *urrent re*or%( but you &ant to %o some s!e*ial !ro*essing right now on t"e next re*or%.
getline < file

<"is 'orm o' t"e getline 'un*tion ta+es its in!ut 'rom t"e 'ile file. Dere file is a string-#alue% ex!ression t"at s!e*i'ies t"e 'ile name. `< file' is *alle% a r#!&r#1.&* sin*e it %ire*ts in!ut to *ome 'rom a %i''erent !la*e. <"is 'orm is use'ul i' you &ant to rea% your in!ut 'rom a !arti*ular 'ile( instea% o' 'rom t"e main in!ut stream.

:e!t.o' MCA( 8;C

3/

Unix Programming Laboratory Manual

I Semester MCA 2013-14

A!!&.&* a7 E6#r1&"#"
1. 2rite a s"ell s*ri!t to 'in% t"e sum o' in%i#i%ual %igits o' a number. 2. 2rite a s"ell s*ri!t to 'in% a gi#en number is e#en or o%%. 3. 2rite a s"ell s*ri!t to *reate 2 %ire*tories an% store 10 'iles in one %ire*tory using 'ile relate% *omman%s an% trans'er all 'iles to ot"er %ire*tory. 4. 2rite a s"ell s*ri!t to 'in% &"et"er a gi#en number is !rime or not -. 2rite a s"ell s*ri!t to *o!y a 'ile &it" all ty!es o' #ali%ation. .. 2rite a s"ell s*ri!t to store %ata in a 'ile an% *om!ress it. /. 2rite a s"ell s*ri!t to %is!lay a gi#en number in terms o' &or%s using s&it*" statement. 0. 2rite a s"ell s*ri!t to im!lement &* &it" all 3 o!tions. 1. 2rite a s"ell s*ri!t to a%% e#en numbere% an% o%% numbere% !ositional !arameters #alues 10. 2rite a s"ell s*ri!t to %is!lay 7 3ibona**i se,uen*e. 11. 2rite an a&+ s*ri!t t"at 'ol%s long lines into 40 *olumns. <"us any line t"at ex*ee%s 40 *"ara*ters must be bro+en a'ter 40t"( a ABA is to be a!!en%e% as t"e in%i*ation o' 'ol%ing an% t"e !ro*essing is to be *ontinue% &it" t"e resi%e. <"e in!ut is to be su!!lie% t"roug" a text 'ile *reate% by t"e user. 12. 2rite an a&+ s*ri!t to a**e!t a 'ile name an% 'in% out number o' re*or%s an% 'iel% se!arator in t"e 'ile. 13.2rite a se% instru*tion to 'in% &"et"er t"e gi#en 'ile "as exe*ute !ermission 'or t"e grou! t"e o&ner belongs to.

:e!t.o' MCA( 8;C

30

Unix Programming Laboratory Manual

I Semester MCA 2013-14

U&=a ( U*1# ;'#".&* "


1.6 2"at is *al *omman%Os 8esult ) &it" an% &it"out argumentT 26 2"at &ill be t"e out!ut o' e*"o *omman% &it"out any argumentT 36 2"at &ill ls *omman% &it" -i o!tion %is!layT 46 2"at %oes t"e 'ollo&ing 'ile "an%ling *omman%s !er'orm &it" its o!tions) i6 *! ii6 rm -6 2"at %oes *% *omman% %o &"en use% &it"out argumentsT .6 2"at is o!tion an% argument to t"e *omman%T /6 List out t"e ty!es o' s"ells !resentT 06 2"at is t"e nee% o' s!lit *omman%T 16 2"at are t"e 3 mo%es o' #i e%itorT 106 Do& *an &e *"ange 'rom one mo%e to anot"er mo%e in #i e%itorT 116 x!lain t!ut *omman% &it" its argumentsT 126 2"at is t"e o!tion use% &it" ls *omman% to %is!lay 'ile attributes( ino%e number an% "i%%en 'ilesT 136 2"at is t"e nee% o' *ut *omman%T 146 2"at is t"e use o' Z o!eratorT 1-6 Do& %o &e use *at *omman% to %is!lay *ontents o' 'ile( *reate a 'ile an% a!!en% to a 'ileT 1.6 7ame t"e *omman% to *reate a lin+ 'or a 'ile an% ex!lain t"e syntaxT 1/6 x!lain tee *omman%. 106 x!lain substitution *omman% in se%T 116 2"at are t"e *omman%s use% in ex mo%e o' #i e%itorT 206 x!lain t"e *"mo% *omman% 216 7ame t"e *omman% &"i*" is use% to %is!lay t"e out!ut in terminal as &ell in t"e 'ileT 226 Do& *an &e *"ange t"e !ermission o' a 'ileT 236 2"at %oes tail *omman% meansT 246 2"at %oes "ea% *omman% meansT 2-6 2"at %oes tr *omman% %oT 2.6 2"at are t"e o!tions use% &it" &* *omman% to +no& number o' &or%s ( number o' lines an% number o' *"ara*ters in a 'ileT :e!t.o' MCA( 8;C 31

Unix Programming Laboratory Manual 2/6 x!lain sort *omman% &it" any 2 o!tions

I Semester MCA 2013-14

206 Do& *an &e 'in% out t"e number o' users &"o "a#e logge% on into t"e systemT 216 2"at is t"e nee% o' gre! *omman%T 306 2"at is t"e o!tion use% &it" gre! *omman% to %is!lay line number &"ere t"e !attern is 'oun%T 316 x!lain any 3 o!tions o' gre! *omman% &it" suitable exam!le 326 2"at is t"e use o' -# o!tion in gre! *omman%T 336 2"at is .bas"U!ro'ileT 346 2"at is a s"ell s*ri!tT 3-6 x!lain 16 JW a. b. 26 J= 3.6 2"at is se% instru*tionT x!lain t"e 'ollo&ing &it" res!e*t to se%) Line a%%ressing Context a%%ressing

3/6 x!lain any 'our built in #ariables o' a&+ &it" exam!les. 306 x!lain t"e *ontrol 'lo& 'eature in a&+T 316 2"en %o &e use set *omman%T 406 2"at is a 'ilterT @i#e some exam!les o' 'ilter *omman%. 416 Do& *an you extra*t only %ay 'rom %ate *omman%T 426 7ame any 2 system %e'ine% 'un*tions in a&+T 436 2"at is t"e nee% o' using %ouble "y!en5--6 in t"is statement) set - - ls Ll J'ilename 446 List out t"e #arious system #ariables use% in a&+ s*ri!t 4-6 2"at %oes system #ariable 73 an% 78 %enote in a&+ s*ri!t 4.6 :istinguis" bet&een &"oami an% &"o am i. 4/6 7ame t"e %e'ault s"ell. 406 Mention t"e met"o%s a#ailable to *"ange 'ile !ermissions 416 2"at is t"e use o' I3ST -06 2"at is t"e use o' s*ri!t *omman%T 2"at are t"e o!tions o' s*ri!t *omman%T

:e!t.o' MCA( 8;C

40

Unix Programming Laboratory Manual

I Semester MCA 2013-14

R U COLLEGE OF ENGINEERING DEPARTMENT OF MCA

UNIX Pr*+ra,,& + La-*ra.*r/ ( LABCYCLES


La- C/17# 12 A. x!lore t"e Unix en#ironment
$. x!lore #i e%itor &it" #im tutor. Per'orm t"e 'ollo&ing o!erations using #i e%itor( but not limite% to) 1. insert *"ara*ter( %elete *"ara*ter( re!la*e *"ara*ter 2. Sa#e t"e 'ile an% *ontinue &or+ing 3. sa#e t"e 'ile a exit t"e e%itor 4. ,uit t"e e%itor -. ,uit &it"out sa#ing t"e 'ile .. rename a 'ile /. insert lines( %elete lines( 0. set line numbers 1. sear*" 'or a !attern 10. mo#e 'or&ar% an% ba*+&ar%

La- C/17# 22 1a. 2rite a s"ell s*ri!t t"at ta+es a #ali% %ire*tory name as an argument an% re*ursi#ely %es*en% all t"e sub%ire*tories( 'in%s t"e maximum lengt" o' any 'ile in t"at "ierar*"y an% &rites t"is maximum #alue to t"e stan%ar% out!ut. b. 2rite a s"ell s*ri!t t"at a**e!ts a !at" name an% *reates all t"e *om!onents in t"at !at" name as %ire*tories. 3or exam!le( i' t"e s*ri!t is name% m!*( t"en t"e *omman% m!* a4b4*4% s"oul% *reate %ire*tories a( a4b( a4b4*( a4b4*4%. La- C/17# 32 2a. 2rite a s"ell s*ri!t t"at a**e!ts t&o 'ile names as arguments( *"e*+s i' t"e !ermissions 'or t"ese 'iles are i%enti*al an% i' t"e !ermissions are i%enti*al( out!ut *ommon !ermissions an% ot"er&ise out!ut ea*" 'ile name 'ollo&e% by its !ermissions. b. 2rite a s"ell s*ri!t &"i*" a**e!ts #ali% log-in names as arguments an% !rints t"eir *orres!on%ing "ome %ire*tories( i' no arguments are s!e*i'ie%( !rint a suitable error message.

La- C/17# )2 3a. b. Create a s*ri!t 'ile *alle% 'ile-!ro!erties t"at rea%s a 'ile name entere% an% out!uts it !ro!erties. 2rite s"ell s*ri!t to im!lement terminal lo*+ing 5similar to t"e lo*+ *omman%6. It s"oul% !rom!t t"e user 'or a !ass&or%. A'ter a**e!ting t"e !ass&or% entere% by t"e user( it must !rom!t again 'or t"e mat*"ing !ass&or% as *on'irmation an% i' mat*" o**urs( it must lo*+ t"e +ey&or% until a :e!t.o' MCA( 8;C 41

Unix Programming Laboratory Manual

I Semester MCA 2013-14

mat*"ing !ass&or% is entere% again by t"e user( 7ote t"at t"e s*ri!t must be &ritten to %isregar% $8 A9( *ontrol-:. 7o time limit nee% be im!lemente% 'or t"e lo*+ %uration La- C/17# 32 4a b 2rite a s"ell s*ri!t t"at a**e!t one or more 'ilenames as argument an% *on#ert all o' t"em to u!!er*ase( !ro#i%e% t"ey exist in *urrent %ire*tory. 2rite a s"ell s*ri!t t"at %is!lays all t"e lin+s to a 'ile s!e*i'ie% as t"e 'irst argument to t"e s*ri!t. <"e se*on% argument( &"i*" is o!tional( *an be use% to s!e*i'y in &"i*" t"e sear*" is to begin. I' t"is se*on% argument is not !resent( t"e sear*" is to begin in *urrent &or+ing %ire*tory. In eit"er *ase( t"e starting %ire*tory as &ell as all its sub%ire*tories at all le#els must be sear*"e%. <"e s*ri!t nee% not in*lu%e any error *"e*+ing.

La- C/17# E2 -a. 2rite a s"ell s*ri!t t"at a**e!ts 'ilenames as arguments an% %is!lays its *reation time i' t"e 'ile exists an% i' t"e 'ile %oes not exists( sen% out!ut error message. b. 2rite a s"ell s*ri!t to %is!lay t"e *alen%ar 'or *urrent mont" &it" *urrent %ate re!la*e% by = or == %e!en%ing on &"et"er t"e %ate "as one %igit or t&o %igits. .a. 2rite a s"ell s*ri!t to 'in% a 'ile4s t"at mat*"es a !attern gi#en as *omman% line argument in t"e "ome %ire*tory( %is!lay t"e *ontents o' t"e 'ile an% *o!y t"e 'ile into t"e %ire*tory >4my%ir. b. 2rite a s"ell s*ri!t to list all t"e 'iles in a %ire*tory &"ose 'ilename is at least 10 *"ara*ters. 5Use ex!r *omman% to *"e*+ t"e lengt"6. La- C/17# 72 /a. 2rite a s"ell s*ri!t t"at gets exe*ute% an% %is!lays t"e message eit"er ?@oo% MorningA or ?@oo% A'ternoonA or ?@oo% #eningA %e!en%ing u!on time at &"i*" t"e user logs in. b. 2rite a s"ell s*ri!t t"at a**e!t a list o' 'ilenames as its argument( *ount an% re!ort o**urren*e o' ea*" &or% t"at is !resent in t"e 'irst argument 'ile on ot"er argument 'iles.

La- C/17# F2 0a. 2rite a s"ell s*ri!t t"at %etermine t"e !erio% 'or &"i*" a s!e*i'ie% user is &or+ing on system an% %is!lay a!!ro!riate message. b. 2rite a s"ell s*ri!t t"at re!orts t"e logging in o' a s!e*i'ie% user &it"in one minute a'ter "e4s"e log in. <"e s*ri!t automati*ally terminate i' s!e*i'ie% user %oes not log in %uring a s!e*i'ie% !erio% o' time.

:e!t.o' MCA( 8;C

42

Unix Programming Laboratory Manual

I Semester MCA 2013-14

La- C/17# G2 1a. 2rite a s"ell s*ri!t t"at a**e!ts t"e 'ile name( starting an% en%ing line number as an argument an% %is!lay all t"e lines bet&een t"e gi#en line number. b. 2rite a s"ell s*ri!t t"at 'ol%s long lines into 40 *olumns. <"us any line t"at ex*ee%s 40 *"ara*ters must be bro+en a'ter 40t"( a ?BA is to be a!!en%e% as t"e in%i*ation o' 'ol%ing an% t"e !ro*essing is to be *ontinue% &it" t"e resi%ue. <"e in!ut is to be su!!lie% t"roug" a text 'ile *reate% by t"e user.

La- C/17# 102 10a. 2rite an a&+ s*ri!t t"at a**e!ts %ate argument in t"e 'orm o' mm-%%-yy an% %is!lays it in t"e 'orm o' %ay( mont"( an% year. <"e s*ri!t s"oul% *"e*+ t"e #ali%ity o' t"e argument an% in t"e *ase o' error( %is!lay a suitable message. b. 2rite an a&+ s*ri!t to %elete %u!li*ate% line 'rom a text 'ile. <"e or%er o' t"e original lines must remain un*"ange%.

La- C/17# 112 11a. 2rite an a&+ s*ri!t to 'in% out total number o' boo+s sol% in ea*" %is*i!line as &ell as total boo+ sol% using asso*iate array %o&n table as gi#en belo&. le*tri*al 34 Me*"ani*al ./ le*tri*al 00 Com!uter S*ien*e 43 Me*"ani*al .Ci#il 10 Com!uter S*ien*e .4 b. 2rite an a&+ s*ri!t to *om!ute gross salary o' an em!loyee a**or%ing to rule gi#en belo&. I' basi* salary is C 10000 t"en D8AE1-F o' basi* G :AE4-F o' basi*. I' basi* salary is HE10000 t"en D8AE20F o' basi* G :AE-0F o' basi*. La- C/17# 122 <o Com!lete $a*+ Logs( i' any an% 8e#ision .

:e!t.o' MCA( 8;C

43

Unix Programming Laboratory Manual

I Semester MCA 2013-14

NOTE2 2rite t"e !rogram5s6 o' t"e *urrent lab *y*le in t"e %ata s"eet !rior to *oming to t"e lab. Mn t"e le't si%e o' t"e re*or% t"e stu%ents are re,uire% to ex!lain t"e syntax an% meaning o' t"e *omman%s( !rogramming *onstru*ts use% in t"e !rogram. rrors en*ountere% %uring t"e *om!ilation must be &ritten in t"e %ata s"eet 'irst( along &it" t"e solutions to *orre*t t"e errors an% t"e same to be &ritten on t"e le't si%e o' t"e re*or%. <"e *om!lete ,uestion( Program sour*e *o%e an% a minimum o' 2 out!uts &it" t"e #ali%ations( must be &ritten on t"e rig"t "an% si%e o' t"e re*or%. Program out*ome- t"e unix *on*e!ts learnt in t"e *urrent !rogram( along &it" t"eir a!!li*ation areas must be &ritten on t"e le't si%e o' t"e re*or%( 'or e#ery !rogram. Any ba*+logs to be *om!lete% only a'ter 'inis"ing t"e regular %e'ine% lab *y*les.

La- E=a7'a.&* S1h#,#


I .#r a7 E=a7'a.&* Lab Internals 2 30 ,ar$"

8e*or% Mar+s ) 20 mar+s ) 30 mar+s Programming S+ills) 10 mar+s E6a, E=a7'a.&* ;i#a Mar+s 2rite u! xe*ution ) 30 ,ar$" ) 1-F ) 1-F ) /0F

3a*ulty in-*"arge I Sem eAK Se*tion


:e!t.o' MCA( 8;C

3a*ulty in-*"arge I Sem e$K Se*tion

:ire*tor

44

Das könnte Ihnen auch gefallen