Unix Lab -II Sem Manual
-
Upload
prabutmalai-sekar -
Category
Documents
-
view
241 -
download
0
Transcript of Unix Lab -II Sem Manual
-
7/29/2019 Unix Lab -II Sem Manual
1/35
0
IDHAYA ENGINEERI NG COLLEGE FOR WOMEN
Chinnasalem
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERI NG
UNIX LAB MANUAL
II SEMESTER-I YEAR ECE
Prepared by
MR.S.RAJA,M.E.,LECTURER IN IT
-
7/29/2019 Unix Lab -II Sem Manual
2/35
1
IDHAYA ENGINEERING COLLEGE FOR WOMEN
DEPARTMENT OF INFORMATION TECHNOLOGY
SEMESTER II
GE2155 COMPUTER PRACTICE LABORATORY II 0 1 2 100
LIST OF EXPERIMENTS
1. UNIX COMMANDS 15
Study of UNIX OS - Basic Shell Commands - Unix Editor
2. SHELL PROGRAMMING 15
Simple Shell program - Conditional Statements - Testing and Loops
3. C PROGRAMMING ON UNIX 15
Dynamic Storage Allocation-Pointers-Functions-File Handling
TOTAL : 45 PERIODS
HARDWARE / SOFTWARE REQUIREMENTS FOR A BATCH OF 30 STUDENTS
Hardware
1 UNIX Clone Server
33 Nodes (thin client or PCs)
Printer 3 Nos.
Software
OS UNIX Clone (33 user license or License free Linux)
Compiler - C
-
7/29/2019 Unix Lab -II Sem Manual
3/35
2
BASIC UNIX COMMANDS
Aim:
To study and execute the File commands and General Purpose Commands in Unix.
GENERAL PURPOSE COMMANDS
1.Command: echo This command is used to display whatever message we want to be displayed on screen.
((eegg)) $$ eecchhoo IIddhhaayyaa EEnnggiinneeeer r iinngg CCoolllleeggee f f oor r wwoommeenn IIddhhaayyaa EEnnggiinneeeer r iinngg CCoolllleeggee f f oor r wwoommeenn
22.. CC oomm mm aa nn dd :: ww hh oo TThhiiss ccoommmmaanndd ggiivveess uuss tthhee ddeettaaiillss oof f wwhhoo aallll hhaavvee llooggggeedd iinnttoo tthhee UUnniixx ssyysstteemm ccuur r r r eennttllyy..
((eegg)) $$ wwhhoo aa b bcc ttttyy00 f f ee b b 2266 1111::1177 xxyyzz ttttyy44 f f ee b b 2266 1111::3300 ggeef f ttttyy99 f f ee b b 2266 1111::4488
33..CC oomm mm aa nn dd :: ww hh oo aa mm ii TThhiiss CCoommmmaanndd iiddeennttiif f iieess tthhee uusseer r aanndd lliissttss tthhee uusseer r nnaammee,, tteer r mmiinnaall nnaammee,, tthhee ddaattee aanndd tthhee ttiimmee oof f llooggiinn.. ((eegg))$$ wwhhoo aamm ii
aa b bcc ttttyy00 f f ee b b 2266 1111::1177
44..CC oomm mm aa nn dd :: dd aa tt ee TThhiiss ccoommmmaanndd iiss uusseedd ttoo ddiiss p pllaayy ccuur r r r eenntt ddaattee..
((eegg))$$ddaattee MMoonn f f ee b b 2266 1133::3300::4455 11sstt 22000099
TThhee ddaattee ccoommmmaanndd ccaann aallssoo uusseedd wwiitthh f f oolllloowwiinngg f f oor r mmaatt ss p peecciif f iiccaattiioonnss..
FF oorr mm aa tt PP uu rr pp oossee
++%%mm TToo ddiiss p pllaayy oonnllyy mmoonntthh
++%%hh TToo ddiiss p pllaayy mmoonntthh nnaammee
++%%dd TToo ddiiss p pllaayy ddaayy oof f mmoonntthh
++%%yy TToo ddiiss p pllaayy llaasstt 22 ddiiggiittss oof f tthhee yyeeaar r
++%%HH TToo ddiiss p pllaayy HHoouur r ss
-
7/29/2019 Unix Lab -II Sem Manual
4/35
-
7/29/2019 Unix Lab -II Sem Manual
5/35
4
((eegg)) $$ p pwwdd
55..CC oomm mm aa nn dd :: rr mm dd iirr TThhiiss ccoommmmaanndd iiss uusseedd ttoo r r eemmoovvee aa ddiir r eeccttoor r yy ss p peecciif f iieedd iinn tthhee ccoommmmaanndd lliinnee.. ((eegg)) $$ r r mmddiir r tteemm p p
66..CC oomm mm aa nn dd :: ccaa tt ((aa)) ccaa tt f f iilleenn aa mm ee:: TThhiiss ccoommmmaanndd hheell p pss uuss ttoo lliisstt tthhee ccoonntteennttss oof f aa f f iillee wwee ss p peecciif f yy.. ((eegg)) $$ ccaatt xxyyzz
(( b b)) ccaa tt >> f f iilleenn aa mm ee:: TThhiiss ccoommmmaanndd hheell p pss uuss ttoo ccr r eeaattee aa nneeww f f iillee wwee ss p peecciif f yy.. ((eegg)) $$ ccaatt >> xxyyzz
((cc)) ccaa tt >>>> f f iilleenn aa mm ee:: TThhiiss ccoommmmaanndd hheell p pss uuss ttoo aa p p p peenndd aa ccoonntteennttss ttoo tthhee eexxiissttiinngg f f iillee aanndd iif f nnoott eexxiissttiinngg,, ccr r eeaattee aa nneeww f f iillee wwee ss p peecciif f yy.. ((eegg)) $$ ccaatt >>>> xxyyzz
77..CC oomm mm aa nn dd :: rr mm TThhiiss ccoommmmaanndd iiss uusseedd ttoo r r eemmoovvee oonnee oor r mmoor r ee f f iilleess f f r r oomm aa ddiir r eeccttoor r yy.. ((eegg)) $$ r r mm tteemm p p//mmaatt
88..CC oomm mm aa nn dd :: ccpp This command is used to copy one or more files from a source directory to a target directory
((eegg)) $$ cc p p tteemm p p //mmaatt tteemm p p11//mmaatt
99..CC oomm mm aa nn dd :: mm vv TThhiiss ccoommmmaanndd iiss uusseedd ttoo mmoovvee oonnee oor r mmoor r ee f f iilleess f f r r oomm aa ddiir r eeccttoor r yy..
((eegg)) $$ mmvv tteemm p p //mmaatt tteemm p p11//mmaatt
1100..CC oomm mm aa nn dd :: llss TThhiiss ccoommmmaanndd iiss uusseedd ttoo f f iinndd tthhee llooccaattiioonn.. ((eegg)) $$ llss
Command Actionllss--ll LLiissttss aallll f f iilleess iinn tthhee lloonngg f f oor r mmaatt
llss--tt LLiissttss iinn oor r ddeer r oof f llaasstt mmooddiif f iiccaattiioonn ttiimmee
llss--aa LLiissttss aallll eennttr r iieess,, iinncclluuddiinngg hhiiddddeenn f f iilleess llss--dd LLiissttss ddiir r eeccttoor r yy f f iillee iinnsstteeaadd oof f iittss ccoonntteennttss
llss-- p p PPuuttss aa ssllaasshh aaf f tteer r eeaacchh ddiir r eeccttoor r yy
llss--uu LLiissttss iinn oor r ddeer r oof f llaasstt aacccceessss ttiimmee
-
7/29/2019 Unix Lab -II Sem Manual
6/35
5
INPUT-OUTPUT REDIRECTION PIPES AND FILTERS
11.. II nn pp uu tt R R eedd iirr eecctt iioonn :: TThhee SSyymm b booll iiss tthhee r r eeddiir r eeccttiinngg iinn p puutt oo p peer r aattoor r .. IItt r r eeaaddss iinn p puutt f f r r oomm tthhee ss p peecciif f iieedd f f iillee.. ((eegg)) $$ ccaatt >> ssaamm p p llee
3. Pipes:TThhee p pii p pee iiss aa ttooooll iinn wwhhiicchh tthhee oouutt p puutt oof f oonnee ccoommmmaanndd ccaann b bee cchhaannnneelleedd iinnttoo tthhee iinn p puutt oof f aannootthheer r ccoommmmaanndd.. ((eegg)) $$ wwhhoo || wwcc
FILTERS
44.. TT hh ee HH eeaa dd FF iill tt eerr :: IItt ddiiss p pllaayyss tthhee lliinneess f f r r oomm tthhee b beeggiinnnniinngg oof f aa f f iillee.. ((eegg)) $$ hheeaadd 44 ssaamm p pllee IItt ddiiss p pllaayyss ttoo p p 44 lliinneess oof f tthhee f f iillee ssaamm p pllee..
55.. TT hh ee TT aa iill FF iill tt eerr :: IItt ddiiss p pllaayyss tthhee lliinneess f f r r oomm tthhee eenndd oof f aa f f iillee.. ((eegg)) $$ ttaaiill 44 ssaamm p p llee IItt ddiiss p pllaayyss 44 lliinneess f f r r oomm tthhee eenndd oof f tthhee f f iillee ssaamm p pllee..
66.. TT hh ee pp gg f f iilltt eerr :: IItt sshhoowwss tthhee f f iillee p paaggee b byy p paaggee ((eegg)) $$ llss || p pgg
77.. TT hh ee ggrr eepp f f ii lltt eerr :: IItt iiss uusseedd ttoo sseeaar r cchh aanndd p pr r iinntt ss p peecciif f iieedd p paatttteer r nnss f f r r oomm aa f f iillee.. $$ ggr r ee p p [[oo p pttiioonn]] p paatttteer r nn f f iillee((ss)) ((eegg)) $$ ggr r ee p p aassiiaa ssaamm p pllee
8. Options available in grep commandCC oomm mm aa nn dd PP uu rr pp oossee
--vv DDiiss p pllaayyss oonnllyy tthhoossee lliinneess wwhhiicchh ddoo nnoott mmaattcchh tthhee p paatttteer r nn ss p peecciif f iieedd
--cc DDiiss p pllaayyss oonnllyy tthhee ccoouunntt oof f tthhoossee lliinneess wwhhiicchh mmaattcchh tthhee p paatttteer r nn ss p peef f iicciieedd
--nn DDiiss p pllaayyss tthhoossee lliinneess wwhhiicchh mmaattcchh tthhee p paatttteer r nn ss p peecciif f iieedd aalloonngg wwiitthh tthhee lliinnee nnuumm b beer r aatt tthhee b beeggiinnnniinngg oof f tthhee f f iillee..
--ii DDiiss p pllaayyss tthhoossee lliinneess wwhhiicchh mmaattcchh tthhee p paatttteer r nn ss p peecciif f iieedd iiggnnoor r iinngg tthhee ccaassee ddiissttiinnccttiioonn..
-
7/29/2019 Unix Lab -II Sem Manual
7/35
6
99.. TT hh ee SSoorr tt f f ii lltt eerr :: TThhee ssoor r tt ccoommmmaanndd iiss uusseedd ttoo ssoor r tt tthhee ccoonntteennttss oof f tthhee f f iillee.. $$ssoor r tt f f iilleennaammee
((eegg)) $$ ssoor r tt ssaamm p pllee
10. Options available in sort command
CC oomm mm aa nn dd PP uu rr pp oossee
--r r SSoor r ttss aanndd ddiiss p pllaayyss tthhee f f iillee ccoonntteennttss iinn r r eevveer r ssee aall p phhaa b beettiiccaall oor r ddeer r
--nn SSoor r ttss nnuummeer r iiccaallllyy
--nnr r SSoor r ttss nnuumm b beer r ss iinn r r eevveer r ssee oor r ddeer r
--uu SSoor r ttss aanndd r r eemmoovveess dduu p plliiccaattee r r eeccoor r ddss
1111.. TT hh ee uu nn iiqq f f iilltt eerr :: TThhee uunniiqq f f iilltteer r ccoomm p paar r eess aadd j jaacceenntt lliinneess iinn tthhee ssoor r tteedd iinn p puutt f f iillee,, aanndd wwhheenn uusseedd wwiitthh aa ddiif f f f eer r eenntt oo p pttiioonn ddiiss p pllaayyss ssiinnggllee ooccccuur r r r eenncceess aanndd mmuullttii p p llee ooccccuur r r r eenncceess.. ((eegg)) $$ ssoor r tt ssaamm p pllee || uunniiqq
1122.. TT hh ee ccuu tt f f ii lltt eerr :: OOnnee p paar r ttiiccuullaar r f f iieelldd f f r r oomm aannyy f f iillee oof f f f r r oomm oouutt p puutt oof f aannyy ccoommmmaanndd ccaann b bee eexxttr r aacctteedd aanndd ddiiss p p llaayyeedd uussiinngg tthhee ccuutt ccoommmmaanndd ((eegg)) $$ ccuutt dd :: ssaamm p pllee
1133.. TT hh ee PP aa sstt ee ccoomm mm aa nn dd :: TThhiiss ccoommmmaanndd mmeer r ggeess tthhee ccoonntteennttss oof f ttwwoo f f iilleess iinnttoo aa ssiinnggllee f f iillee.. ((eegg)) $$ p paassttee ssaamm p pllee11 ssaamm p pllee22 ssaamm p pllee33
PROBLEMS
11.. CC rr eeaa tt ee aa f f iillee ccaa lllleedd sstt uu dd eenn tt ww hh iicchh ccoonn tt aa iinn ss tt hh ee ssaa mm pp llee dd aa tt aa aa ss f f oo llllooww ss:: SS9999000011 SSnneehhaa K K uummaar r 5544 SS9999000055 PPr r aavveeeenn K K aallssii 8899
aa )) DDiisspp llaa yy tt hh ee ccoonn tt eenn tt ss oof f aa f f iillee ssoorr tt eedd aa ccccoorr dd iinn gg tt oo tt hh eeiirr mm aa rr k k ss iinn dd eesscceenn dd iinn gg oorr dd eerr .. SSoollnn :: ssoor r tt ++33 r r nn ssttuuddeenntt OO uu tt pp uu tt :: SS9999000055 PPr r aavveeeenn K K aallssii 8899 SS9999000011 SSnneehhaa K K uummaar r 5544
-
7/29/2019 Unix Lab -II Sem Manual
8/35
-
7/29/2019 Unix Lab -II Sem Manual
9/35
8
EE xxtt rr aa cctt tt hh ee lliinn eess ww hh iicchh eenn dd ww iitt hh iiaa .. SSoollnn :: ggr r ee p p iiaa$$ p pllaacceess OO uu tt pp uu tt :: BBoomm b baayy IInnddiiaa 4455667777 aassiiaa
K K aar r aacchhii PPaak k iissttaann 5544887766 AAssiiaa
11.. CC rr eeaa tt ee aa f f iillee ccaa lllleedd eemm pp llooyyeeee ww hh iicchh ccoonn tt aa iinn ss tt hh ee ssaa mm pp llee dd aa tt aa aa ss f f oollllooww ss:: EE00000011 :: R R aaddhhaa :: PPeer r ssoonnnneell :: 55000000 EE00000044 :: R R oo j jaa :: FFiinnaannccee :: 77000000
aa )) SSoorr tt tt hh ee f f iillee oonn tt hh ee eemm pp llooyyeeee dd eepp aa rr tt mm eenn tt aa nn dd dd iisspp llaa yy tt hh ee nn aa mm ee oof f tt hh ee eemm pp llooyyeeee aa nn dd dd eepp aa rr tt mm eenn tt SSoollnn :: ssoor r tt tt :: ++22 eemm p pllooyyeeee || ccuutt dd:: f f 22,,33 OO uu tt pp uu tt :: R R oo j jaa :: FFiinnaannccee R R aaddhhaa :: PPeer r ssoonnnneell
bb )) LL iisstt tt hh ee eemm pp llooyyeeeess ww hh oossee bb aa ssiicc pp aa yy bb eettww eeeenn 44000000 aa nn dd 66000000 SSoollnn :: ggr r ee p p [[44--55]][[00--99[[00--99]][[00--99]] eemm p pllooyyeeee OO uu tt pp uu tt :: EE00000011 :: R R aaddhhaa :: PPeer r ssoonnnneell :: 55000000
cc)) SSoorr tt tt hh ee f f iillee oonn tt hh ee eemm pp llooyyeeee nn aa mm ee iinn tt hh ee rr eevveerr ssee oorr dd eerr .. EE xxtt rr aa cctt tt hh ee ccoodd eess aa nn dd tthh eeiirr nn aa mm eess.. SSoollnn :: ssoor r tt r r tt :: ++11 eemm p p llooyyeeee || ccuutt dd:: f f 11,,22 OO uu tt pp uu tt :: EE00000011 :: R R aaddhhaa EE00000044 :: R R oo j jaa
dd )) LL iisstt tt hh ee dd uu pp lliiccaa tt eedd rr eeccoorr dd oof f aa f f iillee aa nn dd dd uu mm pp iitt tt oo f f iillee ccaa lllleedd dd uu pp lliiccaa tt ee.. SSoollnn :: ssoor r tt tt:: eemm p pllooyyeeee || uunniiqq --dd OO uu tt pp uu tt :: NNiill
ee)) SSoorr tt tt hh ee f f iillee oonn tthh ee eemm pp llooyyeeee ccoodd eess aa nn dd eexxtt rr aa cctt tt hh ee bb aa ssiicc aa nn dd nn aa mm eess oof f tt hh ee nn oonn --dd uu pp lliiccaa tt eedd lliinn eess..
SSoollnn :: ssoor r tt tt:: eemm p pllooyyeeee || uunniiqq uu || ccuutt dd:: f f 22,,44
-
7/29/2019 Unix Lab -II Sem Manual
10/35
9
SHELL PROGRAMMING INTRODUCTION
Shell - is not just a collection of commands but a programming language. You can automate a lotof task with it. The shell is very good for system administration tasks. Shell or the commandsinterpreter is a program that interprets your request to run the UNIX commands.Set of commands can be grouped together under a single filename and executed and this is doneusing shell scripts.
Types of shell:Bourne shell shC shell cshKorne shell ksh
Creation and execution of shell scripts using command line editor:
1. Creation$ cat > greetecho please enter your name: read name
echo hi! Welcome to this session $name ctrl + D
2. Execution$ sh greet
please enter your name: jayahi! Welcome to this session jaya
Shell has different types of variables.1. user defined variables2. Environment or predefined variables3. Local and global variables
User defined variables-The variables created by the user.$pet=rabbit$echo $pet
Environment variable It gives us a clear idea about the various setting of our terminal, the prompt that we are working in and the name of our home directory etc.,1. The Ps1 variable this variable contains the system prompt. (ex $)2. The HOME variable this contains the home directory where our login shell is initiallylocated after logging in.
-
7/29/2019 Unix Lab -II Sem Manual
11/35
10
3. PATH variable contains a list of filenames that are to be searched for UNIX commandsexecution.4. TERM variable determines the terminal type.5. LOGNAME variable has our login name.6. PS2 variable contains the subshell prompt.
Positional Parameters
The shell scripts can accept arguments from the command line. The arguments are namedas $1,$2,$3 etc..and are referred within the script using these names. Since they represent their
position they are called positional parameters. Note: The maximum number of positional parameters can be $9.
Shift command This command discards the first argument and renumbers the next.
Operators Arithmetic Operators provided by the shell are +, -, * and /
Note: precede * symbol by backslash(\)to make a difference from wildcard character (ex .\*5)
Logical Operators -a and-0 or ! not
Relational Operators- used for comparing integers within the shell scripts -eq :check for equality of integers.-ne :check for inequality-gt :check if one integer is greater than the other -lt :check if one integer is lesser than the other -le :check if one integer is lesser than or equal to the other -f :check if a file is an ordinary file
-d :check if a file is a directory-r :check if a file is readable-w :check if a file is writeable-x :check if a file is executable
Programming language construct1. a)if ..then.elsefi b)if..then..elif..elsefi
2. for..dodone 3. while..do..done4. case esac
1) if construct useful for executing a set of commands based on the condition being true and alternate set
of commands to be executed if the condition is false.Syntax:
If condition
-
7/29/2019 Unix Lab -II Sem Manual
12/35
11
ThenCommands
ElseCommandsfi
Eg.If(grep India countri.dat)Then
Echo pattern found Else
Echo pattern not found fi
2) for construct : used to perform same set of operations on a list of values.
Syntax: For variable in value1 value2 valu e3
DoCommands
Eg. For k in 1 2 3 4 5Do
Echo the number is $k Echo the square of the number is expr $k \*$k
Done
3) while construct : Repeatedly executing group of commands as long as the condition is true.
Syntax:While conditionDo
Command listDone
Eg: to print 3 numbers A=1
While [4a le 3]
Do Echo $aA= expr $a+1
Done
4) Case construct :Its useful for executing a set of commands based on the choices given by the user.
Syntax:case value in
choice 1) commands;;choice 2)commands;;
.
-
7/29/2019 Unix Lab -II Sem Manual
13/35
12
Esac
Eg. $echo enter a value Read myval
Case 4myval in0) echo zero;;1) echo one;;2) echo two;;3) echo three;;*) echo invalid argument;;
esac
-
7/29/2019 Unix Lab -II Sem Manual
14/35
13
SHELL PROGRAMMING I
Ex.No :2A Addition of Two numbers
Aim:To write a shell program to perform addition of two numbers.
Algorithm: 1.Read the two numbers.2.Add the two numbers.3.Display the result.
Program
//Shell Program for addition of two numbers
echo "Enter the value of A "read aecho "Enter the value of B "read bc=`expr $a \+ $b`echo "The sum of $a+$b=$c"
Output: $ sh add.shEnter the value of A10Enter the value of A20The sum of 10+20=30
Result: Thus, the shell program for addition of two numbers was executed and printed the result
successfully.
-
7/29/2019 Unix Lab -II Sem Manual
15/35
-
7/29/2019 Unix Lab -II Sem Manual
16/35
15
Result: Thus, the shell program to swap two numbers without using temporary variable is
executed and verified sucussefully.
Ex.No:2C Odd or Even
Aim: To write a shell program to check whether the given number is odd or even.
Algorithm: 1.Read the input2.Find the remainder value of n and store it into the variable c.3.If it is equal to zero, print the given number is even number else it is odd number.
Program
//Shell program for Odd or Even number
echo "Enter the N value"read nc=`expr $n \% 2`if test $c -eq 0thenecho "Even number"elseecho "Odd number"fi
Output: $sh odd.sh
Enter the N value10Even number $sh odd.shEnter the number
7Odd number
-
7/29/2019 Unix Lab -II Sem Manual
17/35
-
7/29/2019 Unix Lab -II Sem Manual
18/35
17
ResultThus, the shell program for largest among three numbers was executed and printed the
result successfully.
Ex.No:2E Students Performance DetailsAim:
To write a shell program to display students performance details using If-else statement.Algorithm:
1. Read the name of the student.2. Read the student marks in five subjects.3. calculate the total and average for the marks obtained.4. Print the name, total and average.5. Check if the average is greater than 75 then print Distinction .else go to step 5. 6. If average is greater than 60 then print first class.else go to step 6.7. If the average is greater than 50 then print second class.else goto step 7.8.if the average is below 50 print fail.
Programecho "Enter the student name"
read student_nameecho "Enter student Marks"read sub1 sub2 sub3 sub4 sub5total=`expr $sub1 + $sub2 + $sub3 + $sub4 + $sub5`average=`expr $total / 5`echo "The Student Name:$student_name"echo "The Total Marks:$total"echo "The Average Marks:$average"if [ $average -ge 75 ]then
echo "Grade is Distinction"
elif [ $average -ge 60 ]thenecho "Grade is First Class"
elif [ $average -ge 50 ]then
echo "Grade is second class"else
echo "Fail"fiOutput:
Enter the student name: manimozhi
Enter student Marks: 87 98 89 67 79
-
7/29/2019 Unix Lab -II Sem Manual
19/35
18
The Student Name: manimozhiThe Total Marks: 420The Average Marks: 84Grade is Distinction
Result: Thus, the shell program to display student performance details is executed and verifiedsuccessfully.
Ex.No:2F Arithmetic Operations Using CASEAim:
To write a shell program to perform all type of arithmetic operations such as addition,subtraction, etc.Algorithm:
1.Begin the program 2.Display message Enter t wo numbers3.Read the first and second value in variable a and b 4.Display message Enter your choice5.Display message Enter 1 for Addition 6.Display message Enter 2 for Subtraction 7.Display message Enter 3 for Multi plication 8.Display message Enter 4 fpr Division 9. Display message Enter 5 for Reminder10.Display message 5. Exit 11.Read the value in variable choice12 . Look at variable choice using case command13. if value of choice == 1
Compute c = value of a + value of bDisplay message The Sum is : and value of c
14. if value of choice == 2Compute c = value of a - value of bDisplay message The difference is : and value of c
15. if value of choice == 3Compute c = value of a * value of b
Display message The Product id: and value of c16. if value of choice == 4Compute c = value of a / value of b Display message The Quotient is : and value of c
17. if value of choice == 5Compute c = value of a % value of b Display message The Remainder is : and value of c
18. if value of choice == 5exit
19. Stop the program
-
7/29/2019 Unix Lab -II Sem Manual
20/35
19
Program
echo "Enter two numbers"read a becho "Enter your choice"
echo "Enter 1 for addition"echo "Enter 2 for subtraction"echo "Enter 3 for multiplication"echo "Enter 4 for division"read choicecase "$choice" in
1) c=`expr $a + $b`echo "The sum is $c";;
2) c=`expr $a - $b`echo "The difference is $c";;
3) c=`expr $a /* $b`echo "The product is $c";;
4) c=`expr $a / $b`echo "The quotient is $c";;
5) c=`expr $a % $b`echo "The reminder is $c";;
*) echo "Invalid choice";;esac
Output:$ sh scase.shEnter two numbers 5 4Enter your choiceEnter 1 for additionEnter 2 for subtractionEnter 3 for multiplicationEnter 4 for divisionEnter 5 for reminder 3The product is 20
Result: Thus, the shell program for arithmetic operations using CASE was executed and printed
the result successfully.
-
7/29/2019 Unix Lab -II Sem Manual
21/35
20
Ex.No:2G Sum of Series
Aim:
To write a shell program to print the sum of series.
Algorithm:1.Read the input value.2.Assign sum=o and I=13.Repeat the steps 4 until I value is greater than n.4.Add sum and i value and store it in summand then increment the I value.5.Display the result.
Program
//Sum of series 1 +2+3+.+N
echo "Enter the value of N"read nsum=0i=1until [ $i -gt $n ]dosum=`expr $sum \+ $i`i=`expr $i + 1`doneecho "The Sum of the series :$sum"Output: $ sh sum.shEnter the value of N5The Sum of the series :15
Result: Thus, the shell program for sum of series was executed and printed the result successfully.
-
7/29/2019 Unix Lab -II Sem Manual
22/35
-
7/29/2019 Unix Lab -II Sem Manual
23/35
22
Ex.No.2I Factorial of the given number
Aim:To write a shell program to find the factorial value of a given number.
Algorithm:1.Read the value of n.2.Assign n to temporary variable and fact =1.3.Check n is greater than 0 if it is true then repeat the step 4 until the n value is equal to 0.4.Multiply fact with n and assign the result to the variable fact and decrement the valueof n.5.Print the result.
Program
//Shell Program for Factorial of N Numbers
echo "Enter the value of N:"read ni=$n;fact=1while [ $n -gt 0 ]do
fact=`expr $fact \* $n`
n=`expr $n \- 1`doneecho "The factorial of $i is $fact"Output:$ sh fact.shEnter the value of N:5The factorial of 5 is 120
Result Thus, the shell program for factorial number was executed and printed the result
successfully.
-
7/29/2019 Unix Lab -II Sem Manual
24/35
23
Ex.No:2J Fibonacci Series
Aim:To write a shell program to print the Fibonacci series of a given number.
Algorithm:1.Read the input value.2.Initialize the value of a=-1,b=1,j=03.Check j is less than n if it is true then repeat the steps 4,5 until the loop becomes false.4.Add the value of a and b and store it in c5.Print the c value. Then assign the b value to a and c value to b
Program
//Shell Program for Fibonacci number generationecho "Enter the value of N"read na=-1
b=1 j=0while [ $j -lt $n ]do
c=`expr $a + $b`echo $ca=$b
b=$c
j=`expr $j + 1`done
Output: $ sh fib.shEnter the value of N3011
Result: Thus, the shell program for fibonacci series was executed and printed the resultsuccessfully.
-
7/29/2019 Unix Lab -II Sem Manual
25/35
24
Ex.No:2K Armstrong NumberAim:
To write a shell program to check whether the given number is odd or even.
Algorithm:
1.Start the program.2.Read the value of num.3. Initialize the value of variable sum.4.Check the condition that num gt 0 then goto step 5 else goto step 9.5.Calculate the variable of y num%10.6. Calculate the variable of z y*y*y.7. Calculate sum sum+z.8. Calculate num num/10.9. Check the condition whether x eq num. If yes then goto step 10 else goto
step 11.10. Print the given number is an armstrong number.11. Print the given number is not an armstrong number.12. Stop the program.
Program
echo "Enter a number"read numx=$numsum=0while [ $num -gt 0 ]
doy=`expr $num % 10`z=`expr $y \* $y \* $y`sum=`expr $sum + $z`num=`expr $num / 10`
doneif [ $x -eq $sum ]then
echo "$x is an armstrong number"else
echo "$x is not an armstrong number"
fi
Output:$ sh armstrong.shEnter a number: 153153 is an armstrong number
Result: Thus, the shell program for Armstrong number was executed and printed the result successfully.
-
7/29/2019 Unix Lab -II Sem Manual
26/35
25
Ex.No:2L PalindromeAim:
To write a shell program to find the given string is palindrome or not.
Algorithm:
1. Enter the string.2. Read the string.3. calculate the length of the string.4. Check whether the length of the string is greater than 0.5. If step 4 is true do the following expression.
temp=str|cut c $len.rev=print rev temp.len=len-1.
6.Print the reversed string in rev.7. Check if str and rev are same if it is true print string is palindrome else print not
Not a palindrome.Program
echo "Enter the word"read str len=`echo $str|wc -c`while [ $len -gt 0 ]do
temp=`echo $str|cut -c$len`rev=`echo $rev$temp`len=`expr $len - 1`
doneecho "The reversed string is $rev"if [ $str = $rev ]then
echo "The given word is palindrome"else
echo "The given word is not palindrome"fi
Output:$ sh sreverse.sh
Enter the word: madamThe reversed string is madamThe given word is palindrome$ sh sreverse.shEnter the word: idhayaThe reversed string is ayahdiThe given word is not palindrome
Result: Thus, the shell program to find the given string is palindrome or not is executed and
verified sucussefully.
-
7/29/2019 Unix Lab -II Sem Manual
27/35
-
7/29/2019 Unix Lab -II Sem Manual
28/35
27
sort(m,x)int m,x[];{int i,j,t;
for(i=1;i
-
7/29/2019 Unix Lab -II Sem Manual
29/35
28
Ex.No:3B Factorial using recursive functionAim:
To write a C program in vi editor to find the factorial of a given number using recursivefunction.
Algorithm:
1. Start the program.2. Display message Enter the limit 3. Read the first value in variable n 4. Call the function recur(n)5. Display message Factorial of given number is: and the value of a6. Stop the program.Algorithm for the function recur:1.if the num is 1 then return 12. Else return num*recur(num-1) to the main function
Program
#includeint main(){int n,a;clrscr();
printf("Enter the number:");scanf("%d",&n);a=recur(n);
printf("\nFactorial of the given number %d is %d",n,a);}int recur(int num){int fact=1;if(num==1)return(1);elsefact=num*recur(num-1);return(fact);}
Output:$cc fact.c$./a.outEnter the number: 6Factorial of the given number 6 is 720
Result:Thus the C program to find factorial using recursive function has been executed and the
output is verified.
-
7/29/2019 Unix Lab -II Sem Manual
30/35
29
Ex.No:3C Swapping of two numbers using pointersAim:
To write a C program in vi editor to swap two numbers using pointers.Algorithm:
1.Begin the program2.Display message Enter the values of x and y 3.Read the first and second value in variable x and y 4.Call the function change by passing by reference swap(&x,&y)5.Print the values of the variables after swap6.Stop the program
Algorithm for the function change(int*a,int *b)1.Declare k as an integer variable 2.Assign the following
k=*a;*a=*b;*b=k;
3. Print the values of the variables after swap
Program
#includemain(){int x,y,change (int* , int*);
printf("\n enter the values of x and y:");scanf("%d%d",&x,&y);change(&x,&y);
printf("\n In main() :x=%d y=%d",x,y);return 0;}change(int *a,int *b){int k;k=*a;
*a=*b;*b= k;
printf("\n In Change() : x=%d y=%d",*a,*b);}Output:$cc swap.c$./a.outenter the values of x and y: 2 3In main() :x=3 y=2In Change() : x=3 y=2
Result:Thus the C program to swap two numbers using pointers has been executed and the output
is verified.
-
7/29/2019 Unix Lab -II Sem Manual
31/35
30
Ex.No:3D Dynamic Storage Allocation
Aim:To write a C program in vi editor to store a character string in a block of memory space
Created by malloc and then modify the same to store a large string.Algorithm:
1.Start the program.2.Using malloc function create a memory space for file pointer buffer.3.Print the size of the buffer.4.Using Strcpy store a character string in a block of memory space allocated.5. To modify the memory space use realloc function.6. Using strcpy store the large string in a reallocated memory space.7. Free the memory space allocated.8. Stop the program.
Program
M , mm mm m
#include#include#define NULL 0int main(){
char *buffer;/*allocating memory*/
if((buffer=(char *)malloc(10))==NULL){ printf("malloc failed\n");exit(1);
} printf("buffer of size %d created\n",_msize(buffer));strcpy(buffer,"HYDERBAD");
printf("\n buffer contains:%s\n",buffer);/* reallocation*/
if((buffer=(char*)realloc(buffer,15))==NULL){ printf("reallocation failed\n");exit(1);
} printf("\n buffer size modified\n"); printf("\n buffer still contains: %s\n",buffer);strcpy(buffer,"SECUNDERABAD");
printf("\n buffer now contains: %s\n",buffer);/*freeing memory*/free(buffer);
}
-
7/29/2019 Unix Lab -II Sem Manual
32/35
31
Output:$cc dynamic.c$./a.out
Buffer of size 10 createdBuffer contains:HYDERABADBuffer size modifiedBuffer still contains:HYDERABADBuffer now contains:SECUNDERABAD
-
7/29/2019 Unix Lab -II Sem Manual
33/35
-
7/29/2019 Unix Lab -II Sem Manual
34/35
33
Ex.No:3F SEPARATING EVEN AND ODD NUMBERS USING FILES
Aim:To write a program to separate even and odd numbers using files.
Algorithm:1.Start the program.2.Open a file pra.dat in write mode.3.Input the numbers until end of file.4.Close the file.5.Open two files even.dat,odd.dat in write mode.6.Open the file pra.dat in read mode.7. Find the remainder of each number when divided by 2.8. If it is zero copy the number in even.dat else in odd.dat.9. Repeat the steps 5& 6 until end of file.10.Close the files.11. Open the files even.dat and odd.dat in read mode.12. read the contents of the file and print them.13.Repeat step 12 until end of file.14.Close the files.15. Stop the Program.
Program #includeint main(){FILE *fp,*fpo,*fpe;int c,d;fp=fopen("pra.dat","w");
printf("\n\t Enter the numbers");while((scanf("%d",&c))!=EOF)
putw(c,fp);fclose(fp);fp=fopen("pra.dat","r");fpe=fopen("even.dat","w");fpo=fopen("odd.dat","w");while((c=getw(fp))!=EOF){
d=c%2;if(d==0) putw(c,fpe);else
putw(c,fpo);}fclose(fp);fclose(fpo);fclose(fpe);fpe=fopen("even.dat","r");
printf("\n the Even Numbers are");
while((c=getw(fpe))!=EOF)
-
7/29/2019 Unix Lab -II Sem Manual
35/35
printf("\n%d",c);fclose(fpe);fpo=fopen("odd.dat","r");
printf("\n The odd Numbers are");
while((c=getw(fpo))!=EOF) printf("\n%d",c);fclose(fpo);}
Output:$cc feo.c$./a.outFile handling
pra.dat1 6 3 16 10 19 21 24 23 100even.dat
6 16 10 24 100odd.dat
1 3 19 21 23The even numbers are
6 16 10 24 100The odd numbers are
1 3 19 21 23
Result: Thus, the program to separate even and odd numbers using files is executed and verifiedsuccessfully.