This document contains 5 shell script examples:
1. A script that performs basic mathematical operations like addition, subtraction, multiplication, and division on two numbers.
2. A script that swaps the values of two numbers using a temporary variable.
3. A script that swaps two numbers without using a temporary variable.
4. A script that determines whether a given number is even or odd.
5. An assignment to write a script that calculates percentage from marks of 5 subjects and assigns a grade based on percentage ranges.
1. Dr. D. P.
Mishra
Digitally signed by Dr. D. P. Mishra
DN: cn=Dr. D. P. Mishra, o=durg,
ou=BIT, email=dpmishra@bitdurg.
ac.in, c=IN
Date: 2023.03.13 12:39:50 +05'30'
2. #!/bin/sh
# This script performs basic mathematical operations
echo "Enter the first number: "
read num1
echo "Enter the second number: "
read num2
# Addition
add=$((num1 + num2))
echo "The sum of $num1 and $num2 is $add.โ
# Subtraction
sub=$((num1 - num2))
echo "The difference of $num1 and $num2 is $sub."
# Multiplication
mul=$((num1 * num2))
echo "The product of $num1 and $num2 is
$mul."
# Division
if [ $num2 -ne 0 ]
then
div=$((num1 / num2))
echo "The quotient of $num1 and $num2
is $div."
else
echo "Cannot divide by zero."
fi
Linux
Laboratory
-
B.Tech.
6th
CSE
-
Dr.
D.
P.
Mishra,
BIT
Durg
5.1- Shell Script for performing basic
mathematical operations
3. #!/bin/sh
# This script swaps the values of
two numbers using a temporary
variable
echo "Enter the first number: "
read num1
echo "Enter the second number: "
read num2
echo "Before swapping, num1 is
$num1 and num2 is $num2."
# Swap the values of num1 and num2
using a temporary variable
temp=$num1
num1=$num2
num2=$temp
echo "After swapping, num1 is $num1
and num2 is $num2."
Linux
Laboratory
-
B.Tech.
6th
CSE
-
Dr.
D.
P.
Mishra,
BIT
Durg
5.2- Shell Script for Swapping values of two
numbers using temporary variables
4. #!/bin/sh
# This script swaps two numbers
without using a temporary variable
echo "Enter the first number: "
read num1
echo "Enter the second number: "
read num2
echo "Before swapping, num1 is
$num1 and num2 is $num2."
# Swap the values of num1 and num2
without a temporary variable
num1=$((num1 + num2))
num2=$((num1 - num2))
num1=$((num1 - num2))
echo "After swapping, num1 is $num1
and num2 is $num2."
Linux
Laboratory
-
B.Tech.
6th
CSE
-
Dr.
D.
P.
Mishra,
BIT
Durg
5.3- Shell Script to swap two numbers
without Temporary Variables
5. 5.4- Shell Script for determining whether
number is even or odd
Linux
Laboratory
-
B.Tech.
6th
CSE
-
Dr.
D.
P.
Mishra,
BIT
Durg
#!/bin/sh
# This script determines whether a number is even or odd
echo "Enter a number: "
read num
if [ $((num % 2)) -eq 0 ]
then
echo "$num is even."
else
echo "$num is odd."
fi
6. 5.5:Task/Assignment
โข Write a shell script to input marks of five subjects Physics, Chemistry,
Biology, Mathematics and Computer. Calculate percentage and grade
according to following logic:
โข Percentage >= 90% : Grade A,
โข Percentage >= 80% : Grade B,
โข Percentage >= 70% : Grade C,
โข Percentage >=60% . Grade D,
โข Percentage >= 40% : Grade E,
โข Percentage < 40% : Grade F.
Linux
Laboratory
-
B.Tech.
6th
CSE
-
Dr.
D.
P.
Mishra,
BIT
Durg