Unix and Linux Programming File
Unix and Linux Programming File
Programming(IT-214)
7.3 Write a shell script program to check and list attributes of 20-21
a process.
EXPERIMENT No.-02
2.4
Name:-
Finding the largest of three numbers.
Aim:-
To write a C program to find the greatest of three numbers.
Procedure
Source Code:
#include<stdio.h>
int main()
{
int a,b,c;
printf("enter the three number");
scanf("%d%d%d",&a,&b,&c);
if(a>=b&&a>=c)
printf("%d is largest",a);
else if(b>=c)
printf("%d is largest",b);
else
printf("%d is largest",c);
return 0;
}
Output
Aim:-
Write a C program to find factorial of a number.
Procedure
Source Code:
#include<stdio.h>
int main()
{
int num, i, fact=1;
if(num<0)
printf("factorial cannot be calculated");
else
if(num==0)
fact=1;
else
{
for(i=num;i>1;i--)
{
fact=fact*i;
}
}
printf("factorial of %d is %d",num,fact);
return 0;
}
Output
[Gaurav@Sparkie~]$./factorial.c
enter the number6
factorial of 6 is 720
[Gaurav@Sparkie~]$./factorial.c
enter the number -1
factorial cannot be calculated
EXPERIMENT No.-03
3.3
Name:-
Developing scientific calculator.
Aim:-
Write a shell script program to develop a scientific calculator.
Procedure
Source Code:
#!/bin/bash
case "$choice" in
Output
[Gaurav@Sparkie~]$ ./calculator.sh
Following functions are being provided
1.Additon 2.Modulus 3.sqrt 4.sin 5.base conversion
Enter your choice
3
Enter a no
4
2.00000
3.4
Name:-
Finding even and odd number.
Aim:-
Write a shell script program to check whether the given number is even or odd.
Procedure
Source Code:
#!/bin/sh
echo "enter number"
read n
if [ $((n%2)) -eq 0 ]
then
echo "$n is even number"
else
echo "$n is odd number "
fi
Output
[Gaurav@Sparkie~]$./even_odd.sh
enter the number
2
2 is even number
EXPERIMENT No.-04
4.1
Name:-
Checking file is directory or not
Aim:-
Write a shell script programto check whether given file is a directory or not.
Procedure
Source Code:
Output
Aim:-
Write a shell script programto count number of files in directory.
Procedure
Source Code:
#!/bin/sh
echo "enter directory name"
read dname
echo "no of files in the directory"
ls -l $dname | wc -l
Output
Aim:-
Write a grep/egrep script to find number of word characters, words and lines in a file.
Procedure
Source Code:
Output
[Gaurav@Sparkie ~]$ ./char.sh
enter filename
/home/Gaurav/file1
lines in /home/Gaurav/file1 file
19
words in /home/Gaurav/file1 file
138
characters in /home/Gaurav/file1file
469
5.1.2
Name:-
Print Fibonacci series
Aim:-
Write an awk script to develop Fibonacci series.
Procedure
Source Code:
BEGIN{
printf “%s\n”,'fibonacci series'
a=0
b=1
printf "%d\n",a
for(i=0;i<10;i++)
{
print b;
b = b+a
a = b-a
}
}
Samp\e Input and output:-
EXPERIMENT No.-06
6.2
Name:-
Changing the process priority.
Aim:-
Write a shell script to change the priority of a process.
Procedure
Source Code:
#!/bin/sh
echo"enter the PID"
read PID
echo "enter the priority"
read p
renice -n $p $PID
Output
6.6
Name:-
Create zombie process.
Aim:-
Write a program to create a Zombie process.
Procedure
Source Code:
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/wait.h>
#include<sys/types.h>
int main()
{
pid_t PID=fork();
if(PID>0)
{
printf("I am a parent processs%d\n",getpid());
wait(NULL);
printf("Now parent terminates");
exit(0);
}
else
if(PID==0)
{
printf("I am a child processs%d\n",getpid());
printf("Now the child process terminates");
exit(0);
}
else
{
printf("Error"); exit(1);
}
return 0;
}
Output
Gaurav@Sparkie:~/os$ ./zombieProcess.o
I am a parent processs7249
I am a child processs7250
Now the child process terminates
Now parent terminates
EXPERIMENT No.-07
7.2
Name:-
Listing attributes of a process.
Aim:-
Write a shell script program to check and list attributes of a process.
Procedure
Source Code:
#!/bin/bash
echo "enter the name or the id or any other attribute of the process whose attributes you
want to know"
read p
echo "following are attributes of the process"
ps –ef | grep $p
Output
[Gaurav@Sparkie~]$ ./process.sh
enter the name or the id or any other attribute of the process whose attributes you want to
know
1726
following are attributes of the process
Gaurav 1726 1813 0 11:06 pts/0 00:00:00 bash
Gaurav 1032 1176 0 11:08 pts/0 00:00:00 /bin/bash ./process.sh
Gaurav 1940 1932 0 11:08 pts/0 00:00:00 grep 1726
7.3
Name:-
Implement read, write and execute permission.
Aim:-
Write a shell script program to implement read, write and execute permission.
Procedure
1) Open the terminal.
2) Create a file name ‘pr.sh’ using gedit, vi editor or cat command.
3) Write source code in that file.
4) Save and exit.
5) Give the necessary execution permissions to the file for execution using command
$chmod u+x pr.sh
6) Run the file using ./pr.sh
Source Code:
#!/bin/bash
echo "enter the file name to be checked"
read file
if [ -w $file ]
then
echo "File Already writable"
elif [ ! -w $file ]
then
chmod 222 $file
echo "Now file is writable"
fi
if [ -r $file ]
then
echo "File Already readable"
elif [ ! -r $file ]
then
chmod 444 $file
echo "Now file is readable"
fi
if [ -x $file ]
then
echo “File is already executable"
elif [ ! -x $file ]
then
chmod 111 $file
echo "Now file is executable"
fi
Output
[Gaurav@Sparkie~]$ ./pr.sh
enter the file name to be checked
emp.lst
File Already writable
File Already readable
Now file is executable