Sum of 5 numbers with type validation in bash

Anju
Apr 1, 2021

--

#!/bin/bash
echo -e "Enter Five Numbers To Sum\n"
sum=0
cnt=1
while [ $cnt -le 5 ]
do
read -p "enter $cnt number:" Num
if ! [[ "$Num" =~ ^[0–9]+$ ]]
then
echo "Sorry integers only"
else
sum=$((sum + Num))
cnt=$((cnt + 1))
fi
done
echo "Sum of these FIVE numbers: $sum"

--

--

Anju
Anju

Written by Anju

A DevOps engineer who loves automating everything (almost), exploring new places, and finding peace in nature. Always looking for the next adventure!

No responses yet