Akash Pal Unix and Shell Programming Practical File
Akash Pal Unix and Shell Programming Practical File
Practical File
Of
Unix and Shell Programming
BACHELOR OF TECHNOLOGY
Batch:2019-23
Submitted to Submitted by
Mr. Suraj Panwar Akash Pal
Department of Computer Science & Eng. 19134501010
1. Write a Shell Script that accepts a filename, starting and ending line numbers
as arguments and displays all the lines between the given line numbers?
2. Write a Shell script that deletes all lines containing a specified word in one or
more files supplied as arguments to it?
3. Write a C program that demonstrates redirection of standard output to a file.
EX: ls>f1?
4. Write a Shell script to list all of the directory files in a directory
5. Write a Shell script to find factorial of a given integer?
6. Write an awk script to find the number of characters, words and lines in a
file?
7. Write a Shell script that computes the gross salary of an employee according
to the following rules mentioned.
8. Write a C program to create Zombie Process
9. Write a Shell script which receives two file names as arguments. It should
check whether the two file contents are same or not. If they are same then
second file should be deleted
10. Implement in C language the following Unix Commands using System Calls
11. Write Internal Commands in UNIX
12. Write External Commands in UNIX
1) Write a Shell script that accepts a filename, starting and ending line numbers as
arguments and displays all the lines between the given line numbers.
Program (script.sh):
#!/bin/bash
filename=$1
start_line=$2
end_line=$3
Running script.sh:
Output:
2) Write a Shell script to show functioning of for loop in shell programming.
Program:
#!/bin/bash
for i in {1..10}
do
echo "The number is $i"
done
Running script.sh:
Output:
3) Write a shell script using a while loop.
Program:
#!/bin/bash
counter=1
while [ $counter -le 5 ]
do
echo "The number is $counter"
counter=$((counter + 1))
done
Running script.sh:
Output:
4) Write a shell script using if else statement.
Program:
#!/bin/bash
number=10
if [ $number -gt 10 ]
then
echo "The number is greater than 10"
else
echo "The number is less than or equal to 10"
fi
Running script.sh:
Output:
5) Write a Shell script to list all the files in a directory.
Program:
#!/bin/bash
Running script.sh:
Output:
6) Write a Shell script to find whether a number is even or odd.
Program:
#!/bin/bash
Running script.sh:
Output:
7) Write a Shell script to find the factorial of a given integer.
Program:
#!/bin/bash
Running script.sh:
Output:
8) Write a shell script that computes the gross salary of an employee according to
the following rules: (The basic salary is entered interactively through the
keyboard.) I. If basic salary is < 1500 then HRA =10% of the basic and DA
=90% of the basic. II. If the basic salary is >=1500 then HRA =Rs500 and
DA=98% of the basic.
Program:
#!/bin/bash
Running script.sh:
Output:
9) Write a C program to create a zombie process.
Program:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main() {
pid_t pid = fork();
if (pid == 0) {
// This is the child process.
exit(0);
} else {
// This is the parent process.
sleep(10);
}
return 0;
}
Output:
The command prompt will wait for some time (10 sec) and then again the command prompt
will appear.
10) Write a shell script which receives two file names as arguments. It should check
whether the two file contents are the same or not. If they are the same then the
second file should be deleted.
Program:
#!/bin/bash
if [ $# -ne 2 ]; then
echo "Usage: $0 <file1> <file2>"
exit 1
fi
file1=$1
file2=$2
if cmp -s $file1 $file2; then
echo -e "The file contents are:\nSame."
rm $file2
else
echo -e "The file contents are:\nNot Same."
fi
Cases:
File 1
(textfile.txt)
File 2
(textfile2.txt)
Running
script.sh
Output
11) Describe some Internal commands.
Answer:
Here are some common internal commands:
Internal commands are typically faster than external commands because they do not
require the shell to load a separate program.
These are just a few of the many external commands that are available in Unix. You can find
a complete list of external commands by typing man followed by the name of the command.
For example, to see the manual page for the cat command, you would type man cat. External
commands are typically stored in the /bin or /usr/bin directories.
When you type the name of an external command, the shell will search these directories for
the executable file for the command. External commands are a powerful tool for working
with files and directories in Unix. By learning how to use external commands, you can
automate many of your tasks and make your work more efficient