-5

http://imgur.com/a/kLG3f

Hello all,

Im trying to write a script that will add any input numbers and return the sum. I have this part, here's the code:

    sum=0
    for number in "$@";
    do ((sum += number))
    done
    echo $sum

Now I need it to do the rest that is attached in the image, but we only learned if statements and for loops two days ago, and I'm having trouble getting through this. I've tried all combinations I could think of, and I just end up getting frustrating errors.

  • Please copy and paste the text in the image. It's too hard to read on my device. – roaima Dec 11 '16 at 22:48
  • What have you tried and what errors do you get? Please include this in your question. – roaima Dec 11 '16 at 22:50
0

if you can use awk this has been answered in

https://stackoverflow.com/questions/450799/shell-command-to-sum-integers-one-per-line

You did not say what shell you are using but it looks like sh or bash. What you have is correct if it is put in a script (but will not work at the command line). I put your code in a file named test.sh and then executed it as:

sh test.sh 10 20 30

and it printed out the correct value of 60

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.