Noida Institute of Engineering & Technology,
Greater Noida
OS LAB FILE
Branch: B. Tech (AIML) Semester: 4
Session: 2024 - 25
Subject Name: Operating Systems Lab Subject Code: BCSE0453
Student Name: SHIVDEEP PANDEY Faculty Name: Ms. Roshni Afshan
Roll No.: 2301331530160 (Assistant professor)
Department of Computer Science & Engineering
NOIDA INSTITUTE OF ENGINEERING & TECHNOLOGY
19, KNOWLEDGE PARK-II, INSTITUTIONAL AREA,
GREATER NOIDA, (U. P.) - 201 306, INDIA
PROGRAM LIST
S.No. Submission
Program Name Date Grade Signature
1. Write a shell script to determine the Area
and Perimeter of a Rectangle.
2. Write a shell script to count the words,
characters, and lines in the file.
3. Write a shell script that calculates the sum
and average of an array of numbers.
4. Write a shell script to calculate the
Fibonacci sequence.
5. Write a shell script that finds prime numbers
inside a user specified range.
6. Write a shell script to determine whether a
given string is palindrome.
7. Write shell script that CO1 allows users to
create, delete, and list files in a directory.
8. Write a shell script that Count Lines in Each
File in a Directory.
9. Write a shell script that find and Replace
Text in Files.
10. Write a shell script that find Files Modified
in the Last N Days.
Q1. Write a shell script to determine the Area and Perimeter of a Rectangle?
Ans:-
#!/bin/bash
echo "Enter the length of the rectangle:"
read length
echo "Enter the breadth of the rectangle:"
read breadth
area=$((length * breadth))
perimeter=$(((2 * length) + (2 * breadth)))
echo "Area of the rectangle is: $area"
echo "Perimeter of the rectangle is: $perimeter"
root@SHIVDEEP:~
root@SHIVDEEP:~
root@SHIVDEEP:~
root@SHIVDEEP:~
root@SHIVDEEP:~
Q2. Write a shell script to count the words, characters, and lines in the file?
Ans:-
#!/bin/bash
echo "Enter the filename:"
read filename
if [ -f "$filename" ]; then
echo "Counting in file: $filename"
lines=$(wc -l < "$filename")
words=$(wc -w < "$filename")
characters=$(wc -m < "$filename")
echo "Lines: $lines"
echo "Words: $words"
echo "Characters: $characters"
else
echo "File does not exist!"
fi
root@SHIVDEEP:~
root@SHIVDEEP:~
root@SHIVDEEP:~
root@SHIVDEEP:~
root@SHIVDEEP:~
root@SHIVDEEP:~
root@SHIVDEEP:~
root@SHIVDEEP:~
root@SHIVDEEP:~
Q3. Write a shell script that calculates the sum and average of an array of numbers?
Ans:-
#!/bin/bash
echo "Enter the number of elements in the array:"
read n
sum=0
echo "Enter $n numbers:"
for (( i=0; i<n; i++ ))
do
read num
arr[$i]=$num
sum=$((sum + num))
done
average=$(echo "scale=2; $sum / $n" | bc)
echo "Sum: $sum"
echo "Average: $average"
root@SHIVDEEP:~
root@SHIVDEEP:~
root@SHIVDEEP:~
root@SHIVDEEP:~
root@SHIVDEEP:~
root@SHIVDEEP:~
root@SHIVDEEP:~
Q4. Write a shell script to calculate the Fibonacci sequence?
Ans:-
#!/bin/bash
echo "Enter the number of terms:"
read n
a=0
b=1
echo "Fibonacci sequence up to $n terms:"
for (( i=0; i<n; i++ ))
do
echo -n "$a "
fn=$((a + b))
a=$b
b=$fn
done
echo
root@SHIVDEEP:~
root@SHIVDEEP:~
root@SHIVDEEP:~
root@SHIVDEEP:~
root@SHIVDEEP:~
Q5. Write a shell script that finds prime numbers inside a user specified range?
Ans:-
#!/bin/bash
echo "Enter the starting number:"
read start
echo "Enter the ending number:"
read end
echo "Prime numbers between $start and $end are:"
for (( num=$start; num<=end; num++ ))
do
if [ $num -lt 2 ]; then
continue
fi
is_prime=1
for (( i=2; i*i<=num; i++ ))
do
if (( num % i == 0 )); then
is_prime=0
break
fi
done
if [ $is_prime -eq 1 ]; then
echo -n "$num "
fi
done
echo
root@SHIVDEEP:~
root@SHIVDEEP:~
root@SHIVDEEP:~
root@SHIVDEEP:~
root@SHIVDEEP:~
Q6. Write a shell script to determine whether a given string is palindrome?
Ans:-
#!/bin/bash
echo "Enter a string:"
read str
rev_str=$(echo "$str" | rev)
if [ "$str" = "$rev_str" ]; then
echo "The string \"$str\" is a palindrome."
else
echo "The string \"$str\" is not a palindrome."
Fi
root@SHIVDEEP:~
root@SHIVDEEP:~
root@SHIVDEEP:~
root@SHIVDEEP:~
root@SHIVDEEP:~
root@SHIVDEEP:~
root@SHIVDEEP:~
root@SHIVDEEP:~
Q7. Write shell script that CO1 allows users to create, delete, and list files in a directory?
Ans:-
#!/bin/bash
while true
do
echo ""
echo "Choose an option:"
echo "1. Create a file"
echo "2. Delete a file"
echo "3. List files"
echo "4. Exit"
read -p "Enter your choice [1-4]: " choice
case $choice in
1)
read -p "Enter the filename to create: " filename
if [ -e "$filename" ]; then
echo "File '$filename' already exists."
else
touch "$filename"
echo "File '$filename' created."
fi
;;
2)
read -p "Enter the filename to delete: " filename
if [ -e "$filename" ]; then
rm "$filename"
echo "File '$filename' deleted."
else
echo "File '$filename' does not exist."
fi
;;
3)
echo "Listing files in current directory:"
ls -l
;;
4)
echo "Exiting."
break
;;
*)
echo "Invalid choice! Please enter a number between 1 and 4."
;;
esac
done
root@SHIVDEEP:~
root@SHIVDEEP:~
root@SHIVDEEP:~
root@SHIVDEEP:~
Q8. Write a shell script that Count Lines in Each File in a Directory?
Ans:-
#!/bin/bash
echo "Counting lines in each file in the current directory..."
for file in *
do
if [ -f "$file" ]; then
lines=$(wc -l < "$file")
echo "$file: $lines lines"
fi
done
root@SHIVDEEP:~
root@SHIVDEEP:~
root@SHIVDEEP:~
root@SHIVDEEP:~
root@SHIVDEEP:~
root@SHIVDEEP:~
Q9. Write a shell script that find and Replace Text in Files?
Ans:-
#!/bin/bash
echo "Enter the text to find:"
read find_text
echo "Enter the replacement text:"
read replace_text
echo "Enter the filename(s) (separated by space):"
read -a files
for file in "${files[@]}"
do
if [ -f "$file" ]; then
sed -i "s/${find_text}/${replace_text}/g" "$file"
echo "Replaced '$find_text' with '$replace_text' in $file"
else
echo "File '$file' does not exist."
fi
done
root@SHIVDEEP:~
root@SHIVDEEP:~
root@SHIVDEEP:~
root@SHIVDEEP:~
root@SHIVDEEP:~
Q10. Write a shell script that find Files Modified in the Last N Days?
Ans:-
#!/bin/bash
echo "Enter the number of days (N):"
read days
echo "Files modified in the last $days day(s):"
find . -type f -mtime -$days
root@SHIVDEEP:~
root@SHIVDEEP:~
root@SHIVDEEP:~
root@SHIVDEEP:~
root@SHIVDEEP:~