indira institute of engineering and technology

Upload: mohanbalajiece

Post on 09-Apr-2018

224 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/7/2019 INDIRA INSTITUTE OF ENGINEERING AND TECHNOLOGY

    1/36

  • 8/7/2019 INDIRA INSTITUTE OF ENGINEERING AND TECHNOLOGY

    2/36

    INDEX

    S.No LIST OF EXPERIMENTS Page No

    1 UNIX COMMANDS

    1.1 General Purpose Commands 5

    1.2 Directory Handling Commands 7

    1.3 File Handling Commands 8

    1.4 Input and Output Redirection 9

    1.5 Pipes and Filters 10

    2 SHELL PROGRAMMING

    2.1 Program to convert Fahrenheit to centigrade and vice versa 12

    2.2 Program to check whether the number is odd or even 13

    2.3 Program to Compare two strings 14

    2.4 Program to determine the given number is positive, negative or zero 15

    2

  • 8/7/2019 INDIRA INSTITUTE OF ENGINEERING AND TECHNOLOGY

    3/36

    2.5 Program to generate menu driven program 16

    2.6 Program to find the square and cube of the first five numbers 18

    2.7 Program to find the sum of first N natural numbers 19

    2.8 Program to find the sum of digits in the given number. 20

    3 C PROGRAMMING ON UNIX

    3.1 Program to perform linear search using function 22

    3.2 Program to print the elements of an array using pointers 25

    3.3 Program to find the length of the string 28

    3.4 Program to concatenate two strings 31

    3.5 Program to copy the contents of one file to another. 34

    3

  • 8/7/2019 INDIRA INSTITUTE OF ENGINEERING AND TECHNOLOGY

    4/36

    UNIX COMMANDS

    4

  • 8/7/2019 INDIRA INSTITUTE OF ENGINEERING AND TECHNOLOGY

    5/36

    1.1. GENERAL PURPOSE COMMANDS

    1. $ date + %d%m%y

    13/01/09

    Use : Displays the date in the specified dd/mm/yy format.

    2. $cal January 2009

    January 2009

    Use : Displays the calendar for the year Jan 2009

    3. $who

    2cse12 tty5 2009-12-31 07: 062cse11 tty6 2009-12-31 07: 062cse10 tty7 2009-12-31 07: 062cse15 tty8 2009-12-31 07: 06

    Use: Displays the number of users who are logged in to the network

    4. $ who b

    System reboot 2009 -12-30 08:45

    Use : Display the last date and time of reboot of the UNIX server

    5

  • 8/7/2019 INDIRA INSTITUTE OF ENGINEERING AND TECHNOLOGY

    6/36

    5. $ who am i

    User1 tty1 Jan15 09:34

    Use : Identifies the user and lists the user names, terminal line, the date oflogin.

    6. $uname a

    Linux ubuntu 2.64-generic #1 SMP Thu Apr 10 13:25:23 UTC 2009 i686GNU/Linux

    Use : Display details about your operating system.

    7. $ man cat

    Use: Displays the help level for the cat command8. $ echo Information Technology

    Use : Displays the message on the screen

    9. $ login Username

    Password:

    Use : To login in to the UNIX system

    6

  • 8/7/2019 INDIRA INSTITUTE OF ENGINEERING AND TECHNOLOGY

    7/36

    1.2. DIRECTORY HANDLING COMMANDS

    10.$pwd

    /home/root

    Use : Displays the current working directory

    11.$ls a

    .esd_auth .config .gvfs .pulse

    .cache .gnugp. profile .ssh

    .local Template Music Vidoes

    Use: Display all the files including hidden files

    12.$ls R

    Divisors nnsumnsn rns test

    Use : Displays all the files in the sub directories

    13.$mkdir cse

    Use: Creates a new directory cse.

    14.$rmdir cse

    Use : Removes the existing directory cse

    7

  • 8/7/2019 INDIRA INSTITUTE OF ENGINEERING AND TECHNOLOGY

    8/36

    1.3. FILE HANDLING COMMANDS

    15. $ cd stud

    $root/stud

    Use: To change the directory.

    16. $ cat> test

    Unix is a open source operating system and its license free of cost

    Its mainly used for mainframe and high security networks

    Ctr+d

    Use: To create a file in the existing directory

    17. $ cp m.c new.c

    Use : This command copies the m.c to new file new.c

    18. $wc m.c

    Use : This counts the number of lines, characters and words in m.c file.

    19. $rm f.c

    Use: This command removes the file f.c

    8

  • 8/7/2019 INDIRA INSTITUTE OF ENGINEERING AND TECHNOLOGY

    9/36

    1.4. INPUT AND OUTPUT REDIRECTION

    20. $date>testing

    $cat testing

    Wed Dec 31 12:47:59 UTC 2008

    Use: Redirect the system date contents to a file.

    21. $cat test1Welcome to

    $cat test2

    Information Technology

    $cat test1>>test2

    $cat test2

    Welcome to Information Technology

    Use: Concatenate two files and displays the contents of test2

    22. $ ls sh*

    ShanSureshSaiKumar.cShanthi.c

    Use : List the files starting with sh

    23. $ls *ha

    Radha

    AshaUshaSha

    Use : List the files ending with ha

    9

  • 8/7/2019 INDIRA INSTITUTE OF ENGINEERING AND TECHNOLOGY

    10/36

  • 8/7/2019 INDIRA INSTITUTE OF ENGINEERING AND TECHNOLOGY

    11/36

    SHELL PROGRAMMING

    11

  • 8/7/2019 INDIRA INSTITUTE OF ENGINEERING AND TECHNOLOGY

    12/36

    2.1. FAHRENHEIT TO CENTIGRADE AND VICE VERSA

    AIM:

    To write a shell program to convert Fahrenheit to Centigrade and vice-versa

    PROGRAM:

    read c

    f= expr scale=2;(1.8 * $c) + 32 | bc

    echo Fahrenheit equivalent is : $f

    echo n Enter temperature in Fahrenheit:

    read fh

    cg= expr scale=2; ($fh 32) / 1.8 | bc

    echo Centigrade equivalent is : $cg

    OUTPUT

    $ Sh Centigrade to Fahrenheit

    Enter the temperature in Centigrade: 20

    Fahrenheit equivalent is 68.0

    Enter temperature in Fahrenheit: 68

    Centigrade equivalent is 20.00

    RESULT

    Thus the above shell program to convert Fahrenheit to centigrade and vice-versa isbeen verified and executed successfully.

    12

  • 8/7/2019 INDIRA INSTITUTE OF ENGINEERING AND TECHNOLOGY

    13/36

    2.2. ODD OR EVEN

    AIM

    To write a shell program to check whether the given number is odd or even.

    PROGRAM

    echo n Enter the number:

    read a

    c= expr $a % 2

    if test $c eq 0

    then

    echo The number $a is even

    else

    echo The number is $a is odd:

    fi

    OUTPUT

    $ Sh oddeven

    Enter the number 6

    The number 6 is even

    RESULT

    Thus the above shell program to check whether the number is odd or even is beenverified and executed successfully.

    13

  • 8/7/2019 INDIRA INSTITUTE OF ENGINEERING AND TECHNOLOGY

    14/36

    2.3. COMPARE TWO STRINGS

    AIM

    To write a shell program to compare the two given strings.

    PROGRAM

    echo n Enter the String1:

    read s1

    echo n Enter the String2:

    read s2

    if [ $s1 = $s2 ]

    then

    echo Strings are equal

    else

    echo Strings are not equal

    fi

    OUTPUT

    $ Sh stringcomp

    Enter the String1 : Ramesh

    Enter the String1 : Kumar

    Strings are not equal

    RESULT

    Thus the above shell program to compare the two given strings is been verifiedand executed successfully.

    14

  • 8/7/2019 INDIRA INSTITUTE OF ENGINEERING AND TECHNOLOGY

    15/36

    2. 4. CHECK NUMBER IS POSITIVE OR NEGATIVE

    AIM

    To write a shell program to determine the number is positive, negative or zero.

    PROGRAM

    echo n Enter the Value:

    read a

    if test $a -eq 0

    then

    echo The given number $a is equal to zero

    elif test $a -gt 0

    echo The given number $a is positive

    else

    echo The given number $a is negative

    fi

    OUTPUT

    $ Sh nopos

    Enter the Value: 5The given number 5 is positive

    Enter the Value: -9The given number -9 is negative

    Enter the Value: 0The given number 0 is zero.

    RESULT

    Thus the above shell program to determine the number is positive, negative orzero is been verified and executed successfully.

    15

  • 8/7/2019 INDIRA INSTITUTE OF ENGINEERING AND TECHNOLOGY

    16/36

    2. 5. MENU DRIVEN PROGRAM

    AIM

    To write a shell program to generate menu driven program.

    PROGRAM

    echo Menu

    echo 1. List of files

    echo 2. Todays Date

    echo 3. Process Status

    echo 4. Logged in Usersecho 5. Quit

    echo n Enter your Choice:

    read ch

    case $ch in

    1) ls I;;

    2) date;;

    3) ps;;

    4) who;;

    5) exit;;

    6) echo Invalid Choice

    exit;;

    esac

    16

  • 8/7/2019 INDIRA INSTITUTE OF ENGINEERING AND TECHNOLOGY

    17/36

    OUTPUT

    $ Sh menu

    Enter the your choice 1

    .esd_auth .config .gvfs .pulse

    .cache .gnugp. profile .ssh

    Enter Your Choice 2

    13/01/09

    Enter Your Choice 3

    PID TTY TIME CMD

    8547 pts/0 00:00:02 bash4547 pts/0 01:00:42 sh5470 pts/0 00:30:02 ps

    Enter Your Choice 4

    2cse12 tty5 2009-12-31 07: 062cse11 tty6 2009-12-31 07: 062cse10 tty7 2009-12-31 07: 06

    2cse15 tty8 2009-12-31 07: 06

    Enter Your Choice 5

    RESULT

    Thus the above shell program to determine to generate menu driven program is

    been verified and executed successfully.

    17

  • 8/7/2019 INDIRA INSTITUTE OF ENGINEERING AND TECHNOLOGY

    18/36

    2. 6. SQUARE AND CUBE OF NUMBER

    AIM

    To write a shell program to find the square and cube for the first five numbers.

    PROGRAM

    echo n Number Square Cube:

    for i in 1 2 3 4 5

    do

    square= expr $i * $i

    cube= expr $i * $i * $iecho $i $Square $Cube

    done

    OUTPUT

    $ Sh sqcube

    Number Square Cube1 1 12 4 83 9 274 16 645 25 125

    RESULT

    Thus the above shell program to the square and cube for the first five numbers isbeen verified and executed successfully.

    18

  • 8/7/2019 INDIRA INSTITUTE OF ENGINEERING AND TECHNOLOGY

    19/36

    2.7. SUM OF N NATURAL NUMBERS

    AIM

    To write a shell program to find the sum of N natural Numbers.

    PROGRAM

    echo n Enter the limit:

    read n

    sum=0

    until [ $i le $n ]

    dosum = expr $sum + $i

    i= expr $i + 1

    done

    echo The Sum of first $n natural numbers: $sum

    OUTPUT

    $ Sh nna

    Enter the limit: 5

    The sum of first 5 natural numbers: 15

    RESULT

    Thus the above shell program to find the sum of N natural Numbers is beenverified and executed successfully.

    19

  • 8/7/2019 INDIRA INSTITUTE OF ENGINEERING AND TECHNOLOGY

    20/36

    2.8. SUM OF DIGITS IN GIVEN NUMBER

    AIM

    To write a shell program to find the sum of digits in the given number.

    PROGRAM

    echo n Enter the number

    read n

    sd=0

    while [ $n gt 0 ]

    do

    a= expr $n %10

    sd= expr $sd + $a

    n= expr $n /10

    done

    echo The sum of digits in the given number is $sd

    OUTPUT

    $ Sh sd

    Enter the number : 458

    The sum of digits in the given number is 17

    RESULT

    Thus the above shell program to find the sum of digits in the given number is beenverified and executed successfully.

    20

  • 8/7/2019 INDIRA INSTITUTE OF ENGINEERING AND TECHNOLOGY

    21/36

    C PROGRAMMING

    ON UNIX

    21

  • 8/7/2019 INDIRA INSTITUTE OF ENGINEERING AND TECHNOLOGY

    22/36

    3.1. LINEAR SEARCH

    AIM:

    To write a C program to perform linear search using function

    ALGORITHM:

    Step 1: Start the program

    Step 2: Read the value of n.

    Step 3: Read the array of elements a[i].

    Step 4: Read the value of x and call the liner()

    Step 4: Compute i=i+1

    Step 5: if flag==1 then print element is found else not

    Step 6: Stop the program

    22

  • 8/7/2019 INDIRA INSTITUTE OF ENGINEERING AND TECHNOLOGY

    23/36

    PROGRAM

    #include

    void linear(int [],int,int);

    main(){int i,x,a[20],n;printf("Enter the limit\n");scanf("%d",&n);printf("\n Enter the elements\n");for(i=0;i

  • 8/7/2019 INDIRA INSTITUTE OF ENGINEERING AND TECHNOLOGY

    24/36

    OUTPUT:

    Enter the limit : 5

    Enter the elements : 21 56 89 75 41

    Enter the element to be searched: 89

    The element is fond at 3rd position

    RESULT

    Thus the above C program to perform linear search using function is been verified

    and executed successfully.

    24

  • 8/7/2019 INDIRA INSTITUTE OF ENGINEERING AND TECHNOLOGY

    25/36

    3. 2. ARRAY USING POINTERS

    AIM

    To write a program to print the elements of an array string manipulation using

    pointers

    ALGORITHM

    Step 1 : Start the program

    Step 2 : Read the value of n

    Step 3 : Assign i=0

    Step 4 : Repeat until the i

  • 8/7/2019 INDIRA INSTITUTE OF ENGINEERING AND TECHNOLOGY

    26/36

    PROGRAM

    #include

    void main()

    {

    int *a[10],i,n;

    printf("\n Enter the size of array\n");

    scanf("%d",&n);

    printf("Enter the elements");

    for(i=0;i

  • 8/7/2019 INDIRA INSTITUTE OF ENGINEERING AND TECHNOLOGY

    27/36

    OUTPUT:

    Enter the size of array: 4

    Enter the elements: 10 20 30 45

    The given elements are:

    a[0]=10

    a[1]=20

    a[2]=30

    a[3]=45

    RESULT

    Thus the above C program to print the elements of an array string manipulation

    using pointers is been verified and executed successfully.

    3.3. LENGTH OF STRING

    27

  • 8/7/2019 INDIRA INSTITUTE OF ENGINEERING AND TECHNOLOGY

    28/36

    AIM:

    To write a program to find the length of the string

    ALGORITHM:

    Step 1: Start the program

    Step 2: Declare the character array and assign a string char s1[]= anand

    Step 3: call length(s1) function and assign to len.

    Step 4: print len

    Step 5: stop the program

    28

  • 8/7/2019 INDIRA INSTITUTE OF ENGINEERING AND TECHNOLOGY

    29/36

    PROGRAM

    #include

    #include

    int l(char*);

    main()

    {

    int len;

    static char s1[]="anand ";

    len=l(s1);

    printf("\n The length of String s1 %d\n",len);

    }

    int l(char*ptr)

    {

    int s=0;

    while(*ptr!='\0')

    {s+=1;

    ptr++;

    }

    return(s);

    }

    29

  • 8/7/2019 INDIRA INSTITUTE OF ENGINEERING AND TECHNOLOGY

    30/36

    OUPUT:

    Length of string s1: 5

    RESULT

    Thus the above C program to find the length of the string is been verified and

    executed successfully.

    3.4. STRING CONCATENATE

    30

  • 8/7/2019 INDIRA INSTITUTE OF ENGINEERING AND TECHNOLOGY

    31/36

    AIM:

    To write a C program to concatenate two strings.

    ALGORITHM:

    Step 1. Start the program

    Step 2. Declare the character pointer variables as s1,s2,s3.

    Step 3. Allocate memory for s1,s2 and s3 character pointer variables using

    mallloc function

    Step 4. Read the string s1 and s2.

    Step 5. Repeat the steps 7 and 8 until ((c=*(s1+i)!=\0)

    Step 6. Compute i=i+1

    Step 7. Assign s3[i+k]=c

    Step 8. Compute k=k+1

    Step 9. Assign s3[i+k]=\0

    Step 10. Print the concatenated string s3

    Step 11. Stop the program

    PROGRAM

    31

  • 8/7/2019 INDIRA INSTITUTE OF ENGINEERING AND TECHNOLOGY

    32/36

    #include#include#define length 30#includemain()

    {char *s1,*s2,*s3,c;int i,k;//clrscr();s1=(char *)malloc(length *sizeof(char));s2=(char *)malloc(length *sizeof(char));s3=(char *)malloc(2*length *sizeof(char));printf("\n Enter the String S1\n");scanf("%s",s1);printf("\n Enter the String S2\n");scanf("%s",s2);

    i=0;while((c=*(s1+i))!='\0'){

    s3[i]=c;i++;

    }k=0;while((c=*(s2+k))!='\0'){

    s3[i+k]=c;

    k++;}

    s3[i+k]='\0';printf("\n the concatenated string is %s",s3);printf("\n");

    }

    32

  • 8/7/2019 INDIRA INSTITUTE OF ENGINEERING AND TECHNOLOGY

    33/36

    Output:

    Enter the String1: Suresh

    Enter the String2: Kumar

    Concatenated String is Suresh Kumar

    RESULT

    Thus the above C program to concatenate two strings is been verified and

    executed successfully.

    3.5. FILE HANDLING

    33

  • 8/7/2019 INDIRA INSTITUTE OF ENGINEERING AND TECHNOLOGY

    34/36

    AIM:

    To write a C program to copy the contents of one file to another.

    ALGORITHM:

    Step 1. Start the program

    Step 2. Declare the file pointers fp1 and fp2.

    Step 3. Read the source filename fname1

    Step 4. Read the target filename-fname2

    Step 5. Open the file fname1 under read mode assigned to file pointer-fp1.

    Step 6. Open the file fname2 under write mode assigned to file pointer-fp2.

    Step 7. If(fp1==NULL) then step 8 else step 9

    Step 8. Print file fname1 cannot be opened for reading and then goto

    step16

    Step 9. If(fp2==NULL) then step 10 else step 11

    Step10. Print file fname2 cannot be opened for writing and then goto

    step16

    Step 11. Assign c=getc(fp1)

    Step 12. Write the character in fp2

    Step 13. Assign c =getc(fp1)

    Step 14. Close all files(fname1 and fname2)

    Step 15. Stop

    34

  • 8/7/2019 INDIRA INSTITUTE OF ENGINEERING AND TECHNOLOGY

    35/36

    Program:

    #include#include#includevoid main(){FILE *fp1,*fp2;int c;char fname1[40],fname2[40];clrscr();printf("\n Enter the source file\n");gets(fname1);

    printf("\n Enter the target file\n");gets(fname2);fp1=fopen(fname1,"r");fp2=fopen(fname2,"w");if(fp1==NULL){printf("\n Cannot open %s for reading \n",fname1);getch();exit(1);}else if(fp2==NULL)

    {printf("\n Cannot open %s for writing \n",fname2);getch();exit(1);}else{c=getc(fp1);while(c!=EOF){putc(c,fp2);c=getc(fp1);

    }fclose(fp1);fclose(fp2);printf("\n File successfully copied\n");}getch();}

    35

  • 8/7/2019 INDIRA INSTITUTE OF ENGINEERING AND TECHNOLOGY

    36/36

    Output:

    Enter the source file: f1.txt

    Enter the target file : f2.txt

    File successfully copied.

    RESULT

    Thus the above C program to copy the content of one file to another is been

    verified and executed successfully.