SHELL PROGRAMMING
Ex.NO:2
AREA OF THE CIRCLE
Aim: To write a shell program to find the area of the circle. procedure: Step 1: Start the Process Step 2: Read the radius Step 3: Calculate the area. Step 4: Print the Radius and Area Step 5: Stop the process
Flow Chart:
Program
echo "Enter the radius of the circle" read r area=$(echo "scale=2; 3.14 * ($r * $r)" |bc ) echo "Radius of the circle $r" echo "Area of the circle $area"
Output:
Enter the radius of the circle 5 Radius of the circle 5 Area of the circle 78.50
Result : Thus the program to find the area of the circle is executed successfully.
Ex.NO:3.
CALCULATING GROSS SALARY OF AN EMPLOYEE
Aim: To write a shell program to calculate Gross salary of an employee. Procedure: step1: Start the process step2: Read the basic step3: calculate da and hra step4: calculate the gross step5: Print the Gross salary step6: Stop the process
Flow Chart:
Program: echo "Basic Salary" read basic da=`echo $basic \* 40/100 |bc` hra=`echo $basic \* 20/100 |bc` gross=`echo $basicv + $da + $hra |bc` echo "Grosssalary=Rs.$gross"
Output: [dha@localhost ~]$ sh sal.sh Basic Salary 5000 Grosssalary=Rs.8000
Result: Thus the program to find the gross salary of an employee is executed successfully.
Ex.NO:4
UPPER TO LOWER AND LOWER TO UPPER USING CASE STATEMENT
Aim: To Write a Shell Program to convert Upper to lower and lower to Upper using case statements.
Algorithm: Step 1: Start the process Step 2: Read the choice Step 3: Read the file to be converted Step 4: Convert the case using tr [:lower:] [:upper:]
Step 5: Print the result Step 6: Stop the process
Flow Chart:
Start
Display menu
1. Lower to upper 2. Upper to lower
Case?
Choice
End Program: echo "Menu" echo "1.Lower->Upper" echo "2.Upper->Lower" echo "Enter your Choice" read ch case $ch in 1)echo "Enter the file name" read f tr [:lower:] [:upper:]<$f;; 2)echo "Enter the file name" read f tr [:upper:] [:lower:]<$f;; esac
Output: [dha@localhost ~]$ sh conver.sh
Menu 1.Lower->Upper 2.Upper->Lower Enter your Choice 1 Enter the file name f1 HAI HELLO [dha@localhost ~]$ sh conver.sh Menu 1.Lower->Upper 2.Upper->Lower Enter your Choice 2 Enter the file name f1 hai hello
Result: Thus the Shell Program to convert Upper to Lower and Lower to Upper using case statements is executed successfully.
Ex.NO:5
ARITHMETIC OPERATIONS USING CASE STATEMENTS
Aim:
To Write a Shell Program to perform Arithmetic Operations using case statements
Algorithm: Step 1: Start the process Step 2: Read two numbers Step 3: Read the choice Step 4: Perform the arithmetic Operations Step 5: Print the result Step 6: Stop the process.
Flow Chart
Start
Read the limilt
Initialize a=-1, b=1, i=1
While (i<=n)
No
Yes
c=a+b
Print c a=b b=c
i=i+1
Stop
Program: echo "A Simple Calculator" echo "Enter 1 for Addition, 2 for Subtraction,\n" echo "3 for Multiplication, 4 for Division and 5 for Exit\n" echo "Enter Two Numbers" read a b echo "Enter The Choice of Operation" read choice case $choice in 1) res=`expr $a + $b`;; 2) res=`expr $a - $b`;; 3) res=`expr $a * $b`;; 4) res=`expr $a / $b`;; 5) exit;; esac echo "Result= $res"
Output: [dha@localhost ~]$ sh case.sh A Simple Calculator Enter 1 for Addition, 2 for Subtraction,\n 3 for Multiplication, 4 for Division and 5 for Exit\n Enter Two Numbers 73 Enter The Choice of Operation 2 Result= 4
Result : Thus the Arithmetic operations using case is executed successfully.
Ex.NO:6
POSITIVE OR NEGATIVE OR ZERO
Aim: To Write a Shell Program to find the given number is Positive or Negative or Zero using if condition
Algorithm: Step1: Start the process Step2: Read the number. Step3: if number is greater than 0
a. Number is positive
Else if number is lesser than 0
b. Number is negative Else c. Number is Zero
Step4: Print the result. Step5: Stop the process
Flow Chart
Program: echo "Enter a Number" read n if test $n -gt 0 then echo "$n is Positive" elif test $n -lt 0 then echo "$n is Negative" else echo "Zero" fi
Output:-
[dha@localhost ~]$ sh pos.sh Enter a Number 4 4 is Positive [dha@localhost ~]$ sh pos.sh Enter a Number 0
Zero [dha@localhost ~]$ sh pos.sh Enter a Number -3 -3 is Negative
Result : Thus the shell program to find the given number is positive or negative or is executed successfully.
Ex.NO:7 Aim:
FIBONACCI SERIES
To Write a Shell Program to find the Fibonacci Series of a given number using while loop. Procedure: Step1: Start the process Step2: Read the range Step3: while i less than range. c=a+b; i=i+1; Step4: print the result Step 5: Stop the process.
Flow Chart
Program: echo "program to generate Fibonacci Series" echo "enter the range" read n a=0 b=1 echo "Fibonacci series" echo $a echo $b i=2 while test $i -lt $n do c=`expr $a + $b` echo $c i=`expr $i + 1` a=$b b=$c done Output: [dha@localhost ~]$ sh fib.sh program to generate Fibonacci Series enter the range 7 Fibonacci series 0
1 1 2 3 5 8
Result: Thus the Shell Program to find the Fibonacci Series of a given number using while is executed successfully.
Ex.NO:8
SORTING THE CONTENT OF THE FILE
Aim: To Write a Shell Program to sort the given names in a file using while loop. Algorithm:
Step 1: Start the process Step 2: Get the File Name Step 3: Read the Number of Names Step 4: Read the Names Step 5: Use the Sort function Step 6: Display the result Step 7: Stop the process
Flow Chart
Program:echo "Enter the file Name" read x echo "Enter the number of names" read num echo " Enter $num strings" while test $num -gt 0 do read name echo $name>>$x num=`expr $num - 1` done echo "the storted names are:" sort $x rm $x
Output
enter the file name empnames enter the number of names 4 enter 4 strings
srija latha asmi keerthi the sorted names are: asmi keerthi latha srija
Result: Thus the shell program to sort the names is executed successfully.
Ex.NO:9
FACTORIAL OF A GIVEN NUMBER
Aim: To Write a Shell Program to find the Factorial of a given number
Procedure: Step 1: Start the process. Step 2: Read the input Step 3: if the input is 1 Then factorial is 1 else Multiply the successive numbers. Step 4: Display the Result Step 5: Stop the process
Flow Chart
Program: echo "FACTORIAL OF A NUMBER" echo "Enter a number" read n i=1 f=1 if test $n -eq 0 then echo "factorial is 1" else while test $i -le $n
do f=`expr $f \* $i` i=`expr $i + 1` done echo "Factorial is $f" fi
Output: [jp@TRPLinuxserver ~]$ sh fact.sh FACTORIAL OF A NUMBER Enter a number 5 Factorial is 120
Results: Thus the shell program to find the factorial of a given number is executed successfully.
Ex.NO:10 Aim:
PRIME NUMBER
To Write a Shell Program to check whether the given number is Prime number or not. . Procedure: Step 1: Start the process Step 2: Read the number. Step 3: Calculate the number is divided by the same number without any
Remainder,then it is prime. Step 4: Display the result Step 5: Stop the process
Flow Chart
Program i=2 rem=1 echo -e "Enter a number: \c" read num if [ $num -lt 2 ]; then echo -e "$num is not prime\n" exit 0 fi while [ $i -le `expr $num / 2` -a $rem -ne 0 ]; do rem=`expr $num % $i` i=`expr $i + 1` done if [ $rem -ne 0 ]; then echo -e "$num is prime\n" else echo -e "$num is not prime\n" fi Output
Enter a number: 5 5 is prime Enter a number: 22 22 is not prime
Results: Thus the shell program to find the given number is prime executed successfully.
Ex.NO:11 Aim:
MULTIPLICATION TABLE
To Write a Shell Program to generate the Multiplication table of a given number using for loop.
Algorithm: Step 1: Start the process Step 2: Read the number Step 3: for 1 to 10 Multiply the number. Step 4: Display the result Step 5: Stop the process
Flow Chart
Program:
echo "Enter the Number" read n for i in 1 2 3 4 5 6 7 8 9 10 do m=`expr $n \* $i |bc` echo $i*$n=$m done Output: [dha@localhost ~]$ sh mul.sh Enter the Number 5 1*5=5 2*5=10 3*5=15 4*5=20 5*5=25 6*5=30 7*5=35 8*5=40 9*5=45 10*5=50
Results: Thus the multiplication table is executed successfully.
Ex.NO:12 Aim:
SEARCH THE CONTENT IN A FILE USING GREP
To write a shell program to search the contents of a file.. Algorithm: 1. Start the Program. 2. Enter the Pattern to be searched. 3. Enter the file to be searched.. 4. If grep pname in fl name..
5. Pattern found. 6. Otherwise Pattern not found . 7. Stop the Program . Flow chart :
Program: echo "Enter the Pattern to be searched : \c" read pname echo "Enter the file to be used : \c" read flname echo "Searching for $pname from file $flname" if grep "$pname" $flname
then echo "Pattern found" else echo "Pattern not found" fi
Output: [dha@localhost ~]$ sh grep.sh Enter the Pattern to be searched : \c read Enter the file to be used : \c sample Searching for read from file sample read Pattern found
Result: Thus the content are searched in the specified file and the result is displayed.
Ex.NO:13 Aim:
USAGE OF AWK FILTER
To Write a Shell Program to print the average pay for an employee using awk filter. Algorithm: 1. Star the Program. 2. Get the Employee abstract from the user. 3. Compile and execute the program. 4. Display the average basic pay.
5. Stop the Program.
Flow Chart:
Program: BEGIN { printf "\t\tEmployee abstract\n\n" } $6 > 7000 { kount++ ; tot += $6 printf "%3d %-20s %-12s %d\n" , kount,$2,$3,$6 } END { printf "\n\tThe average basic pay is %6d\n", tot/kount }
Output: Employee abstract 1 Mano 2 Asmi 3 Bala Staff Lect H.O.D 15000 20000 30000
The average basic pay is 21666
Result: Thus the Shell Program to print the average pay for an employee using awk filter is executed successfully.
Ex.NO:14 Aim:
SECURITY COMMAND
To Write a Shell Program to change the Access Mode of a given File. Algorithm: 1. Start the Program. 2. Enter the file name. 3. Enter the mode. 4. Enter the Permission Mode. 5. Enter the operation. 6. Display the Output. 7. Stop the Program.
Flow Chart:
Start START . Enter the File Name
read f
Enter the operation
chmod $u$p$o $f
STOP
Program: echo "Enter the file name:" read f echo "Enter mode" read u echo "Enter Permission mode:" read p echo "Enter Operation" read o chmod $u$p$o $f ls -l $f
Output: Enter the file name: sample Enter mode g Enter Permission mode: = Enter Operation w -rw--w-r-- 1 22 Feb 26 18:15 sample
Result: Thus the Shell Program to change the Access Mode of a given File is executed successfully.
C PROGRAMS
Ex.NO:15
PALINDROME
Aim: To Write a C Program to check the given number is palindrome or not. Algorithm: Step 1: Start the Program. Step 2: Get the number from the user. Step 3: Split up the given number using model operation. Step 4: Store the split values in reverse order. Step 5: If both the numbers match, display as palindrome else display not a palindrome. Step 6: Stop the program Flow Chart
that it
is
Program: #include<stdio.h> main() { int a,b=0,c=0,e; printf("Enter the number"); scanf("%d",&a); e=a; while(a>0) { b=a%10; c=c*10+b; a=a/10; } if(e==c) printf("\nPalindrome\n"); else printf("\nNot a Palildrome\n"); }
Output: [jp@TRPLinuxserver ~]$ vi pali.c [jp@TRPLinuxserver ~]$ cc pali.c [jp@TRPLinuxserver ~]$ ./a.out Enter the number121 Palindrome [jp@TRPLinuxserver ~]$ vi pali.c [jp@TRPLinuxserver ~]$ cc pali.c [jp@TRPLinuxserver ~]$ ./a.out Enter the number156 Not a Palildrome
Result: Thus the program to check the given number is palindrome or not executed successfully.
Ex.NO:16
SWAP THE GIVEN TWO VALUES USING FUNCTIONS
Aim: To Write a C Program to swap the given two values using functions. Algorithm: Step 1: Start the Program. Step 2: Get the two Values from the user. Step 3: Swap the a & b Values. Step 4: Display the After swapping Values. Step 5: Stop the program.
Flow Chart
Program: #include<stdio.h> main() { void swap1(void); swap1(); } void swap1() { int a,b,c; printf("Enter two numbers:"); scanf("\n %d%d",&a,&b);
c=a; a=b; b=c; printf("The Numbers after swaping...%d%d\n",a,b); }
Output: [dha@localhost ~]$ cc swap1.c [dha@localhost ~]$ ./a.out Enter two numbers:7 9 The Numbers after swaping...97
Result: Thus the C program to swap the given number has been executed successfully.
Ex.NO:17
USAGE OF POINTERS
Aim: To Write a C program to add two numbers using Pointers..
Algorithm:
Step 1: Step 2: Step 3: Step 4: Start the program. Read the two numbers from the user a&b. s=sum (&a,&b). Display the sum of the Values.
Step 5: Stop the program
Flow Chart
Program: #include<stdio.h> main() { int sum(int *,int *); int a,b,s; printf("\nEnter the value of a : "); scanf("%d",&a); printf("\nEnter the value of b : "); scanf("%d",&b); s=sum(&a,&b); printf("\nThe sum of the values are : %d\n",s); return; } int sum(int *x,int *y) { return(*x+*y); } Output: [dha@localhost ~]$ cc poin.c [dha@localhost ~]$ ./a.out Enter the value of a : 4
Enter the value of b : 9 The sum of the values are : 13
Result: Thus the C program to print the value and address of a given number has been executed successfully
Ex.NO:18
USAGE OF STRUCTURES
Aim: To Write a C Program to print the Students Name, Marks using Structure. Algorithm: Step 1: Start the Program. Step 2: Get the number of Students from the user. Step 3: Enter the students Details. Step 4: Enter the Marks. Step 5: Display the Students Record. Step 6: Stop the program. Flow Chart
Program: #include<stdio.h> struct student { char name[10][10]; int rollno,m1,m2,m3,total;
}; void main() { int num,i,j; struct student s1[10]; printf("enter the number of students"); scanf("%d",&num); for(i=0;i<num;i++) { printf("Enter the roll number\n"); scanf("%d",&s1[i].rollno); printf("Enter the name \n"); scanf("%s",&s1[i].name); printf("Enter the mark1\n"); scanf("%d",&s1[i].m1); printf("Enter the mark2\n"); scanf("%d",&s1[i].m2); printf("Enter the mark3\n"); scanf("%d",&s1[i].m3); s1[i].total=s1[i].m1+s1[i].m2+s1[i].m3; } printf("The details of the mark list is as follows \n"); printf("\nRollno"); printf("\tname "); printf("\tmark1"); printf("\tmark2"); printf("\tmark3"); printf("\ttotal"); printf("\n"); for(i=0;i<num;i++) { printf("\n%d",s1[i].rollno); printf("\t%s",s1[i].name); printf("\t%d",s1[i].m1); printf("\t%d",s1[i].m2); printf("\t%d",s1[i].m3); s1[i].total=s1[i].m1+s1[i].m2+s1[i].m3; printf("\t%d",s1[i].total); } }
Output: [dha@localhost ~]$ cc stru.c [dha@localhost ~]$ ./a.out enter the number of students2 Enter the roll number 11 Enter the name vino Enter the mark1 78 Enter the mark2 90 Enter the mark3 86 Enter the roll number 12 Enter the name surya Enter the mark1 67 Enter the mark2 87 Enter the mark3 96 The details of the mark list is as follows Rollno name mark1 mark2 mark3 total 11 12 vino surya 78 67 90 87 86 96 254 250
RESULT: Thus the C Program to print the Students Name, Marks using Structure
Ex.NO:19
LARGEST AND SMALLEST NUMBER USING ARRAY
Aim: To Write a C Program to find the Largest and Smallest numbers using array
Algorithm: Step 1: Step 2: Step 3: Step 4: Start the Program. Enter the number of element. if a[r]> large. Print the largest element.
Step 5: if a[r] <small. Step 6: Print the Smallest element Step 6: Stop the program.
Flow Chart
Program: #include<stdio.h> void main() { int a[10],i,j,x,max,min; printf("Enter 10 nos:"); for(x=0;x<10;x++) { scanf("%d",&a[x]); } max=a[0]; for(i=0;i<10;i++) { if(max<a[i+1]) max=a[i+1]; } printf("largest no is %d",max); min=a[0]; for(j=0;j<10;j++) { if(min>a[j+1]) min=a[j+1]; } printf("\nsmallest no is %d",min); } Output: [jp@TRPLinuxserver ~]$ vi largesma.c [jp@TRPLinuxserver ~]$ cc largesma.c [jp@TRPLinuxserver ~]$ ./a.out Enter 10 nos:10 20 60 5 89 55 45 23 11 21
largest no is 89 smallest no is 5 Result: Thus the program to find the largest and smallest using array has been executed successfully using array.
Ex.NO:20
COMMAND LINE ARGUMENTS
Aim: To Write a C Program to print the command line arguments. Program: #include<stdio.h> int main(int argc, char **argv) { int counter; puts("The Arugements to the Program are: "); for(counter=0;counter<argc;counter++) puts(argv[counter]); return 0; } Output: [dha@localhost ~]$ cc comm.c [dha@localhost ~]$ ./a.out The Arugements to the Program are: ./a.out one two three ./a.out One two three
Result: Thus the program to print the command line arguments is executed successfully.
Ex.NO:21
FILE HANDLING
Aim: To write a C Program to print the Mark sheet of n Students using File Handling. Algorithm: Step 1: Step 2: Step 3: Step 4: Start the Program. Enter the file name.
Enter the student details Enter the no. of records.
Step 5: Enter the student details. Step 6: open the file. Step 7. Print the details. Step 8. Stop the Program.
Flow Chart
Program:
#include<stdio.h> main() { FILE *fp1; int rno,m1,m2,m3,n,i,tot,avg;
char fname[10],sname[20]; printf("\n Enter the file name:"); scanf("%s",fname); fp1=fopen(fname,"W"); printf("\n Enter the student details"); printf("\n Enter the no.of records:"); scanf("%d",&n); for(i=1;i<=n;i++) { printf("\n Enter the rollno\t name\t mark1\t mark2\t mark3\t:\n"); fscanf(stdin,"%d%s%d%d%d",&rno,sname,&m1,&m2,&m3); tot=m1+m2+m3; avg=tot/3; fprintf(fp1,"%d\t%s\t%d\t%d\t%d\t%d\t%d\n",rno,sname,m1,m2,m3,tot,avg); } fclose(fp1); fprintf(stdout,"\n\n"); fp1=fopen(fname,"r"); printf("\n rollno Name Mark1 Mark2 Mark3 Total Aveerage\n"); for(i=1;i<n;i++); { fprintf(fp1,"%d\t%s\t%d\t%d\t%d\t%d\t %d \n",rno,sname,m1,m2,m3,tot,avg); } fclose(fp1); } }
Output: [dha@localhost ~]$ cc mano.c [dha@localhost ~]$ ./a.out mano.c
Enter the no. of students:2 Enter SPRNO:786 Enter student name:Asmi Enter Marks: Enter Maths Mark = 100 Enter Lab Mark = 98 Enter SPRNO:794 Enter student name:Vino Enter Marks: Enter Maths Mark = 90 Enter Lab Mark = 95
[dha@localhost ~]$ vi z:marklist.txt Student Marklist
SPRNO Name M1 M2 Total 786 Asmi 100 98 198 794 Vino 90 95 185
Result: Thus the program to print the file content is executed successfully.
Ex.NO:22
DEMONSTRATION OF FORK
Aim: To Write a C Program to represent Parent and Child Process Id using Fork. Algorithm: Step 1: Step 2: Step 3: Step 4: Start the Program. Enter the initial id.
if id==0
Print Return value of fork to child.
Step 5: Print Return value of fork to Parent. Step 6. Stop the Program
Flow Chart
Program:
#include<stdio.h> main() { int id; id=fork(); if(id==0) { printf("\n Return value of fork to child : %d",id); printf("\n Parent process id : %d",getppid()); printf("\n Child process id : %d",getpid()); }
else { printf("\n Return value of fork to parent : %d",id); printf("\n Parent process id : %d",getppid()); printf("\n Child process id : %d\n",getpid()); } }
Output: [dha@localhost ~]$ cc fork.c [dha@localhost ~]$ ./a.out fork.c
Return value of fork to child : 0 Parent process id : 3473 Child process id : 3474 Return value of fork to parent : 3474 Parent process id : 3330 Child process id : 3473 Parent: 9 Child: 9
Result: Thus the program represent Parent and Child Process Id using Fork is executed successfully.
Ex.NO:22
SIGNALS HANDLING
Aim:
To write a program for Signal Handling.
Algorithm:
Step 1: Step 2: Step 3: Step 4: Start the Program. Declare the variable. The signal (SIGNT, change) Display the signal with no interrupt Give the Interrupt to the signal.
Step 5: Stop the Program.
Flow Chart:
Program:
#include<stdio.h> #include<signal.h> main() { void change(); int i; signal(SIGINT,change); for(;;) { printf("\n No Interruption..."); sleep(2); } } void change() { static int cnt=0; cnt=cnt+1; signal(SIGINT,change); printf("\n %d Time Interrupted...",cnt); if(cnt==3) signal(SIGINT,SIG_DFL); }
Output: [dha@localhost ~]$ cc signal.c [dha@localhost ~]$./a.out signal.c
No Interruption... No Interruption... No Interruption... 1 Time Interrupted... No Interruption... No Interruption... No Interruption... 2 Time Interrupted... No Interruption... No Interruption... No Interruption... 3 Time Interrupted... No Interruption...
Result:
Thus the program for Signal Handling is executed and verified.