Os Ass2
Os Ass2
LAB ASSIGNMENT - 2
CT10709: OPERATING SYSTEM
Page no.1
Khushi Sahu OS assignment 2
LAB ASSIGNMENT 2
Program 1: Linux shell script program to create and print the values of variable.
country="India"
year=2021
echo "Country name: $country"
echo "Year: $year"
Page no.2
Khushi Sahu OS assignment 2
Program 3: Linux shell script program to swap two numbers using third variable and
without using third variable.
Using third variable:
num1=10
num2=20
echo "Before Swapping : Num1: $num1 Num2: $num2"
num3=$num1
num1=$num2
num2=$num3
echo "After Swapping: Num1: $num1 Num2: $num2"
Program 4: Linux shell script program to print program name using command line
argument and also Linus shell script program to execute ‘ls’ command.
echo "Script program Name: $0"
cd /
ls
Page no.3
Khushi Sahu OS assignment 2
Program 6: Linux shell script to display good morning, good afternoon, good evening
according to system time.
hour=$(date +%H)
if [ $hour -lt 12 ]
then
greet="Good Morning"
elif [ $hour -le 16 ]
then
greet="Good Afternoon"
elif [ $hour -lt 20 ]
then
greet="Good Evening"
else
greet="Good Night"
fi
echo "$greet"
Program 7: Linux shell script program to demonstrate the ‘$#’ variable and Linux shell
script program to print the current process id.
# Linux shell script program to print the current process id.
echo "Current process identifier: $$"
Page no.4
Khushi Sahu OS assignment 2
Program 11: Linux shell script to find greatest of two number and three number from
user input.
echo "Enter the first number:"
read num1
echo "Enter the second number:"
read num2
echo "Enter the third number (optional, press Enter if not applicable):"
Page no.5
Khushi Sahu OS assignment 2
read num3
# Checking for greatest number among two
if [ "$num3" = "" ]; then
if [ "$num1" -gt "$num2" ]; then
echo "The greatest number is $num1"
else
echo "The greatest number is $num2"
fi
else
# Checking for greatest number among three
if [ "$num1" -gt "$num2" ] && [ "$num1" -gt "$num3" ]; then
echo "The greatest number is $num1"
elif [ "$num2" -gt "$num1" ] && [ "$num2" -gt "$num3" ]; then
echo "The greatest number is $num2"
else
echo "The greatest number is $num3"
fi
fi
Program 12: Linux shell script factorial of number from user input.
echo "Enter a number:"
read number
factorial=1
# Check if the number is less than 0
if [ $number -lt 0 ]; then
echo "Factorial is not defined for negative numbers."
exit
fi
# Calculate factorial
for (( i=1; i<=$number; i++ )); do
factorial=$((factorial * i))
done
echo "The factorial of $number is: $factorial"
Page no.6
Khushi Sahu OS assignment 2
Program 14: Linux shell script program whether year is leaping year or not?
echo "Enter a year:"
read year
# Check if the year is divisible by 4 and not divisible by 100
if [ $((year % 4)) -eq 0 ] && [ $((year % 100)) -ne 0 ]; then
echo "$year is a leap year."
# Check if the year is divisible by 400
elif [ $((year % 400)) -eq 0 ]; then
echo "$year is a leap year."
else
echo "$year is not a leap year."
fi
Page no.7
Khushi Sahu OS assignment 2
Page no.8