0% found this document useful (0 votes)
28 views

Akash Pal Unix and Shell Programming Practical File

Uploaded by

AKASH PAL
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Akash Pal Unix and Shell Programming Practical File

Uploaded by

AKASH PAL
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

A

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

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING


SCHOOL OF ENGINEERING AND TECHNOLOGY
HEMWATI NANDAN BAHUGUNA GARHWAL UNIVERSITY
(A CENTRAL UNIVERSITY), SRINAGAR (GARHWAL) – 246174
CONTENTS

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

if [ $start_line -lt 1 ]; then


echo "Starting line number must be greater than or equal to 1."
exit 1
fi

if [ $end_line -gt $(wc -l < $filename) ]; then


echo "Ending line number must be less than or equal to the number of lines in the file."
exit 1
fi

sed -n "$start_line,$end_line"p $filename

Text file (textfile.txt):

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

# Get the current directory


current_directory=$(pwd)
# List all the files in the current directory
echo "Files in current directory:"
ls "$current_directory"

Running script.sh:

Output:
6) Write a Shell script to find whether a number is even or odd.
Program:
#!/bin/bash

# Get the number from the user


echo "Enter a number:"
read number

# Check if the number is even


if [ $(expr $number % 2) -eq 0 ]; then
echo "The number $number is even."
else
echo "The number $number is odd."
fi

Running script.sh:

Output:
7) Write a Shell script to find the factorial of a given integer.
Program:
#!/bin/bash

# Get the number from the user


echo "Enter a number:"
read number

# Initialize the factorial


fact=1

# Loop from 1 to the number


for (( i=1; i<=number; i++ )); do
# Multiply the factorial by the current number
fact=$((fact * i))
done

# Print the factorial


echo "The factorial of $number is $fact"

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

echo "Enter the basic salary:"


read basic_salary
if [ $basic_salary -lt 1500 ]; then
hra=$((basic_salary * 0.1))
da=$((basic_salary * 0.9))
else
hra=500
da=$((basic_salary * 98 / 100))
fi
gross_salary=$((basic_salary + hra + da))
echo "The gross salary is $gross_salary"

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:

Case 1 (Not same file) Case 2 (Same file)

File 1
(textfile.txt)

File 2
(textfile2.txt)

Running
script.sh

Output
11) Describe some Internal commands.
Answer:
Here are some common internal commands:

● cd: Changes the current directory.


● ls: Lists the contents of a directory.
● pwd: Prints the current working directory.
● echo: Prints text to the console.
● date: Prints the current date and time.
● time: Prints the execution time of a command.
● exit: Exits the shell.

Internal commands are typically faster than external commands because they do not
require the shell to load a separate program.

12) Describe some External commands.


Answer:
Here are some common external commands:

● cp: Copies files and directories.


● mv: Moves files and directories.
● rm: Deletes files and directories.
● grep: Searches for text in files.
● sort: Sorts lines of text.
● uniq: Removes duplicate lines of text.
● wc: Counts the number of lines, words, and characters in a file.

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

You might also like