Os Lab Part 2
Os Lab Part 2
Write a shell script that calculates the average marks of a student considering that
number of subjects are five and also calculate the corresponding grade on average.
Ans:
#!/bin/bash
total_marks=$((total_marks + marks))
done
case $choice in
1) # Addition
read -p "Enter the first number: " num1
read -p "Enter the second number: " num2
result=$(bc <<< "$num1 + $num2")
echo "Result: $num1 + $num2 = $result"
;;
2) # Subtraction
read -p "Enter the first number: " num1
read -p "Enter the second number: " num2
result=$(bc <<< "$num1 - $num2")
echo "Result: $num1 - $num2 = $result"
;;
3) # Multiplication
read -p "Enter the first number: " num1
read -p "Enter the second number: " num2
result=$(bc <<< "$num1 * $num2")
echo "Result: $num1 * $num2 = $result"
;;
4) # Division
read -p "Enter the dividend: " num1
read -p "Enter the divisor: " num2
Menu:
1) Addition
2) Subtraction
3) Multiplication
4) Division
5) Exit
Enter your choice (1/2/3/4/5): 4
Enter the numerator: 10
Enter the denominator: 0
Error: Division by zero is not allowed.
Menu:
1) Addition
2) Subtraction
3) Multiplication
4) Division
5) Exit
Enter your choice (1/2/3/4/5): 5
Exiting the program.
10. Write a shell script to print a number in reverse order.
Ans:
#!/bin/bash
Enter a number: 5
Factorial of 5 is 120
echo "$reversed"
}
16.
Write a shell script to draw the following patterns:
a) b) c) d) e) f) g)
| **** 1 1 1 * * * * * * * *
| | *** 12 22 23 * * * * * * * *
| | | ** 123 3 3 3 456 * * * * * * * *
| | | | * 1234 4 444 7 8 9 10 * * * * * * * *
#!/bin/bash
echo "a)"
for ((i=1; i<=4; i++)); do
for ((j=1; j<=i; j++)); do
echo -n " *"
done
echo ""
done
echo -e "\nb)"
for ((i=1; i<=4; i++)); do
for ((j=1; j<=i; j++)); do
echo -n " $i"
done
echo ""
done
echo -e "\nc)"
num=1
for ((i=1; i<=4; i++)); do
for ((j=1; j<=i; j++)); do
echo -n " $num"
((num++))
done
echo ""
done
echo -e "\nd)"
num=1
for ((i=1; i<=4; i++)); do
for ((j=1; j<=i; j++)); do
echo -n " $i"
done
((i++))
echo ""
done
echo -e "\ne)"
for ((i=1; i<=4; i++)); do
for ((j=1; j<=i; j++)); do
echo -n " *"
done
echo ""
done
echo -e "\nf)"
for ((i=1; i<=4; i++)); do
for ((j=4; j>i; j--)); do
echo -n " "
done
for ((k=1; k<=2*i-1; k++)); do
echo -n " *"
done
echo ""
done
echo -e "\ng)"
num=1
for ((i=1; i<=4; i++)); do
for ((j=1; j<=i; j++)); do
echo -n " $num"
((num++))
done
echo ""
done
17.
Write a menu driven shell script-
a) Create two blank files at a time,
b) Add some content in an existing file,
c) Copy the content of one file into another file,
d) Search and print the lines which contain a specific word,
e) Count the total number of files whose name starts with a vowel.
Ans:
#!/bin/bash
while true; do
echo "Menu:"
echo "a) Create two blank files"
echo "b) Add content to an existing file"
echo "c) Copy the content of one file to another"
echo "d) Search for specific word in a file"
echo "e) Count files starting with a vowel"
echo "f) Exit"
case $choice in
a)
# Create two blank files
touch file1.txt file2.txt
echo "Two blank files created: file1.txt and file2.txt"
;;
b)
# Add content to an existing file
read -p "Enter the filename to add content: " filename
if [ -f "$filename" ]; then
read -p "Enter the content to add: " content
echo "$content" >> "$filename"
echo "Content added to $filename"
else
echo "File does not exist. Please enter a valid filename."
fi
;;
c)
# Copy the content of one file to another
read -p "Enter the source filename: " source_file
read -p "Enter the destination filename: " dest_file
if [ -f "$source_file" ]; then
cp "$source_file" "$dest_file"
echo "Content of $source_file copied to $dest_file"
else
echo "Source file does not exist. Please enter a valid source filename."
fi
;;
d)
# Search and print lines containing a specific word
read -p "Enter the filename to search: " search_file
if [ -f "$search_file" ]; then
read -p "Enter the word to search: " search_word
grep -i "$search_word" "$search_file"
else
echo "File does not exist. Please enter a valid filename."
fi
;;
e)
# Count files starting with a vowel
count=$(ls -l | grep -i "^[aeiou]" | wc -l)
echo "Number of files starting with a vowel: $count"
;;
f)
# Exit the program
echo "Exiting the program. Goodbye!"
exit 0
;;
*)
echo "Invalid choice. Please select a valid option (a/b/c/d/e/f)."
;;
esac
done
Menu:
a) Create two blank files
b) Add content to an existing file
c) Copy content from one file to another
d) Search and print lines with a specific word
e) Count files starting with a vowel
f) Exit
Enter your choice (a/b/c/d/e/f): a
Enter the names of two blank files (space-separated): file1.txt file2.txt
Two blank files created: file1.txt and file2.txt
Menu:
a) Create two blank files
b) Add content to an existing file
c) Copy content from one file to another
d) Search and print lines with a specific word
e) Count files starting with a vowel
f) Exit
Enter your choice (a/b/c/d/e/f): b
Enter the name of the file to which you want to add content: file1.txt
Enter the content you want to add: This is some content for file1.
Content added to file1.txt
Menu:
a) Create two blank files
b) Add content to an existing file
c) Copy content from one file to another
d) Search and print lines with a specific word
e) Count files starting with a vowel
f) Exit
Enter your choice (a/b/c/d/e/f): d
Enter the file to search in: file1.txt
Enter the word you want to search: content
This is some content for file1.
18. Write a shell script which calculates total number of files and sub-directories present in
a specific directory.
Ans:
#!/bin/bash
# Prompt the user to enter the directory path
read -p "Enter the directory path: " directory_path
# Check if the directory exists
if [ -d "$directory_path" ]; then
# Use 'find' to count files and directories
num_files=$(find "$directory_path" -type f | wc -l)
num_directories=$(find "$directory_path" -type d | wc -l)
19. Write a shell script which counts total number lines in a file without using wc -l
command.
Ans:
#!/bin/bash
20. Write a shell script which works similar to wc command. Script can receive the
options – -l, -w and -c to indicate whether number of lines, number of words, number
of characters from the input string is to be counted. The user may use any all of
options. Your script should be intelligent enough to identify invalid options and reject
them.
#!/bin/bash
# Initialize counters
count_lines=false
count_words=false
count_characters=false
if $count_lines; then
echo "Lines: $line_count"
fi
if $count_words; then
echo "Words: $word_count"
fi
if $count_characters; then
echo "Characters: $char_count"
fi
}
21. A shell script receives even number of file names, suppose 4 file names are
supplied, then 1st file should get copied into the 2nd file and 3rd file should get copied
into the 4th file, so on. If odd number of files are supplied, no copy should take place
and an error message should be displayed.
Ans:
#!/bin/bash
shift 2
done
22. Write a shell script to implement the Fibonacci series using function.
#!/bin/bash
if [ $n -eq 0 ]; then
echo "Fibonacci Series: "
echo "0"
elif [ $n -eq 1 ]; then
echo "Fibonacci Series: "
echo "0"
echo "1"
else
echo "Fibonacci Series: "
echo "0"
echo "1"
i=2
while [ $i -lt $n ]; do
c=$((a + b))
echo "$c"
a=$b
b=$c
i=$((i + 1))
done
fi
}
23. Write a shell script to check whether a number is Palindrome or not by using function.
#!/bin/bash
25. Write a script to implement First Come First Served (FCFS) CPU Scheduling Algorithm.
#!/bin/bash
Enter the arrival time and burst time of each process (comma-separated): 0,6 2,3 4,1 5,2
Process | Arrival Time | Burst Time | Waiting Time | Turnaround Time
P0 | 0 | 6 | 0 | 6
P1 | 2 | 3 | 4 | 7
P2 | 4 | 1 | 6 | 7
P3 | 5 | 2 | 6 | 8
Average Waiting Time: 4.00
Average Turnaround Time: 7.00
26. Write a script to implement Shortest Job First (SJF) CPU Scheduling Algorithm.
#!/bin/bash
completion_time[min_index]=$(($current_time + ${burst_time[min_index]}))
completed[min_index]=1
current_time=${completion_time[min_index]}
done
Enter the burst time of each process (comma-separated): 6,3 3,2 1,1 2,2
Process | Burst Time | Waiting Time | Turnaround Time
P2 | 1 | 0 | 1
P1 | 2 | 1 | 3
P3 | 2 | 3 | 5
P0 | 3 | 5 | 8
Average Waiting Time: 2.25
Average Turnaround Time: 4.25
27. Write a script to implement Round Robin (RR) CPU Scheduling Algorithm.
#!/bin/bash
local completed=0
local current_time=0
# Simulate RR scheduling
while true; do
local done=false
for ((i=0; i<$n; i++)); do
if ((arrival_time[i] <= current_time)) && ((remaining_time[i] > 0)); then
done=true
if ((remaining_time[i] <= time_quantum)); then
current_time=$((current_time + remaining_time[i]))
remaining_time[i]=0
local turnaround_time=$((current_time - arrival_time[i]))
local waiting_time=$((turnaround_time - burst_time[i]))
completed=$((completed + 1))
echo "P$i ${arrival_time[i]} ${burst_time[i]} $current_time
$turnaround_time $waiting_time"
else
current_time=$((current_time + time_quantum))
remaining_time[i]=$((remaining_time[i] - time_quantum))
fi
fi
done
if !$done; then
break
fi
done
}
# Call the Round Robin scheduling function
round_robin_scheduling "${arrival_time[*]}" "${burst_time[*]}" $time_quantum
Enter process names and burst times (comma-separated): P1,8 P2,6 P3,4
Process | Burst Time | Waiting Time | Turnaround Time
P0 | 8 | 0 | 2
P1 | 6 | 2 | 8
P2 | 4 | 8 | 12
P0 | 4 | 2 | 8
P1 | 2 | 8 | 10
P0 | 2 | 8 | 10
Average Waiting Time: 5.33
Average Turnaround Time: 8.67