Unix Lab Manual
Unix Lab Manual
LAB MANUAL
Prepared By
S.chinnaiya, Sr.lecturer/EEE
1
LIST OF EXPERIMENTS
2) BASIC COMMANDS – II
3) PROCESS MANAGEMENT
COMMANDS
II SHELL PROGRAMMING
1) ADDITION OF TWO NUMBERS.
2) GREATEST AMONG THREE NUMBERS.
3) TO FIND THE GIVEN NUMBER IS EVEN
OR ODD.
2
III C – PROGRAM ON UNIX
1) DYNAMIC STORAGE ALLOCATION
i. ALLOCATING MEMORY
ii. ALTERING THE ALLOCATED MEMORY
2) POINTERS
i. WAP TO ADD THE SUM OF NUMBER
USING POINTER
ii. WAP TO SORT A GIVEN NUMBER USING
POINTER
3) FUNCTIONS
i. CONVERSION OF LOWER CASE TO
UPPER CASE
ii. ADDDITION OF TWO NUMBERS
iii. FIND THE CUBE OF GIVEN NUMBERS
(USING CALL BY VALUE)
iv. INTERCHANGING TWO VALUES (USING
CALL BY REFERENCE)
v. PROGRAM TO READ CHARACTERS AND
PRINT REVERSELY USING RECURSION
vi. FACTORIAL OF NUMBER USING
RECURSION
4) FILE HANDLING
i. INPUT/OUTPUT OPERATIONS ON FILE
ii. TO READ INTEGER NUMBERS UP TO 12
AND STORE EVEN AND ODD NUMBERS
IN SEPARATE FILES
iii. HANDLING THE FILE WITH FSCANF()
AND FPRINTF()
iv. IN A GIVEN NUMBERS FIND WHICH IS
EVEN AND WHICH IS ODD NO’S PRINT
AS PER NO’S USING FILES
v. PROGRAM FOR REVERSING WORDS IN
A FILE
vi. MERGING OF TWO FILES
3
EX.NO:
UNIX-BASIC COMMANDS-I
Date:
PROCEDURE:
1. CAT COMMAND
The cat command is used to create file.
Syntax:$ cat>filename
After finished typing press ctrl+d to save and end of file.
b) Create a Directory:
The mkdir is use to create an empty directory in a disk.
Syntax: $mkdir dirname
c) Removing a Directory:
4
The rmdir is used to remove a directory from disk. Before removing a
directory ,the directory must be empty.
Syntax: $ rmdir dirname.
b) Removing a File:
This rm command used to remove or erase an existing file.
Syntax: $ rm filename.
Option:
-I- used to ask the user for confirmation before removing each file.
-r-used to search and delete all the files and subdirectories.
-f-used to prompt for removal if a file is write protected.
c) Moving a File:
The mv command is used to move a file from on place to another. It
removes a specified file from its original location and places it in specified
location.
Syntax:mv old-finename new-filename.
Syntax: $ls
5
Option Purpose
-x Display multicolumnar output
-t Lists files and subdirectories with time order
-r Lists files and subdirectories in reverse order
-A Lists all files excluding. and…
-a Lists all files including the normally hidden files
-c Lists all by inode modification time.
-I Lists the inode for each file
-h Lists names of hidden files also
-q List file names having non-printable characters
-l Lists permission owner size modification time etc
Along with the file and directory names
Option
-l Used to display only number of lines
-w Used to display only number of words
-c Used to display only number of characters
5. METACHARACTERS OF UNIX:
Metacharacter are special character that are at a higher and abstract level
compared to most of other character in Unix. The shell understands and interprets these
metacharacters in a special way.
6
*--- Specifies number of characters
?--- Specifies a single character.
Example:
$ls[t]* Lists the files whose names begin with
‘t’
Or ‘T’
$ls[a-m]* Lists the files whose names begin with
alphabets from ‘a’ to ‘m’
!: Is used to specifies NOT
$ls[!a-g] Lists all files whose names begins with
from character ‘h’ and also lists all files
and directories with uppercase letters
6. FILE PERMISSIONS:
File permission is way of controlling the accessibility of file for each of three
users namely users,groups and others.
7
Syntax: chmod category operation permission file
Where
Category is the usertype.
Operation is used to assign are remove permission
Permission is the type of permission
File are the file to assign are remove permission.
Options:
8
d) Unix calculator:
Unix offers an online calculator and can be invoked by the command bc.
This command is programmable and has complex functions.
Syntax: $ bc
e) The who command:
The Who command is more powerful and used to display data about all
the users, who are currently logged into the system.
Syntax: $who
The –h option in who command displays the column header.
Syntax: $who –h
f)The who am I command:
The who command with am and I displays a single line of output pertain the login
details of the user.
Syntax: $who am I
g) The tty command:
The tty(teletype) command is used to know the terminal name that we are
using.
Syntax: $tty
h) Change the password:
If your account still does not have a password, or has one that already
known to others. Using the passwd command can change the password.
Syntax: passwd
9
EX.NO:
UNIX-BASIC COMMANDS-II
Date:
PROCEDURE:
Command Grouping:
Syntax:
$ Command1; command2;……………command n
Syntax:
$ Command1 && command2………&& command n
3.The || operator
The || operator signifies the logical OR operator appears in between two or more
valid Unix Commands .It means if the first command will happen to be unsuccessful it
will continue to execute next commands.
Syntax:
$ command1 || command 2 || …………….. || command n
Filters:
Syntax:
$ head filename
2.The tail Filter
The tail command is the opposite of the head command . It displays 10 lines of a
file from the end of the file
10
Syntax:
$ tail filename
3.The pg filter
The pg command shows the file page by page. A screen full of information is
displayed, after which the pg command displays a prompt and pauses for the user to
strike the ‘Enter’ key to continue scrolling.
Example $ ls-l | pg
4.The more command
If a file is too large for its contents to fit on the screen, it will scroll your screen
when you view it. The more command almost does the same thing as the pg command
but for few differences. To continue scrolling with the more command you have to press
‘space bar’ not the ‘enter’ key as in pg command
Example $ more myfile
‘more’ command can also works with multiple files
11
Syntax: $ nl filename
8.cut command
We can select specified fields from a line of text using cut command
9.paste command
When we cut some of the characters in a file using the cut command those can be
pasted back with the paste command.
I/O Redirection :
The symbol ‘<’ is the redirection operator, it reads the input from the specified
file.
Pipes:
A pipe is a mechanism by which the output of one command can be channeled
into the input of another command.
12
Transfer data between devices/users:
Syntax:$ mesg y
where
mesg is the command used to give permission
y specifies yes to communicate.
2.The write command
The write command is used to communicate with other users that are
logged in at the same time. before using this command we have to make sure that
our terminal has the write permission.
3.Wall command
The wall command is used to send message to all users those who are
currently logged in using the Unix server and have permission to receive message
Options
-n This option will only list the names of the news item not yet
read for /usr/news directory Example $ news –n
-s This option provides a count of the unread news item in
the /usr/news directory. Example $ news –s
Mail:
Mail refers, the textual information that can be electronically transferred from one
user to another user.
1.Mail command:
The mail Syntax: $ mail username command is used to send
mail to another user. Body of the mail
Ctrl+d
13
2.Sending mails with subject:
We can send mail to user with subject specified in mail command itself
14
EX.NO:
AIM:
To execute the process management commands in Unix environment
PROCEDURE:
1. Process Status:
The PS command is used to display the characteristics of a process. It reads through
the kernel data structure or process table to fetch the attributes of a process. This
command involved without options simply lists out the process associated with a user
at a particular terminal.
Syntax : $ ps
Options in PS command:
Option Meaning
-f Generates full listing showing
eight attributes of a process
-u usr Process of User only
-a Process of all users
-l Generates a long listing
-t term Process running on terminal term
-e or –A All process including user and
system processes.
The ‘&’ operator is used to run a process in the background and it is a shell
operator. In this case the parent process does not wait for the child process death, it just
terminates the command line with ‘&’, this command run in the background.
Eg. $ sort abc &
3923
where 3923 is the jobs PID
15
2.2 The nohup command
This command is used to execute the process, even after the user has logged out.
Eg. $ nohup cal &
5669
sending output to nohup.out
Syntax: $ at time
Eg: $ at 8 am
16
EX.NO:
SHELL PROGRAMMING
Date:
S.NO TITILE
CONDITIONAL STATMENTS
1 Addition of two numbers.
2 Greatest among three numbers.
3 To find the given number is even or odd.
4 To find the given number is equal or not.
5 To find the given year is leap year or not.
TESTING AND LOOPS
6 To find the sum of ‘n’ numbers.
7 To find the power of a given number.
8 Arithematic operations using case statement.
9 To find the factorial of the given number.
10 To find the fibonacci series of the given number.
ALGORITHM:
PROGRAM:
1. ADDITION OF TWO NUMBERS.
17
if [ $a –gt $b –a $a –gt $c ]
then
echo A IS GREATER.
elif [ $b –gt $c ]
echo B IS GREATER.
else
echo C IS GREATER.
fi
18
6..TO FIND THE SUM OF ‘N’ NUMBERS.
19
esac
20
EX.NO:
C PROGRAMMING ON UNIX
Date:
S.NO TITILE
ALLOCATING MEMORY
#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”,n);
printf(“enter the values…..”);
for(a=n;a<n+size;a++)
scanf(“%d”,a);
printf(“Printing the values……\n”);
for(a=n+size-1;a>=n;a--)
printf(“%d is stored in address %u\n”,*a,a);
#include<stdio.h>
#include<stdlib.h>
main()
{
char *p;
p=(char*)malloc(6);
strcpy(p,”MADRAS”);
21
printf(“MEMOREY CONTAINS:%s/n”,p);
strcpy(p,”CHENNAI”);
printf(“MEMOREY NOW CONTAINS:%s/n”,p);
free(P);
}
OUTPUT:
Enter the no 10 20 30 40 50
Total 150
22
for(j=0;j<5;j++)
Printf(“/n %d”,arr[j]);
a=arr;
for(j=0;j<5;j++)
for(k=0;k<5-j-1;k++)
if(*(arr+k)>*(arr+k+1))
swap(arr+k,arr+k+1)
printf(“after sorting”);
for(j=0;j<5;j++)
Printf(“/n %d”,arr[j]);
}
OUTPUT:
ENTER THE NUMBER :8
ENTER THE NUMBER :6
ENTER THE NUMBER :7
ENTER THE NUMBER :5
ENTER THE NUMBER :4
Before sorting
86754
After sorting
45678
23
OUTPUT
Ksrce
KSRCE
OUTPUT:
ENTER THE TWO NUMBERS 22 20
result is . . . . .42
int cube(int);
{
x=x*x*x;
return(x);
}
OUTPUT:
cube of 5 is . . . .125
24
INTERCHANGING TWO VALUES (USING CALL BY REFERENCE):
#include<stdio.h>
void interchange(int *a,int *b);
main()
{
int i=10,j=12;
printf(“i and j values before: %d %d /n”,i,j);
void interchange(&i,&j);
printf(“i and j values after: %d %d /n”,i,j);
}
OUTPUT:
i and j values before :10 12
i and j values after : 12 10
void reverse()
{
char c;
if ((c=getchar()!=’\n’)
reverse ();
putchar(c);
}
OUTPUT:
25
FACTORIAL OF NUMBER USING RECURSION :
main()
{
int a;
printf(“Enter The Number”);
scanf(%d”,&a);
printf(“The factorial of %d=%d”,a,rec(a) );
}
rec(int x)
{
int f;
if(x==1)
return(1);
else
f=x*rec(x-1);
return(f)
}
OUTPUT:
ENTER THE NUMBER 5
THE FACTORIALOF 5 =120
26
TO READ INTEGER NUMBERS UP TO 12 AND STORE EVEN AND ODD
NUMBERS IN SEPARATE FILES
#include<stdio.h>
main()
{
FILE *fp1, *fp2, *fp3;
int I,no;
clrscr();
fp1=fopen(“number”,”w”);
printf(“ENTER 12 NUMBER/n”);
for(i=1;i<12;i++)
{
scanf(“%d”,&no);
putw(no,fp1);
}
fclose(fP1);
fp1=fopen(“number”,”r”);
fp2=fopen(“enum”,”w”);
fp3=fopen(“onum”,”w”);
while((no=getw(fp1))!=EOF))
{
if((no%2)==0))
putw(no,fp2);
else
putw(no,fp3);
}
fclose(fP1);
fclose(fP2);
fclose(fP3);
fp2=fopen(“enum”,”r”);
fp3=fopen(“onum”,”r”);
printf(“EVEN NUMBERS/n”);
while((no=getw(fp2))!=EOF))
putc(“%d”,no);
printf(“ODD NUMBERS/n”);
while((no=getw(fp3))!=EOF))
putc(“%d”,no);
fclose(fP2);
fclose(fP3);
}
O/P :ENTER 12 NUMBERS
1 2 3 4 5 6 7 8 9 10 11
EVEN NUMBERS
2 4 6 8 10
ODD NUMBERS
1 3 5 7 9 11
27
HANDLING THE FILE WITH FSCANF () AND FPRINTF () :
#include<stdio.h>
main()
{
FILE *fp1,;
int age,i;
char filename[25],name[25];
float salary;
clrscr();
printf(“give the file name”);
scanf(“%s”,filename);
fp1=fopen(filename,”w”);
printf(“ENTER THE NAME,SALARY AND AGE/N”);
for(i=1;i<3;i++)
{
fscanf(stdin,“%s%f%d”,&filename,&salary,&age);
fprintf(“fp1, “%s%f%d”,filename,salary,age”);
}
fclose(fP1);
fprintf(stdout,”/n /n”);
fp1=fopen(“filename”,”r”);
printf(“name salary age”);
for(i=1;i<=3;i++)
{
fscanf(fp1,”%s %f %d”,name,&salary,&age);
fscanf(stdout,”%s %f %d”,name,salary,age);
}
fclose(fP1);
}
O/p :
ENTER THE FILENAME VRB
ENTER THE NAME, SALARY,AGE
AJAY 10000 27
UDAY 12000 22
RAJA 9000 19
28
WAP FROM A GIVEN NO OF NUMBERS FIND WHICH IS EVEN AND WHICH
IS ODD NO’S PRINT AS PER NO’S USING FILES :
#include<stdio.h>
#include<ctype.h>
#include<conio.h>
Void main()
{
FILE *fp1, *fp2,*fp3;
Int I,number,f;
Clrscr();
Fp1=fopen(“data”,”w”);
Scanf(“%d”,&f);
For(i=1; i<=f; i++)
{
Scanf(“%d”, &number);
If(number==-1)
Break;
Putw(number,fp1);
}
Fclose(fp1);
Fp1=fopen(“data”, “r”);
Fp2=fopen(“odd”, “w”);
Fp3=fopen(“even”, “w”);
While((number=getw(fp1))!=EOF)
{
If(number%2==0)
Putw(number,fp3);
Else
Putw(number,fp2);
}
Fclose(fp1);
Fclose(fp2);
Fclose(fp3);
Fp2=fopen(“odd”, “r”);
Fp3=fopen(“even”, “r”);
Printf(“contents of odd file”);
While((number=getw(fp2))!=EOF)
{
Printf(“\t%d”,number);
}
Printf(“\ncontents of evev file”);
While((number=getw(fp3))!=EOF)
{
Printf(“\t%d”,number);
}
29
Fclose(fp2);
Fclose(fp3);
Getch();
}
30
MERGING OF TWO FILES :
#include<stdio.h>
#include<conio.h>
#include<string.h>
Void main()
{
Char c;
FILE *p1,*p2,*p3;
Clrscr();
P1=fopen(“data1”,”w”);
Printf(“input data for file 1:\n”);
While((c=getchar())!=EOF)
{
Fprintf(p1,”%c”,C);
}
Fclose(p1);
P2=fopen(“data2”,”w”);
Printf(“\n input data for file 2:\n”);
While((c=getchar())!=EOF)
{
Fprintf(p2,”%c”,C);
}
Fclose(p2);
p1=fopen(“data1”, “r”);
p2=fopen(“data2”, “r”);
p2=fopen(“data3”, “w”);
While((c=getc(p1))!=EOF)
{
Fseek(p1,-1,1);
While((c=getc(p1))!=’\n’)
{
Putc(c,p3);
Putc(c,p3);
}
While((c=getc(p2))!=’\n’)
{
Putc(c,p3);
Putc(c,p3);
}
}
While((c=getc(p2))!=EOF)
{
Putc(c,p3);
}
Fclose(p1);
31
Fclose(p2);
Fclose(p3);
P3=fopen(“data3”,”r”);
Printf(“\nthe merged file is:\n”);
While ((c=getc(p3))!=EOF)
{
Putchar(C);
}
Fclose(p3);
Getch();
}
32