Os Ass3
Os Ass3
PROGRAM 1:Write a shell script to generate marksheet of a student. Take 3 subjects, calculate
and display total marks, percentage and Class obtained by the student. Take marks from the user
terminal.
# Function to calculate total marks
calculate_total_marks() {
total=$((subject1 + subject2 + subject3))
echo $total
}
# Calculating percentage
percentage=$(calculate_percentage $total_marks)
# Displaying marksheet
echo "-----------------------"
echo " MARKSHEET "
echo "-----------------------"
echo "Subject 1: $subject1"
echo "Subject 2: $subject2"
echo "Subject 3: $subject3"
echo "-----------------------"
Page no. 1
echo "Total Marks: $total_marks"
echo "Percentage: $percentage%"
echo "Class Obtained: $class_obtained"
PROGRAM 2: Write a shell script to find the largest among the 3 given numbers in UNIX / Linux.
Using looping statements.
# Function to find the largest number among three
find_largest() {
largest=$1
for num in "$@"; do
if [ $num -gt $largest ]; then
largest=$num
fi
done
echo $largest
}
PROGRAM 3: Write a menu driven shell script which will print the following menu and execute
the given task.
a) Display calendar of current month
b) Display today’s date and time
c) Display usernames those are currently logged in the
system
d) Display your name at given x, y position
e) Display your terminal number
f) Exit
while true; do
# Displaying the menu
echo "MENU:"
echo "a) Display calendar of current month"
echo "b) Display today’s date and time"
echo "c) Display usernames those are currently logged in the system"
Page no. 2
echo "d) Display your name at given x, y position"
echo "e) Display your terminal number"
echo "f) Exit"
echo -n "Enter your choice: "
read choice
case $choice in
a)
# Display calendar of current month
cal
;;
b)
# Display today's date and time
date
;;
c)
# Display usernames currently logged in
who
;;
d)
# Display name at given x, y position
echo -n "Enter X position: "
read x
echo -n "Enter Y position: "
read y
tput cup $x $y
echo "Your Name"
;;
e)
# Display terminal number
tty
;;
f)
# Exit the program
echo "Exiting..."
exit 0
;;
*)
echo "Invalid option, please choose again."
;;
esac
done
Page no. 3
PROGRAM 4: Write a shell script that displays a list of all the files in the current directory.
# Displaying list of files in current directory
echo "List of files in the current directory:"
ls
PROGRAM 5: Write a shell script that receives any number of file names as arguments checks if
every argument supplied is a file or a directory and reports accordingly. whenever the argument is
a file or directory.
# Function to check if argument is a file or directory and report accordingly
check_file_or_directory() {
if [ -e "$1" ]; then
if [ -f "$1" ]; then
echo "$1 is a file."
elif [ -d "$1" ]; then
echo "$1 is a directory."
else
echo "$1 is not a regular file or directory."
fi
else
echo "$1 does not exist."
fi
}
Page no. 4
echo "Factorial of $num is $result"
else
echo "Invalid input. Please enter a positive integer."
fi
PROGRAM 7: Write a Shell script to list all of the directory files in a directory
# Check if directory argument is provided
if [ $# -eq 0 ]; then
echo "Usage: $0 <directory>"
exit 1
fi
directory="$1"
# Check if directory exists
if [ ! -d "$directory" ]; then
echo "Error: Directory '$directory' not found."
exit 1
fi
PROGRAM 9: Write Shell Script to calculate power of a number using while & for loop
# Function to calculate power of a number using while loop
calculate_power_while() {
base=$1
Page no. 5
exponent=$2
result=1
while [ $exponent -gt 0 ]; do
result=$((result * base))
exponent=$((exponent - 1))
done
echo $result
}
Page no. 6
# Taking input from the user
echo -n "Enter first number: "
read num1
echo -n "Enter second number: "
read num2
# Calculating HCF
calculate_hcf $num1 $num2
PROGRAM 12: Write Shell Script Counting characters, words & lines in the file.
# Function to count characters, words, and lines in a file
count_characters_words_lines() {
file="$1"
if [ ! -f "$file" ]; then
echo "Error: File '$file' not found."
exit 1
fi
Page no. 7
# Calling function to count characters, words, and lines
count_characters_words_lines "$filename"
PROGRAM 14: Write Shell Script for simple calculator to perform addition subtraction
multiplication and division based on the symbol using case statements.
# Function to perform addition
addition() {
result=$(echo "$1 + $2" | bc)
echo "Result: $result"
}
Page no. 8
# Taking input from the user
echo "Enter the operation:"
read operation
echo "Enter the first number:"
read num1
echo "Enter the second number:"
read num2
PROGRAM 15: Write Shell Script for Add array elements - Shell Script
# Define an array
my_array=(10 20 30 40 50)
Page no. 9
# Call the function to reverse array elements
reverse_array_elements
PROGRAM 17: Write shell script program to addition, subtraction, multiplication of two matrix.
# Function to transpose a matrix
transpose_matrix() {
local rows=$1
local cols=$2
local -n matrix=$3
local transposed=()
for ((j = 0; j < cols; j++)); do
for ((i = 0; i < rows; i++)); do
transposed[$((j * rows + i))]=${matrix[$i * $cols + $j]}
done
done
Page no. 10
local rows1=$1
local cols1=$2
local cols2=$3
local -n result=$4
local -n matrix1=$5
local -n matrix2=$6
for ((i = 0; i < rows1; i++)); do
for ((j = 0; j < cols2; j++)); do
local sum=0
for ((k = 0; k < cols1; k++)); do
sum=$((sum + matrix1[$((i * cols1 + k))] * matrix2[$((k * cols2 + j))]))
done
result[$((i * cols2 + j))]=$sum
done
done
}
# Perform addition
add_result=()
add_matrices $rows $cols add_result matrix1 matrix2
echo "Addition of two matrices:"
print_matrix $rows $cols add_result
echo
# Perform subtraction
subtract_result=()
subtract_matrices $rows $cols subtract_result matrix1 matrix2
echo "Subtraction of two matrices:"
print_matrix $rows $cols subtract_result
echo
Page no. 11
# Perform multiplication
multiply_result=()
multiply_matrices $rows $cols $cols multiply_result matrix1 matrix2
echo "Multiplication of two matrices:"
print_matrix $rows $cols multiply_result
PROGRAM 18: Write Shell program to add two numbers using functions.
# Function to add two numbers
add_numbers() {
local result=$(( $1 + $2 ))
echo "The sum of $1 and $2 is: $result"
}
PROGRAM 19: Write Shell Script to compare two strings in unix shell script.
# Taking input from the user
echo -n "Enter first string: "
read string1
echo -n "Enter second string: "
read string2
PROGRAM 20: Write a shell program to demonstrate Equal operator (=), Not Equal operator
(!=),Less than (\<),Greater than (\>) operation on two strings.
# Taking input from the user
echo -n "Enter first string: "
read string1
echo -n "Enter second string: "
read string2
Page no. 12
# Demonstrating comparison operators
echo "Comparison results:"
echo "-------------------"
echo "Equal operator (=):"
if [ "$string1" = "$string2" ]; then
echo "The strings are equal."
else
echo "The strings are not equal."
fi
PROGRAM 21: Write a Program for system calls of Unix operating systems (opendir, readdir,
closedir).
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
int main() {
DIR *directory;
struct dirent *entry;
// Open directory
directory = opendir(".");
if (directory == NULL) {
perror("Unable to open directory");
exit(EXIT_FAILURE);
}
Page no. 13
printf("Contents of the directory:\n");
while ((entry = readdir(directory)) != NULL) {
printf("%s\n", entry->d_name);
}
// Close directory
closedir(directory);
return 0;
}
PROGRAM 22: Write a Program for Process system calls of Unix operating systems (fork, getpid,
exit).
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main() {
pid_t pid;
if (pid < 0) {
// Fork failed
perror("Fork failed");
return 1;
} else if (pid == 0) {
// Child process
printf("Child process:\n");
printf("PID: %d\n", getpid());
printf("Parent PID: %d\n", getppid());
// Child exits
exit(0);
} else {
// Parent process
printf("Parent process:\n");
printf("PID: %d\n", getpid());
printf("Child PID: %d\n", pid);
// Wait for child to finish
wait(NULL);
}
return 0;
}
Page no. 14
PROGRAM 23: Write the program to implement the system calls wait ( ) and exit ( ).
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
int main() {
pid_t pid;
int status;
if (pid < 0) {
// Fork failed
perror("Fork failed");
return 1;
} else if (pid == 0) {
// Child process
printf("Child process is running...\n");
// Simulating some task in the child process
sleep(3);
printf("Child process is done.\n");
// Child exits
exit(42);
} else {
// Parent process
printf("Parent process is waiting for child...\n");
// Wait for child to finish and collect its exit status
wait(&status);
if (WIFEXITED(status)) {
printf("Child process exited with status: %d\n", WEXITSTATUS(status));
}
printf("Parent process is done.\n");
}
return 0;
}
int main() {
printf("Parent process executing...\n");
if (pid < 0) {
// Fork failed
perror("Fork failed");
exit(EXIT_FAILURE);
Page no. 15
} else if (pid == 0) {
// Child process
printf("Child process executing...\n");
return 0;
}
int main() {
printf("Parent process executing...\n");
if (pid < 0) {
// Fork failed
perror("Fork failed");
exit(EXIT_FAILURE);
} else if (pid == 0) {
// Child process
printf("Child process executing...\n");
Page no. 16
}
return 0;
}
int main() {
int input_fd, output_fd; // File descriptors for input and output files
ssize_t bytes_read, bytes_written;
char buffer[BUFFER_SIZE];
// Read from the input file and write to the output file
while ((bytes_read = read(input_fd, buffer, BUFFER_SIZE)) > 0) {
bytes_written = write(output_fd, buffer, bytes_read);
if (bytes_written != bytes_read) {
perror("Write error");
close(input_fd);
close(output_fd);
exit(EXIT_FAILURE);
}
}
if (bytes_read == -1) {
perror("Read error");
close(input_fd);
close(output_fd);
Page no. 17
exit(EXIT_FAILURE);
}
return 0;
}
Page no. 18