0% found this document useful (0 votes)
24 views

Os Ass2

The document contains 15 programs written in Linux shell script with explanations. The programs demonstrate various shell scripting concepts like variables, operators, conditional statements, loops, functions etc.

Uploaded by

Khushi sahu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Os Ass2

The document contains 15 programs written in Linux shell script with explanations. The programs demonstrate various shell scripting concepts like variables, operators, conditional statements, loops, functions etc.

Uploaded by

Khushi sahu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Khushi Sahu OS assignment 2

SHRI G.S. INSTITUTE OF TECHNOLOGY & SCIENCE


INDORE

Department of Computer Technology and Applications


MCA First Year Semester II Session: Feb-July 2023

LAB ASSIGNMENT - 2
CT10709: OPERATING SYSTEM

SUBMITTED TO: SUBMITTED BY:


Mrs. Shweta Gupta Khushi Sahu
Enroll No. 0801CA231074

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"

Program 2: Linux shell script program using calculator operations like +, -, *, /, %, ^,


++, -- operator. Perform all operations and read the values from the user terminals.
echo “enter first number:”
read a
echo “enter second number:”
read b
echo “choose operation”
echo “1.addition”
echo “2.substraction”
echo “3.multiplication”
echo “4.division”
echo “5.modulus”
echo “6. exponential”
echo “7. increment”
echo “8. decrement”
read choice
case $choice in
1) echo “sum is:” $((a+b));;
2) echo “subtract is:” $((a-b));;
3) echo “multiplication is:” $((a*b));;
4) echo “division is:” $((a/b));;
5) echo “modolus is:” $((a%b));;
6) echo “exponential is:” $((a**b));;
7) echo “increment of a:” $((++a));;
8) echo “decrement of b:” $((--b));;
*) echo “invalid choice”
esac

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"

Without using third variable:


a=25
b=12
echo “number before swapping: $a $b”
a=$((a+b))
b=$((a-b))
a=$((a-b))
echo “number after swapping: $a $b”

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

Program 5: Bach shell script to find sum of digits.


echo "Enter a number"
read num
sum=0
while [ $num -gt 0 ]
do
mod=$((num % 10)) #It will split each digits
sum=$((sum + mod)) #Add each digit to sum
num=$((num / 10)) #divide num by 10.
done
echo $sum

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: $$"

# Linux shell script program to demonstrate the "$#" variable.


echo "Total command line arguments are: $#"

Program 8: Linux shell script to demonstrate comparison operator.


a=5
b=10
if [ $a -eq $b ] then
echo "$a is equal to $b"
fi
if [ $a -ne $b ] then
echo "$a is not equal to $b"
fi
if [ $a -lt $b ] then
echo "$a is less than $b"
fi
if [ $a -gt $b ] then
echo "$a is greater than $b"
fi

Page no.4
Khushi Sahu OS assignment 2

Program 9: Linux shell script to find the number is even or odd.


echo "Enter a number"
read number
# Check if the number is divisible by 2
if [ $((number % 2)) -eq 0 ];
then
echo "$number is even."
else
echo "$number is odd."
fi

Program 10: Linux shell script to find number is prime or not.


echo "Enter a number:"
read number
# Check if the number is less than or equal to 1
if [ $number -le 1 ]; then
echo "$number is not a prime number."
exit
fi
# Check for factors from 2 to the square root of the number
is_prime=true
for (( i=2; i*i<=$number; i++ )); do
if [ $((number % i)) -eq 0 ]; then
is_prime=false
break
fi
done
# Print whether the number is prime or not
if $is_prime; then
echo "$number is a prime number."
else
echo "$number is not a prime number."
fi

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"

Program 13: Linux to demonstrate switch case statement.


echo "Enter a fruit name (apple, banana, orange):"
read fruit
case "$fruit" in
"apple")

Page no.6
Khushi Sahu OS assignment 2

echo "You chose an apple." ;;


"banana")
echo "You chose a banana." ;;
"orange")
echo "You chose an orange.” ;;
*)
echo "Invalid fruit choice.” ;;
esac

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

Program 15: Linux shell script program Armstrong no. Or not?


echo "Enter a number:"
read number
# Function to calculate the number of digits in a number
count_digits() {
echo -n $1 | wc -c
}
# Function to check if the number is an Armstrong number
is_armstrong() {
local num=$1
local num_of_digits=$(count_digits $num)
local sum=0
local temp=$num
while [ $temp -gt 0 ]; do
digit=$((temp % 10))
temp=$((temp / 10))
sum=$((sum + digit ** num_of_digits))
done
if [ $sum -eq $num ]; then

Page no.7
Khushi Sahu OS assignment 2

echo "$num is an Armstrong number."


else
echo "$num is not an Armstrong number."
fi
}
is_armstrong $number

Page no.8

You might also like