2

Please help me to find out this, I am very new to UNIX, My code is really can't get the right answer...

#! /usr/bin/env bash

echo -n "How many number : "; read num
for ((i=0; i<$num; i++));
do 
    echo -n "Enter your number : "; read number 
total+=$(echo "${number}" | bc)
echo "${total}"

done
2
  • #! /usr/bin/env bash echo -n "How many number : "; read num for ((i=0; i<$num; i++)); do echo -n "Enter your number : "; read number total+=$(echo "${number}" | bc) echo "${total}" done Commented Jan 10, 2014 at 19:49
  • Please edit your question instead of putting what is missing in a comment. Commented Jan 10, 2014 at 20:08

1 Answer 1

2
echo -n "Enter your numbers: "
read numbers
total=$(echo "$numbers" | sed 's/ \+/ + /g' | bc)
echo "The total is $total"

Sample use:

Enter your numbers: 4 6 2.47
The total is 12.47

MORE: In your script, the problem was the line total+=$(echo "${number}" | bc). This fixes it:

#! /usr/bin/env bash
total=0
echo -n "How many number : "; read num
for ((i=0; i<$num; i++));
do 
    echo -n "Enter your number : "; read number 
    total=$(echo "$total + ${number}" | bc)
    echo "${total}"
done
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.