Tuesday, 12 July 2016

SHELL UNIX LINUX PROGRAM TO IMPLEMENT SIGNAL

#include <stdio.h>     
#include <unistd.h>    
#include <signal.h>    
void catch_int(int sig_num)
{
    signal(SIGINT, catch_int);
    printf("Don't do that\n");
    fflush(stdout);
}

int main(int argc, char* argv[])
{
    /* set the INT (Ctrl-C) signal handler to 'catch_int' */
    signal(SIGINT, catch_int);

    /* now, lets get into an infinite loop of doing nothing. */
    for ( ;; )
        pause();
}


Output:

[csuser@localhost ~]$ vi sig.c
[csuser@localhost ~]$ gcc sig.c
[csuser@localhost ~]$ ./a.out
^CDon't do that
^Z
[1]+  Stopped                 ./a.out

SHELL UNIX-LINUX PROGRAM TO CONVERT LOWER CASE TO UPPER CASE

#include <stdio.h>
#include <ctype.h>

void main()
{
    char sentence[100];
    int count, ch, i;

    printf("Enter a sentence \n");
    for (i = 0; (sentence[i] = getchar()) != '\n'; i++)
    {
        ;
    }
    sentence[i] = '\0';
    /*  shows the number of chars accepted in a sentence */
    count = i;
    printf("The given sentence is   : %s", sentence);
    printf("\n Case changed sentence is: ");
    for (i = 0; i < count; i++)
    {
        ch = islower(sentence[i])? toupper(sentence[i]) :
tolower(sentence[i]);
        putchar(ch);
    }





Output:

[csuser@localhost ~]$ gcc l.c
[csuser@localhost ~]$ ./a.out
Enter a sentence
kishan
The given sentence is   : kishan
 Case changed sentence is: KISHAN

SHELL UNIX-LINUX IMPLEMENTATION OF COPY COMMAND

#include<stdio.h>
int main(){
  FILE *p,*q;
  char file1[20],file2[20];
  char ch;
  printf("\nEnter the source file name to be copied:");
  gets(file1);
  p=fopen(file1,"r");
  if(p==NULL){
      printf("cannot open %s",file1);
      exit(0);
  }
  printf("\nEnter the destination file name:");
  gets(file2);
  q=fopen(file2,"w");
  if(q==NULL){
      printf("cannot open %s",file2);
      exit(0);
  }
  while((ch=getc(p))!=EOF)
      putc(ch,q);
  printf("\nCOMPLETED");
  fclose(p);
  fclose(q);
 return 0;
}



Output:

[csuser@localhost ~]$ vi c.c
[csuser@localhost ~]$ gcc c.c
[csuser@localhost ~]$ ./a.out

Enter the source file name to be copied: file1

Enter the destination file name: file2

COMPLETED

SHELL UNIX-LINUX PROGRAM TO IMPLEMENT PIPES USING GREP

echo "Enter file name:"
read fname
echo "Enter expression to search:"
read exp
echo "Output of grep is :"
grep $exp $fname
echo "Total lines containing $exp in $fname is"
grep $exp $fname | wc –l

output:

[csuser@localhost ~]$ vi gp
[csuser@localhost ~]$ chmod 777 gp
[csuser@localhost ~]$ ./gp
Enter file name:
k
Enter expression to search:
s

Output of grep is :
gdsgsg
sdfgsdgsdg
dfgsdgsg
gsdgsg
Total lines containing s in k is
4

SHELL UNIX-LINUX PROGRAM TO IMPLEMENT CASE STRUCTURE

echo "1. Addition"

echo "2. Subtraction"

echo "3. Multiplication"

echo "4. Division"

echo "Please enter your choice:"

read choice

echo "Enter the first value:"

read a

echo "Enter the second value:"

read b

case $choice in
1) sum=`expr $a + $b`
echo "Sum of $a and $b is " $sum
;;

2) diff=`expr $a - $b`
echo "Difference of $a and $b is " $diff
;;

3) prod=`expr $a \* $b`
echo "Product of $a and $b is " $prod
;;

4) div=`expr $a / $b`
echo "Divions of $a and $b is " $div
;;

*) echo "Invalid option selected!"

Esac


program to implement case structure

Output:

[csuser@localhost ~]$ vi ma
[csuser@localhost ~]$ chmod 777 ma
[csuser@localhost ~]$ ./ma
1. Addition
2. Subtraction
3. Multiplication
4. Division
Please enter your choice:
1
Enter the first value:
2
Enter the second value:
3
Sum of 2 and 3 is  5

SHELL UNIX-LINUX PROGRAM TO IMPLEMENT IF THEN ELSE

echo "Enter marks in subject1"
read m1
echo "Enter marks in subject2"
read m2
echo "Enter marks in subject3"
read m3
sum=`expr $m1 + $m2 + $m3`
echo "Sum is = " $sum
percentage=`expr $sum / 3`
if test $percentage -ge 70
then
echo "You have got a distinction!"
elif test $percentage -ge 60
then
echo "You have got a first class!"
elif test $percentage -ge 40
then
echo "You are qualified!"
else
echo "You have failed!"
fi
program to implement if then else



Out put:

[csuser@localhost ~]$ vi ra
[csuser@localhost ~]$ chmod 777 ra        (ra is file name)
[csuser@localhost ~]$ ./ra
Enter marks in subject1
23
Enter marks in subject2
34
Enter marks in subject3
45
Sum is =  102
You have failed!

SHELL UNIX-LINUX PROGRAM TO WISH DEPENDING ON THE TIME OF DAY

hr=`date | cut -c 12-13`
if [ $hr -lt 12 ]
then
echo "Good Morning"
else
if [ $hr -ge 12 -a $hr -lt 15 ]
then
echo "Good Afternoon"
else
if [ $hr -ge 15 -a $hr -lt 24 ]
then
echo "Good Evening"
fi
fi
fi

Out put:
[csuser@localhost ~]$ vi tt
[csuser@localhost ~]$ chmod 777 tt                   (tt is a file name)
[csuser@localhost ~]$ ./tt
good morning

Monday, 4 July 2016

PROGRAM TO IMPLEMENT TYPE OF A FILE

echo "Enter file name:"
read fname
if test -f $fname
then
echo "The given name $fname is a file!"
elif test -d $fname
then
echo "The given name $fname is a directory!"
else
  echo “Then name $fname doesn’t exist”
fi
output: 
[csuser@localhost ~]$ vi x
[csuser@localhost ~]$ chmod 777 x
[csuser@localhost ~]$ ./x
Enter file name:
a                                                   ->   (a is a file name)
Then name a doesn.t exist.

java program to find the area of a circle

//ComputeArea.java:Compute the area of a circle

public class ComputeArea {
public static void main(String[] args){

 double PI = 3.14159;
double radius = 20;

//Compute area

double area = radius * radius * PI;

//Display result

System.out.println("The area for the circle of radius," + radius  + "is,"   + area);

}
}