Lab Report 2
Lab Report 2
Lab Report NO # 02
Course Title: Operating System Lab
Course Code: CSE 310 Section: 221-D23
Student Details
Name ID
1. Md. Fahad Nakib 221902149
2. OBJECTIVES/AIM
familiarizing with Linux command line interpreter
To gather knowledge of shell program.
To communicate system using shell scripting.
3. IMPLEMENTATION
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:
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:
Output:
Output:
Write a Shell program to find the sum of odd and even numbers from a set of numbers.
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.
Output:
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:
Output:
Write a shell program to display odd position numbers (using For loop).
Output:
Write a Shell program for counting number of digit in a number using while loop:
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: