This script is interactive
Meaning it can take input from the user,
Bash
#!/bin/bash
# array
numbers=()
# Begging for input
echo "Enter numbers to add to the array. Type 'done' when you are finished:"
# Read user input in a loop
while true; do
read -p "> " input # Prompt for input
if [[ "$input" == "done" ]]; then
break # Exit the loop if the user types 'done'
elif [[ "$input" =~ ^-?[0-9]+$ ]]; then
numbers+=("$input") # Add the number to the array
else
echo "Please enter a valid number or 'done' to finish."
fi
done
# Display the numbers in the array
echo "You entered the following numbers:"
for num in "${numbers[@]}"; do
echo "$num"
done
# Calculate the sum of the numbers in the array
sum=0
for num in "${numbers[@]}"; do
sum=$((sum + num)) # Add each number to the sum
done
# Display the sum
echo "The sum of the entered numbers is: $sum"
