0% found this document useful (0 votes)
39 views3 pages

Solutions

Uploaded by

sahil1740b
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)
39 views3 pages

Solutions

Uploaded by

sahil1740b
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/ 3

Question 1: Using equalities in Bash

Method 1:
#!/bin/bash
echo "Enter number 1: "
read n1
echo "Enter number 2: "
read n2

if [ "$n1" -eq "$n2" ]; then


echo "$n1 is equal to $n2"
elif [ "$n1" -gt "$n2" ]; then
echo "$n1 is greater than $n2"
else
echo "$n1 is less than $n2"
fi

Method 2:
#!/bin/bash
echo "Enter number 1: "
read n1
echo "Enter number 2: "
read n2

if [[ "$n1" == "$n2" ]]; then


echo "$n1 is equal to $n2"
elif [[ "$n1" > "$n2" ]]; then
echo "$n1 is greater than $n2"
else
echo "$n1 is less than $n2"
fi
Question 2: Counting the number of elements in an array.
Method 1:
#!/bin/bash
myarr=("apple banana" orange grapes kiwi)
echo "Length of the array : ${#myarr[@]}"

Method 2:
#!/bin/bash
myarr=("apple banana" orange grapes kiwi)
c=0
for element in "${myarr[@]}"
do
((c++))
done

echo "The length of the array is: $c"


Question 3: Calculator
echo -n "Enter Number 1: "
read X
echo -n "Enter Number 2: "
read Y
echo "$X + $Y = $(( X+Y))"
echo "$X - $Y = $(( X-Y))"
echo "$X * $Y = $(( X*Y))"
echo "$X / $Y = $(( X/Y))"

You might also like