0% found this document useful (0 votes)
28 views11 pages

Lab Report 2

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)
28 views11 pages

Lab Report 2

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/ 11

Green University of Bangladesh

Department of Computer Science and Engineering (CSE)


Faculty of Sciences and Engineering
Semester: (Spring, Year:2024), B.Sc. in CSE (Day)

Lab Report NO # 02
Course Title: Operating System Lab
Course Code: CSE 310 Section: 221-D23

Lab Experiment Name: Implementing Shell Scripting.

Student Details

Name ID
1. Md. Fahad Nakib 221902149

Lab Date : 23-03-24


Submission Date : 30-03-24
Course Teacher’s Name : Kazi Hasnayeen Emad

Lab Report Status


Marks: ………………………………… Signature:.....................
Comments:.............................................. Date:..............................
1. TITLE OF THE LAB REPORT EXPERIMENT
Implementing Shell Scripting.

2. OBJECTIVES/AIM
 familiarizing with Linux command line interpreter
 To gather knowledge of shell program.
 To communicate system using shell scripting.

3. IMPLEMENTATION

 Write a Shell program to find the area and circumference of a circle.


echo "Enter radius of circle : "
read R
area=$(echo "scale=4; $R*$R*3.1416" | bc)
circumference=$(echo "scale=4;2*$R*3.1416" | bc)
echo "Area: $area"
echo "Circumference: $circumference"

Output:

 Write a Shell program to find the roots of a quadratic equation.


read A;
if [ $A = 0 ]; then
echo "Not a quadratic equation.";
exit 0;
fi
read B;
read C;
D=$(( ($B)*($B)-4*($A)*($C) ));
if [ $D = 0 ]; then
echo -n "x = "
echo -e "scale=3\n-0.5*($B)/($A)" | bc
exit 0;
fi
echo $D
if [ $D -gt 0 ]; then
echo -n "x1 = "
echo -e "scale=3\n0.5*(-($B)+sqrt($D))/($A)" | bc
echo -n "x2 = "
echo -e "scale=3\n0.5*(-($B)-sqrt($D))/($A)" | bc
else
echo "Solution not possible!"
fi
Output:

 Write a Shell program to Find out the Area and Perimeter of Rectangle.
echo "Enter length :"
read l
echo "Enter width :"
read w
area=$((l*w))
perimeter=$((2*$((l+w))))
echo "Area : $area"
echo "Perimeter : $perimeter"

Output:

 Write a Shell program to Find the Triangle is valid or not.


echo "Enter the angles of triangle"
read A1
read A2
read A3
sum=$((A1+A2+A3))
if [ $sum -eq 180 ]
then
echo "Valid triangle"
else
echo "Invalid triangle"
fi

Output:
 Write a shell Script to Concatenate Two Strings.
echo "Enter 1st string:"
read a
echo "Enter 2nd string:"
read b
c=$a$b
echo "output : $c"

Output:

 Write a Shell program to find the largest among three numbers.


echo "Enter 3 numbe : "
read a
read b
read c
if [ $a == $b -a $a==$c ];
then
echo "Numbers are equal"
elif [ $a -gt $b -a $a -gt $c ];
then
echo "Large number : $a"
elif [ $b -gt $a -a $b -gt $c ];
then
echo "Large number : $b"
elif [ $c -gt $a -a $c -gt $b ];
then
echo "Large number : $c"
else
echo "None of the condition met"
fi

Output:

 Write a Shell program to display student grades using switch case.


read -p "Enter the person's name: " name
read -p "Enter the grade of the person: " grade
case $grade in
[50-59])
echo "$name $grade F";;
[60-69])
echo "$name $grade C";;
[70-79])
echo "$name $grade B";;
[80-100])
echo "$name $grade A+";;
*) echo "Wrong input"
esac

Output:

 Write a Shell program to find the sum of odd and even numbers from a set of numbers.

echo "Enter a number"


read n
echo "Even Numbers : "
esum=0
osum=0
i=1
while [ $i -le $n ]
do
rs=`expr $i % 2`
if [ $rs == 0 ]
then
echo " $i"
esum=$((esum+i))
fi
((++i))
done
echo "Sum of Even Numbers - $esum"
echo "Odd Numbers : "
i=1
while [ $i -le $n ]
do
rs=`expr $i % 2`
if [ $rs != 0 ]
then
echo " $i"
osum=$((osum+i))
fi
((++i))
done
echo "Sum of Odd Numbers - $osum"

Output:
 Write a Shell program to find the smallest number from a set of numbers.
