Unix & Linux
Programming(IT-214)
Submitted to:- Submitted by:-
Mr. M. K. Murmu Gaurav
Assistant Professor 11610325
NIT Kurukshetra IT-5
INDEX
Experiment no. Experiment Name Page no.
2) 2.4 To write a C program to find the greatest of three 3
numbers.
2.6 Write a C program to find factorial of a number. 4
3) 3.3 Write a shell script program to develop a scientific 5-9
calculator.
3.4 Write a shell script program to check whether the given 10
number is even or odd.
4) 4.1 Write a shell script program to check whether given file 11
is a directory or not.
4.2 Write a shell script program to count number of files in 12
directory.
5) 5.1.1 Write a grep/egrep script to find number of word 13
characters, words and lines in a file.
5.1.2 Write a grep/egrep script to find number of word 14-15
characters, words and lines in a file.
6) 6.2 Write a shell script to change the priority of a process. 16-17
6.6 Write a program to create Zombie process 17-18
7) 7.2 Write a shell script program to check and list attributes of 19
a process.
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
1) Open the terminal.
2) Create a file name ‘largest.c’ using gedit,vi editor or cat command.
3) Write source code in that file and save .
4) Compile the file with $ gcc largest.c -o largest.o
5) Run the file using ./largest.c
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
[Gaurav@Sparkie ~]$ ./largest.c
enter the three number 4
8
12
12 is largest
2.6
Name:-
Finding the factorial of a number.
Aim:-
Write a C program to find factorial of a number.
Procedure
1) Open the terminal.
2) Create a file name ‘factorial.c’ using gedit or vi editor or cat command.
3) Write source code in that file.
4) Save and exit.
5) Compile the file using command $ gcc factorial.c -o factorial.o
6) Run the file using ./a.out.
Source Code:
#include<stdio.h>
int main()
{
int num, i, fact=1;
printf("enter the number");
scanf("%d",&num);
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
1) Open the terminal.
2) Create a file name ‘calculator.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 calculator.sh
6) Run the file using ./calculator.sh.
Source Code:
#!/bin/bash
echo -e "Following functions are being provided"
echo -e "1.Additon 2.Modulus 3.sqrt 4.sin 5.base conversion\n"
echo -e "Enter your choice"
read choice
case "$choice" in
1) echo -e "Enter two no.s"
read var1 var2
echo "$var1+$var2"| bc
;;
2) echo -e "Enter two no.s"
read var1 var2
echo "$var1%$var2"|bc
;;
3) echo -e "Enter a no."
read var1
echo "sqrt($var1)"|bc -l
;;
4) echo -e "Ente a no"
read var1
echo "s($var1)"|bc -l
;;
5) echo -e "Enter the input base and output base"
read ib ob
echo -e "Enter your no."
read var
echo "obase=$ob;ibase=$ib;$var"|bc -l
esac
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
1) Open the terminal.
2) Create a file named ‘even_odd.sh’ using gedit or 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 even_odd.sh
6) Run the file using ./even_odd.sh
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
1) Open the terminal.
2) Create a file name ‘file.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 file.sh
6) Run the file using ./file.sh
Source Code:
echo "ENTER a filename"
read name
if [ -d $name ] .
then
echo "$name is a directory"
elif [ -f $name ]
then
echo "$name is a file"
else
echo "INVALID FILE/DIRECTORY NAME"
fi
Output
[Gaurav@Sparkie ~]$ ./file.sh
ENTER A filename
/home/Gaurav/Desktop
/home/Gaurav/Desktop is a directory
[Gaurav@Sparkie ~]$ ./file.sh
ENTER A filename
/home/Gaurav/Desktop/App.java
/home/Gaurav/Desktop/App.java is a file
4.2
Name:-
Counting no of files in directory
Aim:-
Write a shell script programto count number of files in directory.
Procedure
1) Open the terminal.
2) Create a file name ‘file.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 file.sh
6) Run the file using ./file.sh
Source Code:
#!/bin/sh
echo "enter directory name"
read dname
echo "no of files in the directory"
ls -l $dname | wc -l
Output
[Gaurav@Sparkie ~]$ ./file.sh
enter directory name
/home/Gaurav
no of files in the directory
1
EXPERIMENT No.-05
5.1.1
Name:-
Counting no of characters,words,lines in a file.
Aim:-
Write a grep/egrep script to find number of word characters, words and lines in a file.
Procedure
1) Open the terminal.
2) Create a file ‘grep’ whose lines,characters and words are to be counted using
Gedit ,vi editor or cat command
3) Create a file name ‘char.sh’ using gedit ,vi editor or cat command.
4) Write source code in that file.
5) Save and exit.
6) Give the necessary execution permissions to the file for execution using command
$chmod u+x char.sh
7) Run the file using ./char.sh
Source Code:
Source code (char.sh):-
#!/bin/bash
echo "enter filename"
read file
echo "lines in $file file"
grep -c “.*” $file
echo "words in $file file"
grep -c -o “ [a-zA-Z0-9]* ” $file
echo "characters in $file file"
grep -o -c “." $file |
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
1) Open the terminal.
2) Create a file name ‘fib.awk’ using gedit ,vi editor or cat command.
3) Write source code in that file.
4) Save and exit.
5) Execute script by using $awk –f fib.awk.
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:-
[Gaurav@Sparkie~]$ awk -f fib.awk
fibonacci
0
1
1
2
3
5
8
13
21
34
EXPERIMENT No.-06
6.2
Name:-
Changing the process priority.
Aim:-
Write a shell script to change the priority of a process.
Procedure
1) Open the terminal.
2) Create a file name ‘pri.sh’ using gedit ,vi editor or cat command.
3) Write source code in that file.
4) Save and exit.
5) Give the execution permissions using $chmod u+x pri.sh
6) Run the file using ./pri.sh
Source Code:
#!/bin/sh
echo"enter the PID"
read PID
echo "enter the priority"
read p
renice -n $p $PID
Output
a) Before running pri.sh
[Gaurav@Sparkie~]$ ./pri.sh
enter the PID
3702
enter the priority
19
6.6
Name:-
Create zombie process.
Aim:-
Write a program to create a Zombie process.
Procedure
1) Open the terminal.
2) Create a file name ‘zombie.c’ using gedit ,vi editor or cat command.
3) Write source code in that file.
4) Save and exit.
5) Compile the file using command $ gcc zombieProcess.c
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
1) Open the terminal.
2) Create a file name ‘process.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 process.sh
6) Run the file using ./process.sh
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