OS Home Lab Assignment Set1 Complete Solutions Final
OS Home Lab Assignment Set1 Complete Solutions Final
Set 1, Problem 1
Problem Statement:
#!/bin/bash
is_prime=1
for (( i=2; i<=number/2; i++ ))
do
if (( number%i == 0 )); then
is_prime=0
break
fi
done
Program Output:
Justification:
This script uses a loop to check if the number has any divisors other than 1 and itself. If it
finds any divisors, it sets is_prime to 0, indicating the number is not prime.
Set 1, Problem 2
Problem Statement:
#!/bin/bash
factorial=1
Program Output:
Justification:
This script calculates the factorial by multiplying numbers from 1 up to the given input.
Using a loop, it iteratively calculates the factorial value and stores it in the 'factorial'
variable.
Set 1, Problem 3
Problem Statement:
#!/bin/bash
generate_fibonacci() {
a=0
b=1
for (( i=0; i<$1; i++ ))
do
echo -n "$a "
fn=$((a + b))
a=$b
b=$fn
done
}
Program Output:
This script defines a function to generate the Fibonacci sequence. It uses a for loop to
generate the Fibonacci numbers up to the specified term.
Set 1, Problem 4
Problem Statement:
Create a script that takes an array of numbers from the user and prints the even numbers
using a loop.
#!/bin/bash
even_numbers=()
for num in "${arr[@]}"; do
if (( num % 2 == 0 )); then
even_numbers+=($num)
fi
done
Program Output:
Justification:
This script uses an array to store input numbers. It iterates through the array and checks for
even numbers by using the modulo operator.
Set 1, Problem 5
Problem Statement:
Write a script to reverse the contents of a file using the tac command.
#!/bin/bash
tac $filename
Program Output:
Justification:
The 'tac' command is used to reverse the contents of a file. This script takes the filename
from the user and uses 'tac' to reverse its contents.
Set 1, Problem 6
Problem Statement:
Use grep in a shell script to find all lines containing a specific word in a file.
#!/bin/bash
Program Output:
Justification:
This script uses 'grep' to search for the specified word within the file. The '-n' option
outputs the line number along with the matching lines.
Set 1, Problem 7
Problem Statement:
Write a script to count the number of words in a file using the wc command.
#!/bin/bash
Program Output:
This script uses the 'wc' (word count) command to count the words in the specified file. The
'-w' option ensures that the word count is returned.
Set 1, Problem 8
Problem Statement:
Create a script to search for a file in a directory using the find command.
#!/bin/bash
Program Output:
Justification:
The 'find' command is used to search for a file in a given directory. The script takes the
filename and directory as input and returns the file path if found.
Set 1, Problem 9
Problem Statement:
Write a script to list all files in a directory with read permissions for the owner using the ls
command.
#!/bin/bash
Program Output:
Justification:
This script lists files in a directory and filters those that have read permissions for the
owner using 'ls' and 'grep'.
Set 1, Problem 10
Problem Statement:
Use awk to calculate the sum of numbers in a specific column of a file from a shell script.
#!/bin/bash
Program Output:
Justification:
This script uses 'awk' to sum the numbers in a specified column of a file. It iterates through
the file and sums up the values in the given column.
Set 1, Problem 11
Problem Statement:
#!/bin/bash
read -p 'Enter filename: ' filename
if [ -f "$filename" ]; then
echo "$filename exists."
else
echo "$filename does not exist."
fi
Program Output:
Justification:
This script uses the '-f' option to check if the provided file exists. If the file is found, it
returns a message confirming its existence.
Set 1, Problem 12
Problem Statement:
Write a script to print the system's current disk usage using the df -h command.
#!/bin/bash
df -h
Program Output:
Justification:
This script uses the 'df -h' command to display the current disk usage in a human-readable
format, which shows the size, used space, and available space on the system.
Set 1, Problem 13
Problem Statement:
#!/bin/bash
read -p 'Enter numbers: ' -a arr
max=${arr[0]}
for num in "${arr[@]}"; do
if (( num > max )); then
max=$num
fi
done
echo "Largest number is: $max"
Program Output:
Justification:
This script takes an array of numbers as input and iterates through each element,
comparing and updating the largest number found.
Set 1, Problem 14
Problem Statement:
Write a script to remove all empty lines from a file using the sed command.
#!/bin/bash
read -p 'Enter filename: ' filename
sed '/^$/d' $filename
Program Output:
Justification:
The script uses 'sed' to remove any empty lines from the given file. The regular expression
'^$' matches empty lines, and the 'd' command deletes them.
Set 1, Problem 15
Problem Statement:
Use sed to replace all instances of a word in a file with another word from within a shell
script.
#!/bin/bash
read -p 'Enter filename: ' filename
read -p 'Enter the word to replace: ' oldword
read -p 'Enter the new word: ' newword
sed -i "s/$oldword/$newword/g" $filename
Program Output:
Justification:
The script uses 'sed' to replace all occurrences of the old word with the new word in the
specified file. The '-i' option allows in-place editing of the file.
Set 1, Problem 16
Problem Statement:
Write a script to calculate the average of a list of numbers using arrays and a loop.
#!/bin/bash
read -p 'Enter numbers: ' -a arr
sum=0
for num in "${arr[@]}"; do
sum=$((sum + num))
done
avg=$(echo "$sum / ${#arr[@]}" | bc -l)
echo "Average is: $avg"
Program Output:
Justification:
This script calculates the sum of the elements in the array and then divides it by the total
number of elements to get the average.
Set 1, Problem 17
Problem Statement:
Create a script to display all currently running processes using the ps command.
#!/bin/bash
ps aux
Program Output:
Justification:
This script uses the 'ps aux' command to display all currently running processes along with
details like process ID, CPU usage, memory usage, etc.
Set 1, Problem 18
Problem Statement:
Write a script to display the last 10 lines of a file using the tail command.
#!/bin/bash
read -p 'Enter filename: ' filename
tail -n 10 $filename
Program Output:
Justification:
The 'tail' command is used here to display the last 10 lines of the file specified by the user.
The '-n' option ensures only the last 10 lines are displayed.
Set 1, Problem 19
Problem Statement:
Use grep to display the line numbers where a word occurs in a file.
Shell Script Program:
#!/bin/bash
read -p 'Enter word: ' word
read -p 'Enter filename: ' filename
grep -n "$word" $filename
Program Output:
Justification:
This script uses 'grep' to search for occurrences of the specified word in the file and the '-n'
option to display the line numbers where the word is found.
Set 1, Problem 20
Problem Statement:
Write a script to monitor CPU usage and alert if usage exceeds 80% using the top command.
#!/bin/bash
cpu_usage=$(top -bn1 | grep "Cpu(s)" | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | awk '{print 100 -
$1}')
if (( $(echo "$cpu_usage > 80" | bc -l) )); then
echo "Alert: CPU usage is above 80%."
fi
Program Output:
Justification:
This script monitors CPU usage by parsing the output of the 'top' command. It triggers an
alert if the CPU usage exceeds 80%.
Set 1, Problem 21
Problem Statement:
Create a script that counts the number of files and subdirectories in a directory using a loop
and the ls command.
#!/bin/bash
read -p 'Enter directory: ' dir
file_count=$(find "$dir" -type f | wc -l)
dir_count=$(find "$dir" -type d | wc -l)
echo "Number of files: $file_count"
echo "Number of subdirectories: $dir_count"
Program Output:
Justification:
This script uses 'find' to count the number of files and directories separately, using the '-
type f' and '-type d' options respectively.
Set 1, Problem 22
Problem Statement:
#!/bin/bash
add() {
echo $(($1 + $2))
}
subtract() {
echo $(($1 - $2))
}
multiply() {
echo $(($1 * $2))
}
divide() {
echo $(($1 / $2))
}
echo "Enter two numbers:"
read num1 num2
echo "Choose operation (+, -, *, /):"
read op
case $op in
"+") add $num1 $num2 ;;
"-") subtract $num1 $num2 ;;
"*") multiply $num1 $num2 ;;
"/") divide $num1 $num2 ;;
*) echo "Invalid operation" ;;
esac
Program Output:
Justification:
This script defines functions for addition, subtraction, multiplication, and division, and uses
a case statement to choose the appropriate function based on user input.
Set 1, Problem 23
Problem Statement:
#!/bin/bash
is_palindrome() {
local str="$1"
local rev=$(echo "$str" | rev)
if [ "$str" == "$rev" ]; then
echo "$str is a palindrome."
else
echo "$str is not a palindrome."
fi
}
read -p 'Enter a string: ' string
is_palindrome "$string"
Program Output:
Justification:
The script defines a function to reverse the input string and compares it with the original to
check if it's a palindrome.
Set 1, Problem 24
Problem Statement:
Use awk to print only the first and third columns of a file.
#!/bin/bash
read -p 'Enter filename: ' filename
awk '{print $1, $3}' $filename
Program Output:
Justification:
This script uses 'awk' to print only the first and third columns of the file, assuming a space-
separated file structure.
Set 1, Problem 25
Problem Statement:
Program Output:
Justification:
This script defines a function to calculate the factorial of a number by multiplying all
integers up to the input value.
Set 1, Problem 26
Problem Statement:
Use find to search for files modified in the last 7 days from a shell script.
#!/bin/bash
read -p 'Enter directory: ' directory
find $directory -type f -mtime -7
Program Output:
Justification:
This script uses 'find' with the '-mtime' option to search for files modified in the last 7 days
in the specified directory.
Set 1, Problem 27
Problem Statement:
Write a script to print the current date and time in a custom format using the date
command.
#!/bin/bash
date +"%Y-%m-%d %H:%M:%S"
Program Output:
Justification:
This script uses the 'date' command with a custom format to print the date and time in
'YYYY-MM-DD HH:MM:SS' format.
Set 1, Problem 28
Problem Statement:
Use sed to delete all lines in a file that contain a specific word from within a shell script.
#!/bin/bash
read -p 'Enter word: ' word
read -p 'Enter filename: ' filename
sed -i "/$word/d" $filename
Program Output:
Justification:
The script uses 'sed' to delete all lines containing the specified word. The '-i' option edits the
file in place.
Set 1, Problem 29
Problem Statement:
Create a script to compress all .txt files in a directory using the gzip command.
#!/bin/bash
read -p 'Enter directory: ' directory
find $directory -type f -name "*.txt" -exec gzip {} \;
Program Output:
Justification:
This script finds all '.txt' files in the specified directory and compresses them using the 'gzip'
command.
Set 1, Problem 30
Problem Statement:
Write a script to send an email notification when a file is updated using the mail command.
#!/bin/bash
read -p 'Enter filename: ' filename
read -p 'Enter email address: ' email
inotifywait -m "$filename" | while read event; do
echo "File $filename was updated." | mail -s "File Update Alert" $email
done
Program Output:
Justification:
This script uses 'inotifywait' to monitor the file for updates and sends an email notification
using the 'mail' command whenever the file is modified.
Set 1, Problem 31
Problem Statement:
#!/bin/bash
read -p 'Enter a number: ' num
if (( num > 0 )); then
echo "$num is positive."
elif (( num < 0 )); then
echo "$num is negative."
else
echo "$num is zero."
fi
Program Output:
Justification:
This script uses if-else statements to check if the number is positive, negative, or zero and
prints the appropriate message.
Set 1, Problem 32
Problem Statement:
Create a script to find the sum of all even numbers between 1 and 100 using a loop.
#!/bin/bash
sum=0
for (( i=2; i<=100; i+=2 )); do
sum=$((sum + i))
done
echo "Sum of even numbers between 1 and 100 is $sum"
Program Output:
Justification:
The script uses a for loop to iterate through all even numbers from 2 to 100 and calculates
their sum.
Set 1, Problem 33
Problem Statement:
Write a script to check whether a file is a directory or a regular file using the test command.
#!/bin/bash
read -p 'Enter filename or directory name: ' path
if [ -d "$path" ]; then
echo "$path is a directory."
elif [ -f "$path" ]; then
echo "$path is a regular file."
else
echo "$path does not exist."
fi
Program Output:
Justification:
This script uses the test command '-d' to check if the path is a directory and '-f' to check if
it's a regular file.
Set 1, Problem 34
Problem Statement:
#!/bin/bash
read -p 'Enter filename: ' filename
read -p 'Enter column number: ' column
awk '{sum+=$column} END {print "Average is: " sum/NR}' $filename
Program Output:
Justification:
This script uses 'awk' to calculate the sum of numbers in the specified column and divides it
by the total number of lines to compute the average.
Set 1, Problem 35
Problem Statement:
#!/bin/bash
to_uppercase() {
echo "$1" | tr '[:lower:]' '[:upper:]'
}
read -p 'Enter a string: ' string
to_uppercase "$string"
Program Output:
Justification:
The script uses 'tr' to convert the input string to uppercase within a function and prints the
result.
Set 1, Problem 36
Problem Statement:
Write a script to find the minimum and maximum number from an array using a loop.
#!/bin/bash
read -p 'Enter numbers: ' -a arr
min=${arr[0]}
max=${arr[0]}
for num in "${arr[@]}"; do
if (( num > max )); then
max=$num
fi
if (( num < min )); then
min=$num
fi
done
echo "Minimum: $min"
echo "Maximum: $max"
Program Output:
Justification:
This script iterates through the array to find the minimum and maximum values by
comparing each element.
Set 1, Problem 37
Problem Statement:
Create a script to calculate the greatest common divisor (GCD) of two numbers using a
function.
Shell Script Program:
#!/bin/bash
gcd() {
local a=$1
local b=$2
while [ $b -ne 0 ]; do
local temp=$b
b=$((a % b))
a=$temp
done
echo $a
}
read -p 'Enter first number: ' num1
read -p 'Enter second number: ' num2
gcd $num1 $num2
Program Output:
Justification:
This script calculates the GCD of two numbers using the Euclidean algorithm within a
function.
Set 1, Problem 38
Problem Statement:
Use find to list all files in a directory that are larger than 1MB.
#!/bin/bash
read -p 'Enter directory: ' dir
find $dir -type f -size +1M
Program Output:
Justification:
The script uses 'find' with the '-size' option to list all files larger than 1MB in the specified
directory.
Set 1, Problem 39
Problem Statement:
#!/bin/bash
read -p 'Enter a string: ' str
rev=""
for (( i=${#str}-1; i>=0; i-- )); do
rev="$rev${str:$i:1}"
done
echo "Reversed string: $rev"
Program Output:
Justification:
This script uses a loop to iterate through the characters of the input string in reverse order
and constructs the reversed string.
Set 1, Problem 40
Problem Statement:
#!/bin/bash
string_length() {
echo "${#1}"
}
read -p 'Enter a string: ' str
echo "Length of the string is $(string_length "$str")"
Program Output:
Justification:
This script defines a function that returns the length of the input string using the parameter
expansion '${#}' to calculate the string's length.