In: Computer Science
Create a bash script that takes numbers as parameters, calculates sum and prints the result. If no parameters passed, prompt a user (only one time) to enter numbers to sum and print the result.
Submit your program as LastName_FirstName.sh file.
RAW CODE (ALONG WITH COMMENTS)
#! /bin/bash
sum=0
if [[ $# -eq 0 ]] ### In case no argument is provided
then
echo "Enter Numbers to sum" ### Asking for numbers to sum
read -a arr ### Reading numbers in array
for arg in ${arr[@]}
do
((sum += arg)) ### summing all the numbers
done
echo "Sum = $sum" ### Printing sum
else ### In case arguments are provided
for arg in "$@" ; do
((sum += arg)) ### summing them
done
echo "Sum = $sum" ### Printing sum
fi
SCREENSHOTS (CODE AND OUTPUT)
##### FOR ANY QUERY, KINDLY GET BACK, THANKYOU. #####