Shell Script to find Greatest and Smallest number (it is Successfully running)
#vi maxminnum.sh
echo "enter size of an array"
read n
#taking input from user
for((i=0;i<n;i++))
do
echo " enter $((i+1)) number"
read nos[$i]
done
#printing the entered number
echo "number entered are"
for((i=0;i<n;i++))
do
echo ${nos[$i]}
done
#main loop
small=${nos[0]}
greatest=${nos[0]}
for((i=0;i<n;i++))
do
#logic for smallest number
if [ ${nos[$i]} -lt $small ]; then
small=${nos[$i]}
#logic for greatest number
elif [ ${nos[$i]} -gt $greatest ]; then
greatest=${nos[$i]}
fi
done
#printing smallest and greatest number
echo "smallest number in an array is $small"
echo "greatest number in an array is $greatest"
echo "enter size of an array"
read n
#taking input from user
for((i=0;i<n;i++))
do
echo " enter $((i+1)) number"
read nos[$i]
done
#printing the entered number
echo "number entered are"
for((i=0;i<n;i++))
do
echo ${nos[$i]}
done
#main loop
small=${nos[0]}
greatest=${nos[0]}
for((i=0;i<n;i++))
do
#logic for smallest number
if [ ${nos[$i]} -lt $small ]; then
small=${nos[$i]}
#logic for greatest number
elif [ ${nos[$i]} -gt $greatest ]; then
greatest=${nos[$i]}
fi
done
#printing smallest and greatest number
echo "smallest number in an array is $small"
echo "greatest number in an array is $greatest"
:wq!
Shell Script Output:
===============Running Script=======================
#chmod 755 maxminnum.sh or #chmod +x maxminnum.sh
#bash maxminnum.sh or #sh maxminnum.sh
enter size of an array
10
enter 1 number
34
enter 2 number
56
enter 3 number
87
enter 4 number
98
enter 5 number
88
enter 6 number
77
enter 7 number
34
enter 8 number
56
enter 9 number
76
enter 10 number
56
number entered are
34
56
87
98
88
77
34
56
76
56
smallest number in an array is 34
greatest number in an array is 98
enter size of an array
10
enter 1 number
34
enter 2 number
56
enter 3 number
87
enter 4 number
98
enter 5 number
88
enter 6 number
77
enter 7 number
34
enter 8 number
56
enter 9 number
76
enter 10 number
56
number entered are
34
56
87
98
88
77
34
56
76
56
smallest number in an array is 34
greatest number in an array is 98
No comments:
Post a Comment