arr=(5 4 3 2 1)
smallnum=10000000000000
for (( i=0; i<${#arr[@]}; i++ ))
do
if [ ${arr[i]} -lt $smallnum ]
then
smallnum=${arr[i]}
fi
done
echo "Array: ${arr[@]}"
echo "smallest number : $smallnum"

Output:

 Write a Shell program to find the second highest number from a number.
read -p "Enter a number: " num
rem=0
largest1=0
largest2=0
while (( $num != 0 )); do
rem=$(( num % 10 ))
if (( $rem > $largest1 )); then
largest2=$largest1
largest1=$rem
elif (( $rem > $largest2 && $rem != $largest1 )); then
largest2=$rem
fi
num=$((num / 10))
done
echo "The second largest digit is $largest2"

Output:
 Write a Shell program to find the factorial of a number using for loop.
read -p "Enter the number : " n
f=1
for (( i=1; i<=n; i++ )); do
f=$((f * i))
done
echo "The factorial is: $f"

Output:

 Write a Shell program to check the given integer is Armstrong number or not.

read -p "Enter a number: " num


length=${#num}
sum=0
rem=$num
while (($rem > 0 )); do
digit=$((rem % 10))
sum=$((sum + digit**length))
rem=$((rem / 10))
done
if (( $num == $sum )); then
echo "Number is armstrong."
else
echo "Number is not armstrong"
fi

Output:

 Write a Shell program to find the average of n numbers.

calculate_average() {
for (( i=1; i<=n; i++ )); do
read num
sum=$(( sum + num ))
done
echo $(( sum / n ))
}
read -p "Enter the number of element: " n
average=$(calculate_average)
echo "The average of the given numbers is: $average"

Output:

 Write a Shell Program to find the largest element of an array.


read -p "Enter the number of element: " size
for (( i = 0; i < size; i++ )); do
read array[$i]
done
largest=${array[0]}
for (( i=0; i<${#array[@]}; i++ )); do
element="${array[i]}"
if (( element > largest )); then
largest=$element
fi
done
echo $largest

Output:

 Write a shell program to display odd position numbers (using For loop).

read -p "Enter a number : " n


l=${#n}
if [[ $n =~ ^[+-]?[0-9]+$ ]];then
if [[ $n =~ ^[+-] ]];then
i=2
fi
if [[ $n =~ ^[0-9] ]];then
i=1
fi
while [ $i -le $l ]
do
d=$(echo $n | cut -c $i)
echo $d
i=$(($i + 2))
done
fi

Output:

 Write a Shell program for counting number of digit in a number using while loop:

echo "Enter the number:"


read number
digits=()
counts=()
index=0
while [ $number -gt 0 ]; do
digit=$((number % 10))
number=$((number / 10))
found=0
for (( i=0; i<${#digits[@]}; i++ )); do
if [ ${digits[i]} -eq $digit ]; then
counts[i]=$((counts[i]+1))
found=1
break
fi
done
if [ $found -eq 0 ]; then
digits[index]=$digit
counts[index]=1
index=$((index+1))
fi
done
echo "Output:"
for (( i=0; i<${#digits[@]}; i++ )); do
echo "${digits[i]} = ${counts[i]} times"
done

Output:
 Write a Shell program to find the factorial of two different numbers and sum of the
numbers using function.

factorial() {
local num=$1
local result=1
for (( i = 1; i <= num; i++ )); do
result=$((result * i))
done
echo $result
}
echo "Enter the first number:"
read num1
fact1=$(factorial $num1)
echo "Enter the second number:"
read num2
fact2=$(factorial $num2)
sum=$((fact1 + fact2))
echo "Factorial of $num1 is $fact1"
echo "Factorial of $num2 is $fact2"
echo "$fact1 + $fact2 = $sum"

Output:

 Write a Shell program to find total number of alphabets, digits or special characters in
a string.

str_size=100
alp=0
digit=0
splch=0
i=0
echo -n "Input the string : "
read -r str
len=${#str}
while (( $i < $len )); do
char="${str:$i:1}"
case $char in
[a-zA-Z]) alp=$((alp+1));;
[0-9]) digit=$((digit+1));;
*) splch=$((splch+1));;
esac
i=$((i+1))
done
echo "Number of Alphabets in the string is : $alp"
echo "Number of Digits in the string is : $digit"
echo "Number of Special characters in the string is : $splch"

Output:

6. ANALYSIS AND DISCUSSION


Shell scripts are sensitive to whitespace, which can affect how commands are interpreted. Using
backticks(~) or $(...) for command substitution can introduce issues if not handled properly. It's
important to consider things like whitespace and special characters in the output of the
substituted command. If we want to preserve whitespace within a variable, we need to double
quote it to prevent word splitting and globbing.

You might also like