CONTENTS
Ex No. 1. 2. 3. 4. 5. 6. 7. 8. 11. 12. 13. 14. 15. 16. 17. 18. Title of the Exercise
BASIC UNIX COMMANDS DIRECTORY COMMANDS FILE COMMANDS UNIX PIPES UNIX FILTERS EMPLOYEE PAYROLL PROCESSING STUDENT MARK LIST EVALUATION SUM OF DIGITS ODD NUMBER LIST ARMSTRONG NUMBER FACTORIAL OF N NUMBERS FIBONACCI SERIES DYNAMIC STORAGE ALLOCATION POINTERS FUNCTIONS FILE HANDILING
Page No
STEP 1: INTRODUCTION
OBJECTIVE OF THE EXERCISE To make the user to work in the UNIX environment.
STEP 2: ACQUISITION
Commands: 1. Date Command [azar@linuxserver ~]$ date 2. Calendar Command [azar@linuxserver ~]$ cal 3. Echo Command $ echo <Message> [azar@linuxserver ~]$ echo be brave 4. Who Command [azar@linuxserver ~]$ who 5. Precision Calculator [azar@linuxserver ~]$ bc 6. To exit from unix [azar@linuxserver ~]$ exit
OUTPUT:
STEP 3: PRACTICE/TESTING
Questions:
RESULT: 4
DIRECTORY COMMANDS
STEP 1: INTRODUCTION
OBJECTIVE OF THE EXERCISE To understand the various directory commands in UNIX environment.
STEP 2: ACQUISITION
Commands: 1. Creating new directory $ mkdir <dirname> 2. To Change Directory Command $ cd <dirname> [azar@linuxserver ~]$ cd students 3. To know current working directory [azar@linuxserver students]$ pwd 4. Exit from current Directory [azar@linuxserver students]$ cd 5. To Remove a directory $ rmdir <dirname>
OUTPUT:
STEP 3: PRACTICE/TESTING
Questions: 1. How to create directory in UNIX operating system?
RESULT:
FILE COMMANDS
STEP 1: INTRODUCTION
OBJECTIVE OF THE EXERCISE To understand the various commands for creating and manipulate the file operations.
STEP 2: ACQUISITION
Commands: 1. Creating new File $ cat > filename [azar@linuxserver ~]$ cat>file1 2. To display contents of a file $ cat filename [azar@linuxserver ~]$ cat file1 3. Concatenating multiple files $ cat file1 file2 > file3 [azar@linuxserver ~]$ cat file1 file2 > file3 [azar@linuxserver ~]$ cat file3 4. To copying a file $ cp old-file new-file [azar@linuxserver ~]$ cp file3 file4 5. To Removing a file $ rm filename 6. To Moving a file $ mv old-file new-file [azar@linuxserver ~]$ mv file4 file5
7. Listing Files & Directories $ ls [azar@linuxserver ~]$ ls 8. To Counting number of words in a file $ wc filename [azar@linuxserver ~]$ wc file3 9. Sort Command Ascending order: $ sort filename Descending order: $ sort r filename [azar@linuxserver ~]$ sort -r file3 10. To remove duplicate RECORDS $ uniq filename [azar@linuxserver ~]$ uniq file3 11. To compare two files [azar@linuxserver ~]$ cmp file1 file2
STEP 3: PRACTICE/TESTING
Questions: 1. How to create file in UNIX operating system?
3. What the command used to get remove the data in the files?
RESULT:
10
UNIX PIPES
STEP 1: INTRODUCTION
STEP 2: ACQUISITION
Commands: 1.) $ command1 | command2 [azar@linuxserver ~]$ who|wc
2.)
$ command1 | tee file | command2 $ who | tee out-file | wc [azar@linuxserver ~]$ who|tee pipefile1|wc
3.)
Message Command $ mesg y/n Write Command - To send message to a particular users. $ write <user-name> [azar@linuxserver ~]$ write azar
4.)
5.)
WALL Command $ wall <Message> [azar@linuxserver ~]$ wall hello MAIL Command To send a Mail: $ mail <user-name> [azar@linuxserver ~]$ mail azar To receive a Mail: $ mail [azar@linuxserver ~]$ mail 11
6.)
STEP 3: PRACTICE/TESTING
Questions:
RESULT:
12
UNIX FILTERS
STEP 1: INTRODUCTION
OBJECTIVE OF THE EXERCISE To understand the use of Filter commands in UNIX.
STEP 2: ACQUISITION
Commands: 1.) Head Filter $ head n <File-Name> [azar@linuxserver ~]$ head -3 file3 2.) Tail Filter $ tail n <File-Name> [azar@linuxserver ~]$ tail -3 file3 3.) Grep Command $ grep <pattern> <file> [azar@linuxserver ~]$ grep bha file2 4.) Adding Line Numbers $ nl <File-Name> [azar@linuxserver ~]$ nl file1 [azar@linuxserver ~]$ nl file2 5.) CUT Command $ cut c1 <file-Name> [azar@linuxserver ~]$ cut -c1 file2 6.) PASTE Command $ paste <File-Name> [azar@linuxserver ~]$ paste file7 13
7.) Compare Command $ cmp file1 file2 [azar@linuxserver ~]$ cmp file1 file2 8.) Common Command $ comm file1 file2 [azar@linuxserver ~]$ comm file1 file2
14
STEP 3: PRACTICE/TESTING
Questions:
RESULT:
15
STEP 1: INTRODUCTION
OBJECTIVE OF THE EXERCISE To understand about the shell programming in UNIX and calculate the employee payroll processing.
STEP 2: ACQUISITION
ALGORITHM: Step1: Start the program execution. Step2: Get the employee name, id & basic salary details. Step3: Fix the DA, PF & HRA percentages to calculate the Gross Salary. Step4: Calculate the Gross Salary. Step5: Display the calculated Cross Salary of the employees. Step6: Stop the program execution.
16
PROGRAM: echo "Enter Employee Name:" read name echo "Enter Employee Id No:" read id echo "Enter the Basic Salary" read basic let hra=10 let epf=15 let da=10 let net=$basic let t=$basic*$hra/100 let u=$basic*epf/100 let v=$basic*da/100 let net=$net+$t+$u+$v echo "Name : $name" echo "Emp.Id :$id" echo "Net Salary:$net" INPUT:
OUTPUT:
17
STEP 3: PRACTICE/TESTING
Questions:
RESULT:
18
STEP 1: INTRODUCTION
OBJECTIVE OF THE EXERCISE To understand about the conditional statements in shell programming by evaluate the student mar list.
STEP 2: ACQUISITION
ALGORITHM: Step1: Start the program execution. Step2: Get the details of student name, roll number & subjects marks. Step3: Check the constrains of the subject marks. Step4: Judge the student results bases on the constraints & calculate the total marks & average. Step5: Display the calculated results & total marks of the student. Step6: Stop the program execution.
19
PROGRAM: echo "Enter the Student Name :" read name echo "Enter Roll Number :" read rollno echo "Enter Marks-1 :" read mark1 echo "Enter Marks-2 :" read mark2 echo "Enter Marks-3 :" read mark3 let total=$mark1+$mark2+$mark3 let average=$total/3 echo "Name : $name" echo "Roll : $rollno" echo "Total :$total" echo "Avg : $average" let i=0 if(($mark1 > 49)) then let i=$i+1 else echo fi if(($mark2 > 49)) then let i=$i+1 else echo fi if(($mark3 > 49)) then let i=$i+1 20
else echo fi if(($i==3)) then echo "Pass" else echo "Fail" fi INPUT:
OUTPUT:
21
STEP 3: PRACTICE/TESTING
Questions:
1. How to get the input in shell programming? By use the command read.
RESULT:
22
SUM OF DIGITS
STEP 1: INTRODUCTION
OBJECTIVE OF THE EXERCISE
To understand about the conditional statements and arithmetic operators in shell programming by calculating sum of the digits.
STEP 2: ACQUISITION
ALGORITHM: Step1: Start the program execution. Step2: Get the particular number as the input. Step3: Check whether that particular number is single digit number or as multiple digit number. Step4: Separate the given numbers as individual digits. Step5: Add all the individual digits. Step5: Display the calculated results. Step6: Stop the program execution.
23
PROGRAM: echo "Enter the Number:" read Num let sum=0 while(($Num!=0)) do let x=$Num%10 let sum=$sum+$x let Num=$Num/10 done echo "The Result :$sum" INPUT:
OUTPUT:
STEP 3: PRACTICE/TESTING
Questions: 1. What is the use of let command in UNIX?
RESULT:
. 24
STEP 1: INTRODUCTION
OBJECTIVE OF THE EXERCISE To understand about the conditional statements and arithmetic operators in shell programming by find the odd numbers.
STEP 2: ACQUISITION
ALGORITHM: Step1: Start the program execution. Step2: Get the particular number N as the input. Step3: Add all the numbers from 1 to N. Step4: Display the addition values. Step5: Stop the program execution.
PROGRAM: echo " enter the number" read n echo " the odd numbers upto $n are:" let q=1 while ((q<=n)) do echo "$q" let q=$q+2 done
25
INPUT:
OUTPUT:
STEP 3: PRACTICE/TESTING
Questions:
RESULT: .
26
ARMSTRONG NUMBER
STEP 1: INTRODUCTION
OBJECTIVE OF THE EXERCISE To understand about the conditional statements and arithmetic operators in shell programming by finding the Armstrong number.
STEP 2: ACQUISITION
ALGORITHM: Step1: Start the program execution. Step2: Get the particular number as the input. Step3: Check whether that particular number is single digit number or as multiple digit number. Step4: Separate the given numbers as individual digits. Step5: To check whether the given number is Armstrong or not. Step6: Stop the program execution.
27
PROGRAM: echo "Enter the number to find the given number is armstrong" read n let m=$n let s=0 while(($m != 0)) do let c=$m%10 let s=$c*$c*$c+$s let m=$m/10 done if(($n==$s)) then echo "It is an ARMSTRONG NUMBER" else echo "It is not an ARMSTRONG NUMBER" fi
INPUT:
OUTPUT:
INPUT:
OUTPUT:
28
STEP 3: PRACTICE/TESTING
Questions:
RESULT: .
29
FACTORIAL OF N NUMBERS
STEP 1: INTRODUCTION
OBJECTIVE OF THE EXERCISE To understand about the conditional statements and arithmetic operators in shell programming by calculating factorial of N numbers.
STEP 2: ACQUISITION
ALGORITHM:
Step1: Start the program execution. Step2: Get the particular number as the input. Step3: Perform the summation of multiplication of the numbers from 1 to N. Step4: Display the summation value. Step5: Stop the program execution.
PROGRAM: echo "Enter the value " read n let s=1 let i=1 while((i<=n)) do let s=$s*$i let i=$i+1 done echo "The factorial of $n=$s"
30
INPUT:
OUTPUT:
STEP 3: PRACTICE/TESTING
Questions:
RESULT:
31
FIBONACCI SERIES
STEP 1: INTRODUCTION
OBJECTIVE OF THE EXERCISE To write and execute a UNIX shell program to calculate the Fibonacci series for the given number. .
STEP 2: ACQUISITION
ALGORITHM: Step1: Start the program execution. Step2: Get the particular number N as the input. Step3: Perform the cumulative addition of the numbers from 1 to N by adding the current number with the next number. Step4: Display the summation value. Step5: Stop the program execution. PROGRAM: echo "Enter the number (how many times the FIBONACII is to be generated)" read n let p=0 let c=1 let i=3 echo " " echo "$p" echo "$c" while(($i <= $n)) do let sum=$p+$c echo "$sum" let p=$c let c=$sum let i=$i+1 done 32
INPUT:
OUTPUT:
STEP 3: PRACTICE/TESTING
Questions: 1. What is the Fibonacci series for the given number?
RESULT:
33
STEP 1: INTRODUCTION
OBJECTIVE OF THE EXERCISE
The objective of this program is to introduce students to handle the different storage system. It means program can its memory while it is running. It allows us to allocate addition memory space or to release unwanted space at the time of program execution (runner). AIM: To write and execute a C program in UNIX of to print variables from memory address. ALGORITHM: Step 1: Step 2: Step 3: Step 4: Step 5: Step 6: Step 7: Start program. Initialize the data variables. Determine the sizeof() function. Initialize the number of bytes allocated to pointer *n.. Reads the value for for loop. Reads the value for a each time until it reaches the variable size. Stop the program
34
CODING: #include<stdio.h> #include<stdlib.h> #include<conio.h> void main() { int *a,*n,size; clrscr(); printf(Enter the size); scanf(%d,&size); n=(int *)malloc(size * sizeof(int)); printf(Address of the first byte is %u,n); printf(Enter the Values); for(a=n;a<n+size;a++) scanf(%d,a); for(a=n+size-1;a>=n;a--) printf(%d is stored in address %u,*a,a); }
35
OUTPUT:
STEP 3: PRACTICE/TESTING
Questions: 1. What are the differences between malloc() and calloc()?
RESULT:
36
POINTERS
STEP 1: INTRODUCTION
OBJECTIVE OF THE EXERCISE
The objective of this program is to introduce students to the usage of Pointers. A pointer is a memory variable, it contain the memory address of the another variable. It is declared in the same manner like other variables AIM: To write a C program to print address and value of variables. ALGORITHM: Step 1: Step 2: Step 3: Step 4: Step 5: Step 6: Step 7: Step 8: Step 9: Start program. Assign fork() system call to pid. if pid is equal to -1, child process not created. if pid is equal to 0, child process will be created. Print the id of parent process and child process. Create another one child process in same loop. Print id of parent process and the child process. Print grandparent id. Stop the program
37
CODING: #include <stdio.h> main() { int a=22; int *a; a=&a; printf(\n values of a =%d,*a); printf(\n Address of a = %u,&a); printf(\n value at address %u=%d,&a,*(&a)); }
OUTPUT:
38
STEP 3: PRACTICE/TESTING
Questions:
RESULT: .
39
FUNCTIONS
STEP 1: INTRODUCTION
OBJECTIVE OF THE EXERCISE The objective of this program is to introduce students about a function, which is a set of instruction that are used to perform specified task. AIM: To write a user defined function that computes x raised to the power of y ALGORITHM: Step 1: Step 2: Step 3: Step 4: Step 5: Step 6: Step 7: Start program. Initialize the data variables x, pid & SIGNALRM. if pid & fork() is equal to 0, Alarm stopped. if pid is equal to 0, child process will be created. Print the process of alarm going to off. If x is equal to alarm handler function, the alarm function is done. Obtained the signal from the handler, the alarm goes to offline it can be restarted. Step 8: Stop the program.
40
CODING: #include<stdio.h> #include<conio.h> Main() { int x,y; int power(int,int); clrscr(); printf(enter x and y values); scanf(%d%d,&x,&y); printf(%d to the power of %d is .. %f,x,y,power(x,y)); }
int power(int x,int y) { float p=1.0; if(y>=0) while(y--) p*x; else while(y++) p/=x; return(p); }
41
OUTPUT:
STEP 3: PRACTICE/TESTING
Questions: 1. List out the types of functions?
RESULT:
42
FILE HANDLING
STEP 1: INTRODUCTION
OBJECTIVE OF THE EXERCISE The objective of this program is to introduce students about a File, which is a set of instruction that is used to perform specified task. AIM: To write a program for copying the file both read and write operations must be used in the same buffer.
ALGORITHM: Step 1: Step 2: Step 3: Step 4: Step 5: Step 6: Step 7: Start program. Initialize the data variables x, pid & SIGNALRM. if pid & fork() is equal to 0, Alarm stopped. if pid is equal to 0, child process will be created. Print the process of alarm going to off. If x is equal to alarm handler function, the alarm function is done. Obtained the signal from the handler, the alarm goes to offline it can be restarted. Step 8: Stop the program.
43
CODING: #include<fcntl.h> #include<sys.h> #define BUFSIZE 1024 int main(void) { int fd1,fd2,n; char buf[BUFSIZE]; fd1=open(etc/addition,O_RDONLY); fd2=open(addition1,O_WRONLY\O_CRET\\O_TRUNC); while((n=read(fd1,buf,BUFSIZE))>0) write(fd2,buf,n); close(fd1); close(fd2); exit(0); } OUTPUT:
44
STEP 3: PRACTICE/TESTING
Questions: 1. Write the syntax of open a File.
4.
RESULT:
45